Commit 364e3f90 authored by Robert Phair's avatar Robert Phair Committed by Robert Phair
Browse files

Issue #3036501 by siegrist, rphair: Service menu.active_trail dependency injection

parent 85e50441
Loading
Loading
Loading
Loading
+1 −2
Original line number Diff line number Diff line
@@ -9,8 +9,7 @@ remove_home: false
add_home: false
home_as_site_name: false
exclude_empty_url: false
# as of Beta, this option is obsolete:
# hide_on_single_item: false
derived_active_trail: false
menu_breadcrumb_menus:
  main:
    enabled: 1
+3 −0
Original line number Diff line number Diff line
@@ -35,6 +35,9 @@ menu_breadcrumb.settings:
    exclude_empty_url:
      type: boolean
      label: 'Exclude menu items with empty URLs.'
    derived_active_trail:
      type: boolean
      label: 'Derive MenuActiveTrail from RouteMatch.'
    menu_breadcrumb_menus:
      type: sequence
      label: 'Menu Breadcrumb Menu Configurations'
+1 −1
Original line number Diff line number Diff line
services:
  menu_breadcrumb.breadcrumb.default:
    class: Drupal\menu_breadcrumb\MenuBasedBreadcrumbBuilder
    arguments: ['@config.factory', '@plugin.manager.menu.link', '@router.admin_context', '@title_resolver', '@request_stack', '@language_manager', '@entity_type.manager', '@cache.menu', '@lock']
    arguments: ['@config.factory', '@menu.active_trail', '@plugin.manager.menu.link', '@router.admin_context', '@title_resolver', '@request_stack', '@language_manager', '@entity_type.manager', '@cache.menu', '@lock']
    tags:
      # The priority must be higher than core taxonomy builder (priority: 1002)
      # see https://www.drupal.org/node/1495510
+8 −11
Original line number Diff line number Diff line
@@ -59,7 +59,6 @@ class SettingsForm extends ConfigFormBase {
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
    $config = $this->config('menu_breadcrumb.settings');
    // Renamed from the now meaningless option "determine_menu":
    $form['determine_menu'] = [
      '#type' => 'checkbox',
      '#title' => $this->t('Enable the Menu Breadcrumb module'),
@@ -121,12 +120,6 @@ class SettingsForm extends ConfigFormBase {
      ],
    ];

    // Removed option "hide_on_single_item" - makes no sense when the taxonomy
    // attachment feature is added, especially now that this module reverts to
    // other breadcrumb builders (e.g., the path-based system breadcrumb) when
    // it doesn't apply (can be reconsidered if there is a valid use case).
    // $form['hide_on_single_item'] ...
    //
    $form['remove_home'] = [
      '#type' => 'checkbox',
      '#title' => $this->t('Remove "Home" link'),
@@ -155,6 +148,13 @@ class SettingsForm extends ConfigFormBase {
      '#default_value' => $config->get('exclude_empty_url'),
    ];

    $form['derived_active_trail'] = [
      '#type' => 'checkbox',
      '#title' => $this->t('Derive MenuActiveTrail from RouteMatch'),
      '#description' => $this->t('If FALSE, the injected @menu.active_trail service will be used.'),
      '#default_value' => $config->get('derived_active_trail'),
    ];

    $form['include_exclude'] = [
      '#type' => 'fieldset',
      '#title' => $this->t('Enable / Disable Menus'),
@@ -213,10 +213,6 @@ class SettingsForm extends ConfigFormBase {
        ],
      ];
    }
    // Removed description of a "Default setting" selection which was never
    // implemeneted in the current D8 version of Menu Breadcrumb.
    // TODO perhaps find out if this option would be worth preserving
    // (applied to default tick boxes for new menus added in the future).
    return parent::buildForm($form, $form_state);
  }

@@ -237,6 +233,7 @@ class SettingsForm extends ConfigFormBase {
      ->set('add_home', (boolean) $form_state->getValue('add_home'))
      ->set('menu_breadcrumb_menus', $form_state->getValue('menu_breadcrumb_menus'))
      ->set('exclude_empty_url', $form_state->getValue('exclude_empty_url'))
      ->set('derived_active_trail', $form_state->getValue('derived_active_trail'))
      ->save();

    parent::submitForm($form, $form_state);
+21 −5
Original line number Diff line number Diff line
@@ -38,6 +38,13 @@ class MenuBasedBreadcrumbBuilder implements BreadcrumbBuilderInterface {
   */
  protected $configFactory;

  /**
   * The menu active trail interface.
   *
   * @var \Drupal\Core\Menu\MenuActiveTrailInterface
   */
  protected $menuActiveTrail;

  /**
   * The menu link manager interface.
   *
@@ -134,6 +141,7 @@ class MenuBasedBreadcrumbBuilder implements BreadcrumbBuilderInterface {
   */
  public function __construct(
    ConfigFactoryInterface $config_factory,
    MenuActiveTrailInterface $menu_active_trail,
    MenuLinkManagerInterface $menu_link_manager,
    AdminContext $admin_context,
    TitleResolverInterface $title_resolver,
@@ -144,6 +152,7 @@ class MenuBasedBreadcrumbBuilder implements BreadcrumbBuilderInterface {
    LockBackendInterface $lock
  ) {
    $this->configFactory = $config_factory;
    $this->menuActiveTrail = $menu_active_trail;
    $this->menuLinkManager = $menu_link_manager;
    $this->adminContext = $admin_context;
    $this->titleResolver = $title_resolver;
@@ -214,10 +223,17 @@ class MenuBasedBreadcrumbBuilder implements BreadcrumbBuilderInterface {
          }
        }

        if ($this->config->get('derived_active_trail')) {
          // Do not use the global MenuActiveTrail service because we need one
          // which is aware of the given routeMatch, not of the global one.
          $menuActiveTrail = new MenuActiveTrail($this->menuLinkManager, $route_match, $this->cacheMenu, $this->lock);
          $trail_ids = $menuActiveTrail->getActiveTrailIds($menu_name);
        }
        else {
          // Default, for the majority & compatibility with historical use and
          // other modules: use the global (injected) MenuActiveTrail service.
          $trail_ids = $this->menuActiveTrail->getActiveTrailIds($menu_name);
        }
        $trail_ids = array_filter($trail_ids);
        if ($trail_ids) {
          $this->menuName = $menu_name;