diff --git a/context.module b/context.module index 95eac10c13de946ac7a8802c2ef9be2c2f4358f5..5d734deed0aaa17cfd8962ce38207e4aa1c3e620 100644 --- a/context.module +++ b/context.module @@ -8,6 +8,8 @@ use Drupal\Core\Render\Markup; use Drupal\Core\Routing\RouteMatchInterface; use Drupal\Component\Utility\NestedArray; +use Drupal\context\ContextMenuActiveTrail; +use Drupal\Core\Form\FormStateInterface; /** * Implements hook_help(). @@ -86,3 +88,46 @@ function context_theme_suggestions_page_alter(array &$suggestions, array $variab $suggestions = array_merge($suggestions, $template_suggestions); } } + +/** + * Implements hook_form_alter(). + */ +function context_form_alter(&$form, FormStateInterface $form_state, $form_id) { + // If this is Context form. + if ($form_id === 'context_edit_form') { + $reactions = $form["reactions"]["#process"]; + foreach ($reactions as $reaction) { + foreach ($reaction as $react) { + if ( + is_object($react) && + property_exists($react, 'entity') && + $react->getEntity()->getEntityTypeId() === 'context' + ) { + + // If menu reaction is selected. + $entity = $react->getEntity(); + if ( + !empty($entity->get('reactions')) && + array_key_exists('menu', $entity->get('reactions')) + ) { + + // Verify is the correct context class service is correct. + $definition = \Drupal::service('menu.active_trail'); + if (!$definition instanceof ContextMenuActiveTrail) { + // Warn users about this skip. + $messenger = \Drupal::messenger(); + $messenger->addMessage(t( + '@module will not work because @service has a different menu service provider.', + [ + '@module' => 'Context module: "Menu Reactions"', + '@service' => 'menu.active_trail', + ] + ), $messenger::TYPE_WARNING); + $form_state->disableRedirect(); + } + } + } + } + } + } +} diff --git a/src/ContextServiceProvider.php b/src/ContextServiceProvider.php index 96aeebf8074a19e1d693c449ad5fe8aefbfb42c3..f6400f313d7a6d7b840dcc3401749a057c251387 100644 --- a/src/ContextServiceProvider.php +++ b/src/ContextServiceProvider.php @@ -4,6 +4,7 @@ namespace Drupal\context; use Drupal\Core\DependencyInjection\ContainerBuilder; use Drupal\Core\DependencyInjection\ServiceProviderBase; +use Drupal\Core\Menu\MenuActiveTrail; /** * Alter the service container to use a custom class. @@ -15,9 +16,12 @@ class ContextServiceProvider extends ServiceProviderBase { */ public function alter(ContainerBuilder $container) { // Override the menu active trail with a new class. - $definition = $container->getDefinition('menu.active_trail'); - $definition->setClass('Drupal\context\ContextMenuActiveTrail'); - $definition->addArgument($container->getDefinition('context.manager')); + $definition = \Drupal::service('menu.active_trail'); + if (!is_subclass_of($definition, 'Drupal\Core\Menu\MenuActiveTrail') && $definition instanceof MenuActiveTrail) { + $definition = $container->getDefinition('menu.active_trail'); + $definition->setClass('Drupal\context\ContextMenuActiveTrail'); + $definition->addArgument($container->getDefinition('context.manager')); + } } }