You are here

class MenuLinkContentMigrateSubscriber in Multiversion 8

MenuContentLinkMigrateSubscriber class.

A menu_tree database table should be rediscovered after enabling/disabling a menu_link_content entity.

Hierarchy

Expanded class hierarchy of MenuLinkContentMigrateSubscriber

1 string reference to 'MenuLinkContentMigrateSubscriber'
multiversion.services.yml in ./multiversion.services.yml
multiversion.services.yml
1 service uses MenuLinkContentMigrateSubscriber
multiversion.menu_content_link_migrate_subscriber in ./multiversion.services.yml
Drupal\multiversion\EventSubscriber\MenuLinkContentMigrateSubscriber

File

src/EventSubscriber/MenuLinkContentMigrateSubscriber.php, line 18

Namespace

Drupal\multiversion\EventSubscriber
View source
class MenuLinkContentMigrateSubscriber implements EventSubscriberInterface {

  /**
   * @var \Drupal\Core\Database\Connection
   */
  protected $connection;

  /**
   * @var \Drupal\Core\Menu\MenuLinkManagerInterface
   */
  protected $menuLinkManager;

  /**
   * Class constructor.
   *
   * @param \Drupal\Core\Database\Connection $connection
   * @param \Drupal\Core\Menu\MenuLinkManagerInterface $menu_link_manager
   */
  public function __construct(Connection $connection, MenuLinkManagerInterface $menu_link_manager) {
    $this->connection = $connection;
    $this->menuLinkManager = $menu_link_manager;
  }

  /**
   * Set rediscover property and rebuild menu trees.
   *
   * @param \Drupal\multiversion\Event\MultiversionManagerEvent $event
   */
  public function onPostMigrateLinks(MultiversionManagerEvent $event) {
    if ($event
      ->getOp() === MultiversionManager::OP_DISABLE && ($entity_type = $event
      ->getEntityType('menu_link_content'))) {
      $data_table = $entity_type
        ->getDataTable();

      // Truncate 'menu_tree' table before rebuild.
      $this->connection
        ->truncate('menu_tree')
        ->execute();

      // Set a rediscover and rebuild menu_tree table.
      // @see \Drupal\menu_link_content\Plugin\Deriver\MenuLinkContentDeriver
      $this->connection
        ->update($data_table)
        ->fields([
        'rediscover' => 1,
      ])
        ->execute();
      $this->menuLinkManager
        ->rebuild();
    }
  }

  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents() {
    return [
      MultiversionManagerEvents::POST_MIGRATE => [
        'onPostMigrateLinks',
      ],
    ];
  }

}

Members

Namesort descending Modifiers Type Description Overrides
MenuLinkContentMigrateSubscriber::$connection protected property
MenuLinkContentMigrateSubscriber::$menuLinkManager protected property
MenuLinkContentMigrateSubscriber::getSubscribedEvents public static function Returns an array of event names this subscriber wants to listen to.
MenuLinkContentMigrateSubscriber::onPostMigrateLinks public function Set rediscover property and rebuild menu trees.
MenuLinkContentMigrateSubscriber::__construct public function Class constructor.