Browse Source

only change view mode for the current node (#104)

* only change view mode for the current node
* fix coder issue
pull/729/head
Paul Pound 6 years ago committed by Seth Shaw
parent
commit
ef2e42170e
  1. 38
      islandora.module

38
islandora.module

@ -257,23 +257,27 @@ function islandora_entity_view_mode_alter(&$view_mode, EntityInterface $entity)
// ContextReaction.
$storage = \Drupal::service('entity_type.manager')->getStorage('entity_view_mode');
$context_manager = \Drupal::service('context.manager');
foreach ($context_manager->getActiveReactions('\Drupal\islandora\Plugin\ContextReaction\ViewModeAlterReaction') as $reaction) {
// Construct the new view mode's machine name.
$entity_type = $entity->getEntityTypeId();
$mode = $reaction->execute();
$machine_name = "$entity_type.$mode";
// Try to load it.
$new_mode = $storage->load($machine_name);
// If successful, alter the view mode.
if ($new_mode) {
$view_mode = $mode;
}
else {
// Otherwise, leave it be, but log a message.
\Drupal::logger('islandora')->info("EntityViewMode $machine_name does not exist. View mode cannot be altered.");
$current_entity = \Drupal::routeMatch()->getParameter('node');
$current_id = ($current_entity instanceof NodeInterface) ? $current_entity->id() : NULL;
if (isset($current_id) && $current_id == $entity->id()) {
foreach ($context_manager->getActiveReactions('\Drupal\islandora\Plugin\ContextReaction\ViewModeAlterReaction') as $reaction) {
// Construct the new view mode's machine name.
$entity_type = $entity->getEntityTypeId();
$mode = $reaction->execute();
$machine_name = "$entity_type.$mode";
// Try to load it.
$new_mode = $storage->load($machine_name);
// If successful, alter the view mode.
if ($new_mode) {
$view_mode = $mode;
}
else {
// Otherwise, leave it be, but log a message.
\Drupal::logger('islandora')
->info("EntityViewMode $machine_name does not exist. View mode cannot be altered.");
}
}
}
}

Loading…
Cancel
Save