You are here

class PdbBlockDeriver in Decoupled Blocks 8

Provides a deriver for pdb blocks.

Hierarchy

Expanded class hierarchy of PdbBlockDeriver

3 files declare their use of PdbBlockDeriver
Ng2BlockDeriver.php in modules/pdb_ng2/src/Plugin/Derivative/Ng2BlockDeriver.php
PdbBlockDeriverTest.php in tests/src/Unit/Plugin/Derivative/PdbBlockDeriverTest.php
ReactBlockDeriver.php in modules/pdb_react/src/Plugin/Derivative/ReactBlockDeriver.php

File

src/Plugin/Derivative/PdbBlockDeriver.php, line 15

Namespace

Drupal\pdb\Plugin\Derivative
View source
class PdbBlockDeriver extends DeriverBase implements ContainerDeriverInterface {

  /**
   * The component discovery service.
   *
   * @var \Drupal\pdb\ComponentDiscoveryInterface
   */
  protected $componentDiscovery;

  /**
   * PdbBlockDeriver constructor.
   *
   * @param \Drupal\pdb\ComponentDiscoveryInterface $component_discovery
   *   The component discovery service.
   */
  public function __construct(ComponentDiscoveryInterface $component_discovery) {
    $this->componentDiscovery = $component_discovery;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, $base_plugin_id) {
    return new static($container
      ->get('pdb.component_discovery'));
  }

  /**
   * {@inheritdoc}
   */
  public function getDerivativeDefinitions($base_plugin_definition) {

    // Get all custom blocks which should be rediscovered.
    $components = $this->componentDiscovery
      ->getComponents();
    foreach ($components as $block_id => $block_info) {
      $this->derivatives[$block_id] = $base_plugin_definition;
      $this->derivatives[$block_id]['info'] = $block_info->info;
      $this->derivatives[$block_id]['admin_label'] = $block_info->info['name'];
      $this->derivatives[$block_id]['cache'] = [
        'max-age' => 0,
      ];

      // Only set category if available. Defaults to provider.
      if (isset($block_info->info['category'])) {
        $this->derivatives[$block_id]['category'] = $block_info->info['category'];
      }
      if (isset($block_info->info['contexts'])) {
        $this->derivatives[$block_id]['context_definitions'] = $this
          ->createContexts($block_info->info['contexts']);
      }
    }
    return $this->derivatives;
  }

  /**
   * Creates the context definitions required by a block plugin.
   *
   * @param array $contexts
   *   Contexts as defined in component label.
   *
   * @return \Drupal\Core\Plugin\Context\ContextDefinition[]
   *   Array of context to be used by block module.
   */
  protected function createContexts(array $contexts) {
    $contexts_definitions = [];

    // Support for old node entity context defintion.
    // "entity: node" must now be defined as "entity: entity:node".
    if (isset($contexts['entity']) && $contexts['entity'] === 'node') {
      $contexts['entity'] = 'entity:node';
    }
    foreach ($contexts as $context_id => $context_type) {
      if (strpos($context_type, 'entity:') === 0) {

        // Use dedicated entity context class for entity contexts.
        $contexts_definitions[$context_id] = new EntityContextDefinition($context_type);
      }
      else {

        // Otherwise, use common context class.
        $contexts_definitions[$context_id] = new ContextDefinition($context_type);
      }
    }
    return $contexts_definitions;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DeriverBase::$derivatives protected property List of derivative definitions. 1
DeriverBase::getDerivativeDefinition public function Gets the definition of a derivative plugin. Overrides DeriverInterface::getDerivativeDefinition
PdbBlockDeriver::$componentDiscovery protected property The component discovery service.
PdbBlockDeriver::create public static function Creates a new class instance. Overrides ContainerDeriverInterface::create
PdbBlockDeriver::createContexts protected function Creates the context definitions required by a block plugin.
PdbBlockDeriver::getDerivativeDefinitions public function Gets the definition of all derivatives of a base plugin. Overrides DeriverBase::getDerivativeDefinitions 2
PdbBlockDeriver::__construct public function PdbBlockDeriver constructor.