You are here

class EntityBlock in Entity Blocks 8

Same name in this branch
  1. 8 src/Plugin/Derivative/EntityBlock.php \Drupal\entity_block\Plugin\Derivative\EntityBlock
  2. 8 src/Plugin/Block/EntityBlock.php \Drupal\entity_block\Plugin\Block\EntityBlock

Provides block plugin definitions for all Entity Block block displays.

Hierarchy

Expanded class hierarchy of EntityBlock

See also

\Drupal\entity_block\Plugin\block\block\EntityBlock

File

src/Plugin/Derivative/EntityBlock.php, line 14

Namespace

Drupal\entity_block\Plugin\Derivative
View source
class EntityBlock implements ContainerDeriverInterface {

  /**
   * List of derivative definitions.
   *
   * @var array
   */
  protected $derivatives = [];

  /**
   * The base plugin ID.
   *
   * @var string
   */
  protected $basePluginId;

  /**
   * The entity type manager.
   *
   * @var EntityTypeManagerInterface
   */
  protected $entityTypeManager;

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

  /**
   * Constructs a EntityBlock object.
   *
   * @param string $base_plugin_id
   *   The base plugin ID.
   * @param EntityTypeManagerInterface $entityTypeManager
   *   The entity type manager.
   */
  public function __construct($base_plugin_id, EntityTypeManagerInterface $entityTypeManager) {
    $this->basePluginId = $base_plugin_id;
    $this->entityTypeManager = $entityTypeManager;
  }

  /**
   * {@inheritdoc}
   */
  public function getDerivativeDefinition($derivative_id, $base_plugin_definition) {
    if (!empty($this->derivatives) && !empty($this->derivatives[$derivative_id])) {
      return $this->derivatives[$derivative_id];
    }
    $this
      ->getDerivativeDefinitions($base_plugin_definition);
    return $this->derivatives[$derivative_id];
  }

  /**
   * {@inheritdoc}
   */
  public function getDerivativeDefinitions($base_plugin_definition) {
    foreach ($this->entityTypeManager
      ->getDefinitions() as $entityDefinition) {
      if ($entityDefinition
        ->hasViewBuilderClass()) {
        $delta = $entityDefinition
          ->id();
        $this->derivatives[$delta] = [
          'category' => 'Entity Block',
          'admin_label' => $entityDefinition
            ->getLabel(),
          'config_dependencies' => [
            'config' => [],
          ],
        ];
        $this->derivatives[$delta] += $base_plugin_definition;
      }
    }
    return $this->derivatives;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
EntityBlock::$basePluginId protected property The base plugin ID.
EntityBlock::$derivatives protected property List of derivative definitions.
EntityBlock::$entityTypeManager protected property The entity type manager.
EntityBlock::create public static function Creates a new class instance. Overrides ContainerDeriverInterface::create
EntityBlock::getDerivativeDefinition public function Gets the definition of a derivative plugin. Overrides DeriverInterface::getDerivativeDefinition
EntityBlock::getDerivativeDefinitions public function Gets the definition of all derivatives of a base plugin. Overrides DeriverInterface::getDerivativeDefinitions
EntityBlock::__construct public function Constructs a EntityBlock object.