Skip to content
Snippets Groups Projects

ISTWCMS-6525: Rewrite preprocess function so it does not through errors.

Closed Tyler Struyk requested to merge feature/ISTWCMS-6525-tstruyk-fix-revision-view-error into 1.0.x
+ 25
102
@@ -6,7 +6,6 @@
*/
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Entity\EntityInterface;
/**
* Implements hook_form_alter().
@@ -94,85 +93,36 @@ function uw_additional_options_preprocess_region(&$variables) {
// Get the current url path.
$current_path = \Drupal::service('path.current')->getPath();
// If the path contains revisions, we need to get the
// revision if from the url.
// If not we can get reivison id from the node object.
if (str_contains($current_path, '/revisions/')) {
// Remove everything before the revision id.
$revision_id = preg_replace('/\/node\/.*\/revisions\//', '', $current_path);
// Remove everything after the revision id.
$revision_id = preg_replace('/\/.*/', '', $revision_id);
}
else {
// Get the revision id from the node object.
$revision_id = $node->getLoadedRevisionId();
// If the revision is being viewed, then we need to make sure the menu
// of the additional options, displays correctly.
// If not we can get revision id from the node object.
$path_array = explode('/', $current_path);
if (in_array('revisions', $path_array)) {
// Switch is used, as there might be multiple paths that need to be
// tested.
switch (count($path_array)) {
case 6:
if ($path_array[1] == 'node' && $path_array[3] == 'revisions' && $path_array[5] == 'view' && is_numeric($path_array[4])) {
$revision_id = $path_array[4];
}
else {
$revision_id = $node->getLoadedRevisionId();
}
break;
default:
$revision_id = $node->getLoadedRevisionId();
}
// Set the uw_option based on the entity id.
$variables['uw_option'] = _uw_additional_options_get_option(
$node->id(),
$revision_id
);
}
// Set the uw_option based on the entity id.
$variables['uw_option'] = _uw_additional_options_get_option(
$node->id(),
$revision_id
);
}
}
}
/**
* Implements hook_entity_predelete().
*/
function uw_additional_options_entity_predelete(EntityInterface $entity) {
// Perform the query to delete the additional option using entity id.
\Drupal::database()->delete('uw_additional_options')
->condition('entity_id', $entity->id())
->execute();
}
/**
* Submit handler for UW additional options.
*
* @param array $form
* The form array.
* @param Drupal\Core\Form\FormStateInterface $form_state
* The form state array.
*/
function _uw_additional_options_form_submit(array $form, FormStateInterface $form_state): void {
// Get the content types not to include additional options.
$cts_not_to_use = _uw_additional_options_get_content_types_not_to_include();
// Get the type of entity.
$entity_type = $form_state->getformObject()->getEntity()->getType();
// If we have a content type with additional options, process it.
if (!in_array($entity_type, $cts_not_to_use)) {
// Get the values from the form state.
$values = $form_state->getValues();
// Get the node object from the form state.
$node = $form_state->getFormObject()->getEntity();
// Perform the query to either update or insert option.
\Drupal::database()->upsert('uw_additional_options')
->fields([
'entity_id',
'revision_id',
'uw_option',
])
->values([
$values['nid'],
$node->getLoadedRevisionId(),
$values['uw_option'],
])
->key('entity_id')
->execute();
}
}
/**
* Function to get the content types that do not have additional options.
*
@@ -209,30 +159,3 @@ function _uw_additional_options_get_entity_type_form_form_id(string $form_id): s
// Return the entity type.
return $entity_type;
}
/**
* Get the uw option from the entity id.
*
* @param int $entity_id
* The entity id.
* @param int $revision_id
* The revision id.
*
* @return string
* The option.
*/
function _uw_additional_options_get_option(int $entity_id, int $revision_id): string {
// Perform the query to get the option based on the id.
$query = \Drupal::database()
->select('uw_additional_options', 'uwo')
->fields('uwo', ['uw_option'])
->condition('uwo.entity_id', $entity_id)
->condition('uwo.revision_id', $revision_id);
// Get the option.
$uw_option = $query->execute()->fetchField();
// If there is an option, return it, if not return default of all.
return $uw_option ?? 'all';
}
Loading