Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / web / core / modules / block_content / src / Plugin / Derivative / BlockContent.php
1 <?php
2
3 namespace Drupal\block_content\Plugin\Derivative;
4
5 use Drupal\Component\Plugin\Derivative\DeriverBase;
6 use Drupal\Core\Entity\EntityStorageInterface;
7 use Drupal\Core\Plugin\Discovery\ContainerDeriverInterface;
8 use Symfony\Component\DependencyInjection\ContainerInterface;
9
10 /**
11  * Retrieves block plugin definitions for all custom blocks.
12  */
13 class BlockContent extends DeriverBase implements ContainerDeriverInterface {
14
15   /**
16    * The custom block storage.
17    *
18    * @var \Drupal\Core\Entity\EntityStorageInterface
19    */
20   protected $blockContentStorage;
21
22   /**
23    * Constructs a BlockContent object.
24    *
25    * @param \Drupal\Core\Entity\EntityStorageInterface $block_content_storage
26    *   The custom block storage.
27    */
28   public function __construct(EntityStorageInterface $block_content_storage) {
29     $this->blockContentStorage = $block_content_storage;
30   }
31
32   /**
33    * {@inheritdoc}
34    */
35   public static function create(ContainerInterface $container, $base_plugin_id) {
36     $entity_manager = $container->get('entity.manager');
37     return new static(
38       $entity_manager->getStorage('block_content')
39     );
40   }
41
42   /**
43    * {@inheritdoc}
44    */
45   public function getDerivativeDefinitions($base_plugin_definition) {
46     $block_contents = $this->blockContentStorage->loadByProperties(['reusable' => TRUE]);
47     // Reset the discovered definitions.
48     $this->derivatives = [];
49     /** @var $block_content \Drupal\block_content\Entity\BlockContent */
50     foreach ($block_contents as $block_content) {
51       $this->derivatives[$block_content->uuid()] = $base_plugin_definition;
52       $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     return parent::getDerivativeDefinitions($base_plugin_definition);
58   }
59
60 }