class MenuLinkContentHooks

Hook implementations for menu_link_content.

Hierarchy

Expanded class hierarchy of MenuLinkContentHooks

File

core/modules/menu_link_content/src/Hook/MenuLinkContentHooks.php, line 16

Namespace

Drupal\menu_link_content\Hook
View source
class MenuLinkContentHooks {
  use StringTranslationTrait;
  
  /**
   * Implements hook_help().
   */
  public function help($route_name, RouteMatchInterface $route_match) : ?string {
    switch ($route_name) {
      case 'help.page.menu_link_content':
        $output = '';
        $output .= '<h2>' . $this->t('About') . '</h2>';
        $output .= '<p>' . $this->t('The Custom Menu Links module allows users to create menu links. These links can be translated if multiple languages are used for the site.');
        if (\Drupal::moduleHandler()->moduleExists('menu_ui')) {
          $output .= ' ' . $this->t('It is required by the Menu UI module, which provides an interface for managing menus and menu links. For more information, see the <a href=":menu-help">Menu UI module help page</a> and the <a href=":drupal-org-help">online documentation for the Custom Menu Links module</a>.', [
            ':menu-help' => Url::fromRoute('help.page', [
              'name' => 'menu_ui',
            ])->toString(),
            ':drupal-org-help' => 'https://www.drupal.org/documentation/modules/menu_link',
          ]);
        }
        else {
          $output .= ' ' . $this->t('For more information, see the <a href=":drupal-org-help">online documentation for the Custom Menu Links module</a>. If you install the Menu UI module, it provides an interface for managing menus and menu links.', [
            ':drupal-org-help' => 'https://www.drupal.org/documentation/modules/menu_link',
          ]);
        }
        $output .= '</p>';
        return $output;
    }
    return NULL;
  }
  
  /**
   * Implements hook_entity_type_alter().
   */
  public function entityTypeAlter(array &$entity_types) : void {
    // @todo Moderation is disabled for custom menu links until when we have an UI
    //   for them.
    //   @see https://www.drupal.org/project/drupal/issues/2350939
    $entity_types['menu_link_content']->setHandlerClass('moderation', '');
  }
  
  /**
   * Implements hook_ENTITY_TYPE_delete().
   */
  public function menuDelete(MenuInterface $menu) : void {
    $storage = \Drupal::entityTypeManager()->getStorage('menu_link_content');
    $menu_links = $storage->loadByProperties([
      'menu_name' => $menu->id(),
    ]);
    $storage->delete($menu_links);
  }
  
  /**
   * Implements hook_ENTITY_TYPE_insert() for 'path_alias'.
   */
  public function pathAliasInsert(PathAliasInterface $path_alias) : void {
    _menu_link_content_update_path_alias($path_alias->getAlias());
  }
  
  /**
   * Implements hook_ENTITY_TYPE_update() for 'path_alias'.
   */
  public function pathAliasUpdate(PathAliasInterface $path_alias) : void {
    if ($path_alias->getAlias() != $path_alias->getOriginal()
      ->getAlias()) {
      _menu_link_content_update_path_alias($path_alias->getAlias());
      _menu_link_content_update_path_alias($path_alias->getOriginal()
        ->getAlias());
    }
    elseif ($path_alias->getPath() != $path_alias->getOriginal()
      ->getPath()) {
      _menu_link_content_update_path_alias($path_alias->getAlias());
    }
  }
  
  /**
   * Implements hook_ENTITY_TYPE_delete() for 'path_alias'.
   */
  public function pathAliasDelete(PathAliasInterface $path_alias) : void {
    _menu_link_content_update_path_alias($path_alias->getAlias());
  }
  
  /**
   * Implements hook_entity_predelete().
   */
  public function entityPredelete(EntityInterface $entity) : void {
    /** @var \Drupal\Core\Menu\MenuLinkManagerInterface $menu_link_manager */
    $menu_link_manager = \Drupal::service('plugin.manager.menu.link');
    $entity_type_id = $entity->getEntityTypeId();
    foreach ($entity->uriRelationships() as $rel) {
      $url = $entity->toUrl($rel);
      // Entities can provide uri relationships that are not routed, in this
      // case getRouteParameters() will throw an exception.
      if (!$url->isRouted()) {
        continue;
      }
      $route_parameters = $url->getRouteParameters();
      if (!isset($route_parameters[$entity_type_id])) {
        // Do not delete links which do not relate to this exact entity. For
        // example, "collection", "add-form", etc.
        continue;
      }
      // Delete all MenuLinkContent links that point to this entity route.
      $result = $menu_link_manager->loadLinksByRoute($url->getRouteName(), $route_parameters);
      if ($result) {
        foreach ($result as $id => $instance) {
          if ($instance->isDeletable() && str_starts_with($id, 'menu_link_content:')) {
            $instance->deleteLink();
          }
        }
      }
    }
  }

}

Members

Title Sort descending Modifiers Object type Summary Overrides
MenuLinkContentHooks::entityPredelete public function Implements hook_entity_predelete().
MenuLinkContentHooks::entityTypeAlter public function Implements hook_entity_type_alter().
MenuLinkContentHooks::help public function Implements hook_help().
MenuLinkContentHooks::menuDelete public function Implements hook_ENTITY_TYPE_delete().
MenuLinkContentHooks::pathAliasDelete public function Implements hook_ENTITY_TYPE_delete() for &#039;path_alias&#039;.
MenuLinkContentHooks::pathAliasInsert public function Implements hook_ENTITY_TYPE_insert() for &#039;path_alias&#039;.
MenuLinkContentHooks::pathAliasUpdate public function Implements hook_ENTITY_TYPE_update() for &#039;path_alias&#039;.
StringTranslationTrait::$stringTranslation protected property The string translation service. 3
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language. 1

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.