Commit e4ac4dad authored by Chris Green's avatar Chris Green Committed by code-brighton
Browse files

#3279267 Adding theme_hook_suggestions for the menus that are rendered.

parent 18777ffb
Loading
Loading
Loading
Loading
+44 −0
Original line number Diff line number Diff line
@@ -56,3 +56,47 @@ function field_menu_theme() {
    ],
  ];
}

/**
 * Implements hook_theme_registry_alter().
 */
function field_menu_theme_registry_alter(&$theme_registry) {
  // Add $field_menu_configuration as a variable to the 'menu' theme hook. Set
  // its default value to be an empty array.
  $theme_registry['menu']['variables']['field_menu_configuration'] = [];
}

/**
 * Implements hook_theme_suggestions_HOOK() for "menu".
 */
function field_menu_theme_suggestions_menu(array $variables) {
  $suggestions = [];
  // The MenuTreeFormatter plugin's viewElements() method populates this variable.
  if (!empty($variables['field_menu_configuration'])) {
    $menu_name = strtr($variables['menu_name'], '-', '_');
    $suggestions[] = 'menu__' . $menu_name;
    $suggestions[] = 'field_menu__menu';
    $suggestions[] = 'field_menu__menu__' . $menu_name;
    // Add our custom theme suggestion.
    $config = $variables['field_menu_configuration'];
    if (!empty($config['suggestion']) && $config['suggestion'] !== $menu_name) {
      $suggestions[] = 'field_menu__menu';
      $suggestions[] = 'field_menu__menu__' . $menu_name;
    }
    if (!empty($config['providing_entity']) && $config['providing_entity'] instanceof \Drupal\Core\Entity\EntityBase) {
      $entity = $config['providing_entity'];
      $type = $entity->getEntityTypeId();
      $bundle = $entity->bundle();
      $suggestions[] = 'menu__' . $type . '__field_menu';
      $suggestions[] = 'menu__' . $type . '__field_menu__' . $menu_name;
      $suggestions[] = 'menu__' . $type . '__' . $bundle . '__field_menu';
      $suggestions[] = 'menu__' . $type . '__' . $bundle . '__field_menu__' . $menu_name;
      if(!empty($config['view_mode'])) {
        $view_mode = $config['view_mode'];
        $suggestions[] = 'menu__' . $type . '__' . $bundle . '__' . $view_mode . '__field_menu';
        $suggestions[] = 'menu__' . $type . '__' . $bundle . '__' . $view_mode . '__field_menu__' . $menu_name;
      }
    }
  }
  return $suggestions;
}
+13 −2
Original line number Diff line number Diff line
@@ -78,6 +78,9 @@ class MenuTreeFormatter extends FormatterBase implements ContainerFactoryPluginI
   */
  public function viewElements(FieldItemListInterface $items, $langcode) {
    $elements = [];
    // Set the values here so we can pass them to the menu.
    $this->configuration['providing_entity'] = $items->getEntity();
    $this->configuration['view_mode'] = $this->viewMode;

    foreach ($items as $delta => $item) {

@@ -108,9 +111,17 @@ class MenuTreeFormatter extends FormatterBase implements ContainerFactoryPluginI

      $tree = $this->menuLinkTree->transform($tree, $manipulators);
      $tree_render_array = $this->menuLinkTree->build($tree);

      $menu_title = trim($item->menu_title);

      if (!empty($tree_render_array['#theme'])) {
        // Add the configuration for use in menu_block_theme_suggestions_menu().
        $tree_render_array['#field_menu_configuration'] = $this->configuration;
        // Set the generated label into the configuration array so it is
        // propagated to the theme preprocessor and template(s) as needed.
        $tree_render_array['#field_menu_configuration']['label'] = $menu_title;
        // Remove the menu name-based suggestion so we can control its precedence
        // better in menu_block_theme_suggestions_menu().
        $tree_render_array['#theme'] = 'menu';
      }
      $elements[$delta] = [
        '#theme' => 'field_menu_item',
        '#title' => $menu_title,