Commit 634c8014 authored by paulocs's avatar paulocs Committed by paulocs

Issue #3018331 by paulocs, anzcar, Stephen Ollman, ydahi, bzoks, maticb:...

Issue #3018331 by paulocs, anzcar, Stephen Ollman, ydahi, bzoks, maticb: Install fails if module Menu Trail by Path is installed
parent 0701edb1
......@@ -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();
}
}
}
}
}
}
}
......@@ -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'));
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment