0

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

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',
    ];
  }


}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.