You are here

class BlockListOverride in Block List Override 1.0.x

Service to remove blocks from a list.

Hierarchy

Expanded class hierarchy of BlockListOverride

2 files declare their use of BlockListOverride
DefaultController.php in src/Controller/DefaultController.php
PluginAlter.php in src/PluginAlter.php
1 string reference to 'BlockListOverride'
block_list_override.services.yml in ./block_list_override.services.yml
block_list_override.services.yml
1 service uses BlockListOverride
block_list_override.list in ./block_list_override.services.yml
Drupal\block_list_override\BlockListOverride

File

src/BlockListOverride.php, line 11

Namespace

Drupal\block_list_override
View source
class BlockListOverride extends ServiceProviderBase {

  /**
   * The Entity Type Manager service.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

  /**
   * Block Blacklist match settings.
   *
   * @var string
   */
  protected $match;

  /**
   * Block Blacklist prefix settings.
   *
   * @var string
   */
  protected $prefix;

  /**
   * Block Blacklist regex settings.
   *
   * @var string
   */
  protected $regex;

  /**
   * Block Blacklist validity.
   *
   * @var bool
   */
  protected $hasSettings;

  /**
   * Block List Override negated.
   *
   * @var int
   */
  protected $isNegated;

  /**
   * The constructor.
   *
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The Entity Type Manager service.
   */
  public function __construct(EntityTypeManagerInterface $entity_type_manager) {
    $this->entityTypeManager = $entity_type_manager;
  }

  /**
   * Configure variables based on configuration settings.
   */
  public function setUp($settings) {
    $this->match = !empty($settings) ? trim($settings['match']) : '';
    $this->prefix = !empty($settings) ? trim($settings['prefix']) : '';
    $this->regex = !empty($settings) ? trim($settings['regex']) : '';
    $this->hasSettings = !empty($this->match . $this->prefix . $this->regex);
    $this->isNegated = !empty($settings['negate']);
  }

  /**
   * Determine if there are any settings to adjust for.
   */
  public function hasSettings() {
    return $this->hasSettings;
  }

  /**
   * Determines whether the given block plugin is allowed or not.
   *
   * @param string $plugin_id
   *   The block plugin ID to check.
   *
   * @return bool
   *   TRUE if the block is allowed, FALSE otherwise.
   */
  public function blockIsAllowed($plugin_id) {
    static $block_is_listed, $blocks_in_use = [];
    if (!$this->hasSettings) {
      return TRUE;
    }

    // See which blocks are in use in Layout builder. We must not remove any
    // Layout Builder blocks that are already in use.
    if (!isset($block_is_listed)) {
      foreach ($this->entityTypeManager
        ->getStorage('entity_view_display')
        ->loadMultiple() as $entity_view_display) {

        // Look for block plugins in each Layout Builder section.

        /** @var \Drupal\layout_builder\Section $section */
        foreach ($entity_view_display
          ->getThirdPartySetting('layout_builder', 'sections', []) as $section) {

          /** @var \Drupal\layout_builder\SectionComponent $component */
          foreach ($section
            ->getComponents() as $component) {
            $blocks_in_use[] = $component
              ->getPluginId();
          }
        }
      }

      // Define a helper function.
      $block_is_listed = function ($plugin_id) {
        $list = explode("\n", $this->prefix);
        foreach ($list as $prefix) {
          $prefix = trim($prefix);
          if (!empty($prefix) && strpos($plugin_id, "{$prefix}:") === 0) {
            return TRUE;
          }
        }
        $list = explode("\n", $this->match);
        foreach ($list as $match) {
          $match = trim($match);
          if (!empty($match) && $plugin_id == $match) {
            return TRUE;
          }
        }
        $list = explode("\n", $this->regex);
        foreach ($list as $regex) {
          $regex = trim($regex);
          if (!empty($regex) && preg_match($regex, $plugin_id, $parts)) {
            return TRUE;
          }
        }
      };
    }

    // (Dis)allow the block if it is not in the list, or if it's already in use.
    if ($this->isNegated) {
      return in_array($plugin_id, $blocks_in_use) || $block_is_listed($plugin_id);
    }
    return in_array($plugin_id, $blocks_in_use) || !$block_is_listed($plugin_id);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
BlockListOverride::$entityTypeManager protected property The Entity Type Manager service.
BlockListOverride::$hasSettings protected property Block Blacklist validity.
BlockListOverride::$isNegated protected property Block List Override negated.
BlockListOverride::$match protected property Block Blacklist match settings.
BlockListOverride::$prefix protected property Block Blacklist prefix settings.
BlockListOverride::$regex protected property Block Blacklist regex settings.
BlockListOverride::blockIsAllowed public function Determines whether the given block plugin is allowed or not.
BlockListOverride::hasSettings public function Determine if there are any settings to adjust for.
BlockListOverride::setUp public function Configure variables based on configuration settings.
BlockListOverride::__construct public function The constructor.
ServiceProviderBase::alter public function Modifies existing service definitions. Overrides ServiceModifierInterface::alter 5
ServiceProviderBase::register public function Registers services to the container. Overrides ServiceProviderInterface::register 1