BlockContent.php 1.88 KB
Newer Older
1 2 3 4
<?php

namespace Drupal\block_content\Plugin\Derivative;

5
use Drupal\Component\Plugin\Derivative\DeriverBase;
6 7 8
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Plugin\Discovery\ContainerDeriverInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
9 10 11 12

/**
 * Retrieves block plugin definitions for all custom blocks.
 */
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
class BlockContent extends DeriverBase implements ContainerDeriverInterface {

  /**
   * The custom block storage.
   *
   * @var \Drupal\Core\Entity\EntityStorageInterface
   */
  protected $blockContentStorage;

  /**
   * Constructs a BlockContent object.
   *
   * @param \Drupal\Core\Entity\EntityStorageInterface $block_content_storage
   *   The custom block storage.
   */
  public function __construct(EntityStorageInterface $block_content_storage) {
    $this->blockContentStorage = $block_content_storage;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, $base_plugin_id) {
36
    $entity_type_manager = $container->get('entity_type.manager');
37
    return new static(
38
      $entity_type_manager->getStorage('block_content')
39 40 41
    );
  }

42 43 44 45
  /**
   * {@inheritdoc}
   */
  public function getDerivativeDefinitions($base_plugin_definition) {
46
    $block_contents = $this->blockContentStorage->loadByProperties(['reusable' => TRUE]);
47 48
    // Reset the discovered definitions.
    $this->derivatives = [];
49
    /** @var $block_content \Drupal\block_content\Entity\BlockContent */
50 51 52
    foreach ($block_contents as $block_content) {
      $this->derivatives[$block_content->uuid()] = $base_plugin_definition;
      $this->derivatives[$block_content->uuid()]['admin_label'] = $block_content->label();
53
      $this->derivatives[$block_content->uuid()]['config_dependencies']['content'] = [
54
        $block_content->getConfigDependencyName(),
55
      ];
56 57 58
    }
    return parent::getDerivativeDefinitions($base_plugin_definition);
  }
59

60
}