You are here

class MenuBlockKernelViewSubscriber in Menu Block 8

Alters the block library modal.

We can't use hook_ajax_render_alter() because the #markup is rendered before it is passed to that hook.

Hierarchy

Expanded class hierarchy of MenuBlockKernelViewSubscriber

1 string reference to 'MenuBlockKernelViewSubscriber'
menu_block.services.yml in ./menu_block.services.yml
menu_block.services.yml
1 service uses MenuBlockKernelViewSubscriber
menu_block.kernel_view_subscriber in ./menu_block.services.yml
Drupal\menu_block\EventSubscriber\MenuBlockKernelViewSubscriber

File

src/EventSubscriber/MenuBlockKernelViewSubscriber.php, line 16

Namespace

Drupal\menu_block\EventSubscriber
View source
class MenuBlockKernelViewSubscriber implements EventSubscriberInterface {

  /**
   * The current route match.
   *
   * @var \Drupal\Core\Routing\RouteMatchInterface
   */
  protected $currentRouteMatch;

  /**
   * Constructs a new MenuBlockKernelViewSubscriber.
   *
   * @param \Drupal\Core\Routing\RouteMatchInterface $current_route_match
   *   The current route match.
   */
  public function __construct(RouteMatchInterface $current_route_match) {
    $this->currentRouteMatch = $current_route_match;
  }

  /**
   * Alters the block library modal.
   *
   * @param \Symfony\Component\HttpKernel\Event\GetResponseEvent $event
   *   The event to process.
   */
  public function onView(GetResponseEvent $event) {
    switch ($this->currentRouteMatch
      ->getRouteName()) {
      case 'block.admin_library':
      case 'context.reaction.blocks.library':

        // Grab the render array result before it is rendered by the
        // main_content_view_subscriber.
        $result = $event
          ->getControllerResult();
        foreach ($result['blocks']['#rows'] as $key => $row) {

          // Remove rows for any block provided by the system_menu_block plugin.
          $routeParameters = $row['operations']['data']['#links']['add']['url']
            ->getRouteParameters();
          $plugin_id = !empty($routeParameters['plugin_id']) ? $routeParameters['plugin_id'] : $routeParameters['block_id'];
          if (strpos($plugin_id, 'system_menu_block:') === 0) {
            unset($result['blocks']['#rows'][$key]);
          }
        }

        // Override the original render array.
        $event
          ->setControllerResult($result);
        break;
    }
  }

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

    // Run before main_content_view_subscriber.
    $events[KernelEvents::VIEW][] = [
      'onView',
      1,
    ];
    return $events;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
MenuBlockKernelViewSubscriber::$currentRouteMatch protected property The current route match.
MenuBlockKernelViewSubscriber::getSubscribedEvents public static function Returns an array of event names this subscriber wants to listen to.
MenuBlockKernelViewSubscriber::onView public function Alters the block library modal.
MenuBlockKernelViewSubscriber::__construct public function Constructs a new MenuBlockKernelViewSubscriber.