Problem/Motivation

Drupal\navigation\Hook\NavigationHooks::modulesInstalled() uses the following code.

    // Do not modify config during sync. Config should be already consolidated.
    if ($is_syncing) {
      return;
    }
    foreach ($modules as $module) {
      $blocks = $this->moduleHandler->invoke($module, 'navigation_defaults');

      if (!is_array($blocks)) {
        return;
      }

      foreach ($blocks as $block) {
        $this->configActionManager->applyAction('addNavigationBlock', 'navigation.block_layout', $block);
      }
    }

It stops invoking those hook implementations after one of them returns a value that is different from the expected one.

navigation_install() uses instead the following code.

  // Do not modify config during sync. Config should be already consolidated.
  if ($is_syncing) {
    return;
  }

  $blocks = \Drupal::moduleHandler()->invokeAll('navigation_defaults');
  $manager = \Drupal::service('plugin.manager.config_action');
  foreach ($blocks as $block) {
    $manager->applyAction('addNavigationBlock', 'navigation.block_layout', $block);
  }

Proposed resolution

Change Drupal\navigation\Hook\NavigationHooks::modulesInstalled() to use the following code.

    // Do not modify config during sync. Config should be already consolidated.
    if ($is_syncing) {
      return;
    }
    foreach ($modules as $module) {
      $blocks = $this->moduleHandler->invoke($module, 'navigation_defaults');

      if (!is_array($blocks)) {
        continue;
      }

      foreach ($blocks as $block) {
        $this->configActionManager->applyAction('addNavigationBlock', 'navigation.block_layout', $block);
      }
    }

navigation_install() does not need to be changed, since ModuleHandler::invokeAll() returns an array.

Issue fork drupal-3604082

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

avpaderno created an issue. See original summary.

avpaderno’s picture

Title: Drupal\navigation\Hook\NavigationHooks::modulesInstalled() stops invoking hook_navigation_defaults() when an implementation returns a value that is not an array » NavigationHooks::modulesInstalled() does not invoke all the hook_navigation_defaults() implementations
Issue summary: View changes

avpaderno’s picture

Issue summary: View changes
avpaderno’s picture

Status: Active » Needs review
smustgrave’s picture

Status: Needs review » Needs work
Issue tags: +Needs tests

Can we. get test coverage showing this please.

avpaderno’s picture

Issue summary: View changes

charlliequadros made their first commit to this issue’s fork.

charlliequadros’s picture

Hi, everyone!

Added the unit test `NavigationHooksTest::testModulesInstalledContinuesWhenModuleHasNoNavigationDefaults()` to cover the fix in `NavigationHooks::modulesInstalled()`.

The test uses two modules: one whose `hook_navigation_defaults()` implementation returns `NULL`, and another that returns a valid navigation block. It verifies that `ModuleHandlerInterface::invoke()` is called for both modules and that `ConfigActionManager::applyAction()` is called only once, for the valid block.

charlliequadros’s picture

Status: Needs work » Needs review