You are here

class MenuRouterRebuildSubscriber in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/lib/Drupal/Core/EventSubscriber/MenuRouterRebuildSubscriber.php \Drupal\Core\EventSubscriber\MenuRouterRebuildSubscriber

Rebuilds the default menu links and runs menu-specific code if necessary.

Hierarchy

Expanded class hierarchy of MenuRouterRebuildSubscriber

1 string reference to 'MenuRouterRebuildSubscriber'
core.services.yml in core/core.services.yml
core/core.services.yml
1 service uses MenuRouterRebuildSubscriber
menu.rebuild_subscriber in core/core.services.yml
Drupal\Core\EventSubscriber\MenuRouterRebuildSubscriber

File

core/lib/Drupal/Core/EventSubscriber/MenuRouterRebuildSubscriber.php, line 20
Contains \Drupal\Core\EventSubscriber\MenuRouterRebuildSubscriber.

Namespace

Drupal\Core\EventSubscriber
View source
class MenuRouterRebuildSubscriber implements EventSubscriberInterface {

  /**
   * @var \Drupal\Core\Lock\LockBackendInterface
   */
  protected $lock;

  /**
   * The menu link plugin manager.
   *
   * @var \Drupal\Core\Menu\MenuLinkManagerInterface $menuLinkManager
   */
  protected $menuLinkManager;

  /**
   * Constructs the MenuRouterRebuildSubscriber object.
   *
   * @param \Drupal\Core\Lock\LockBackendInterface $lock
   *   The lock backend.
   * @param \Drupal\Core\Menu\MenuLinkManagerInterface $menu_link_manager
   *   The menu link plugin manager.
   */
  public function __construct(LockBackendInterface $lock, MenuLinkManagerInterface $menu_link_manager) {
    $this->lock = $lock;
    $this->menuLinkManager = $menu_link_manager;
  }

  /**
   * Rebuilds the menu links and deletes the local_task cache tag.
   *
   * @param \Symfony\Component\EventDispatcher\Event $event
   *   The event object.
   */
  public function onRouterRebuild(Event $event) {
    $this
      ->menuLinksRebuild();
    Cache::invalidateTags(array(
      'local_task',
    ));
  }

  /**
   * Perform menu-specific rebuilding.
   */
  protected function menuLinksRebuild() {
    if ($this->lock
      ->acquire(__FUNCTION__)) {
      $transaction = db_transaction();
      try {

        // Ensure the menu links are up to date.
        $this->menuLinkManager
          ->rebuild();

        // Ignore any database replicas temporarily.
        db_ignore_replica();
      } catch (\Exception $e) {
        $transaction
          ->rollback();
        watchdog_exception('menu', $e);
      }
      $this->lock
        ->release(__FUNCTION__);
    }
    else {

      // Wait for another request that is already doing this work.
      // We choose to block here since otherwise the router item may not
      // be available during routing resulting in a 404.
      $this->lock
        ->wait(__FUNCTION__);
    }
  }

  /**
   * {@inheritdoc}
   */
  static function getSubscribedEvents() {

    // Run after CachedRouteRebuildSubscriber.
    $events[RoutingEvents::FINISHED][] = array(
      'onRouterRebuild',
      100,
    );
    return $events;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
MenuRouterRebuildSubscriber::$lock protected property
MenuRouterRebuildSubscriber::$menuLinkManager protected property The menu link plugin manager.
MenuRouterRebuildSubscriber::getSubscribedEvents static function Returns an array of event names this subscriber wants to listen to. Overrides EventSubscriberInterface::getSubscribedEvents
MenuRouterRebuildSubscriber::menuLinksRebuild protected function Perform menu-specific rebuilding.
MenuRouterRebuildSubscriber::onRouterRebuild public function Rebuilds the menu links and deletes the local_task cache tag.
MenuRouterRebuildSubscriber::__construct public function Constructs the MenuRouterRebuildSubscriber object.