public function Blacklist::blockIsAllowed in Block Blacklist [Deprecated] 8
Determines whether the given block plugin is allowed or blacklisted.
Parameters
string $plugin_id: The block plugin ID to check.
Return value
bool TRUE if the block is allowed, FALSE otherwise.
File
- src/
Blacklist.php, line 84  
Class
- Blacklist
 - Service to remove blocks from a list.
 
Namespace
Drupal\block_blacklistCode
public function blockIsAllowed($plugin_id) {
  static $block_is_blacklisted, $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_blacklisted)) {
    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_blacklisted = 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;
        }
      }
    };
  }
  // Allow the block if it is not in the blacklist, or if it's already in use.
  return in_array($plugin_id, $blocks_in_use) || !$block_is_blacklisted($plugin_id);
}