Hide a list of blocks from Panelizer in-place editor (IPE)

by vijaycs85   Last Updated August 13, 2019 20:07 PM

I have a list of blocks that shouldn't be added via panelizer IPE popup under manage content. Is there any way to remove them from the list? enter image description here

Tags : 8 blocks panels


Answers 1


One of the options is to extend the IPE controller to remove the unwanted blocks from display. Here are the code changes:

  1. Route subscriber:
  mymodule.route_subscriber:
    class: Drupal\mymodule\Routing\RouteSubscriber
    tags:
      - { name: 'event_subscriber' }

  1. Update the IPE controller:
<?php

namespace Drupal\mymodule\Routing;

use Drupal\Core\Routing\RouteSubscriberBase;
use Drupal\mymodule\Controller\MymoduleIPEPageController;
use Symfony\Component\Routing\RouteCollection;

/**
 * Subscriber to update IPE controller.
 */
class RouteSubscriber extends RouteSubscriberBase {

  /**
   * {@inheritdoc}
   */
  protected function alterRoutes(RouteCollection $collection) {
    /* \Symfony\Component\Routing\Route $route */
    if ($route = $collection->get('panels_ipe.block_content_types')) {
      $route->setDefault('_controller', MymoduleIPEPageController::class . '::getBlockContentTypes');
    }
  }

}

  1. Controller:
<?php

namespace Drupal\mymodule\Controller;

use Drupal\panels_ipe\Controller\PanelsIPEPageController;

/**
 * Extends IPE controller for site specific customization.
 */
class MymoduleIPEPageController extends PanelsIPEPageController {

 /**
   * {@inheritdoc}
   */
  public function getBlockContentTypesData($panels_storage_type, $panels_storage_id) {
    // Assemble our relevant data.
    $types = $this->entityTypeManager()
      ->getStorage('block_content_type')
      ->loadMultiple();
    $data = [];
    $blocks_blacklist = $this->getHiddenBlockList();
    /** @var \Drupal\block_content\BlockContentTypeInterface $definition */
    foreach ($types as $id => $definition) {
      if (in_array($id, $blocks_blacklist)) {
        continue;
      }
      $data[] = [
        'id' => $definition->id(),
        'revision' => $definition->shouldCreateNewRevision(),
        'label' => $definition->label(),
        'description' => $definition->getDescription(),
      ];
    }

    return $data;
  }

 /**
   * List of blocks not available to add via PIE.
   *
   * @return array
   *   An array of block IDs.
   */
  protected function getHiddenBlockList() {
    // Remove UUID and title fields from the list.
    return [
      'entity_field:node:uuid',
      'entity_field:node:title',
    ];
  }


}

vijaycs85
vijaycs85
August 13, 2019 19:05 PM

Related Questions


Block not showing up in Custom Blocks for Panel

Updated June 10, 2015 02:03 AM


Can I make a "type" available as a block?

Updated March 27, 2019 12:07 PM

Custom block panelizer settings

Updated April 19, 2017 15:07 PM

Layout per term?

Updated June 04, 2017 01:07 AM