51342d010c02271d8fa8b3221879c8774190e6c8
[yaffs-website] / web / modules / contrib / ctools / src / Plugin / Block / EntityView.php
1 <?php
2
3 namespace Drupal\ctools\Plugin\Block;
4
5 use Drupal\Core\Block\BlockBase;
6 use Drupal\Core\Cache\CacheableMetadata;
7 use Drupal\Core\Entity\EntityManagerInterface;
8 use Drupal\Core\Form\FormStateInterface;
9 use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
10 use Drupal\Core\Plugin\ContextAwarePluginInterface;
11 use Symfony\Component\DependencyInjection\ContainerInterface;
12
13 /**
14  * Provides a block to view a specific entity.
15  *
16  * @Block(
17  *   id = "entity_view",
18  *   deriver = "Drupal\ctools\Plugin\Deriver\EntityViewDeriver",
19  * )
20  */
21 class EntityView extends BlockBase implements ContextAwarePluginInterface, ContainerFactoryPluginInterface {
22
23   /**
24    * The entity manager.
25    *
26    * @var \Drupal\Core\Entity\EntityManagerInterface
27    */
28   protected $entityManager;
29
30   /**
31    * Constructs a new EntityView.
32    *
33    * @param array $configuration
34    *   A configuration array containing information about the plugin instance.
35    * @param string $plugin_id
36    *   The plugin ID for the plugin instance.
37    * @param mixed $plugin_definition
38    *   The plugin implementation definition.
39    * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
40    *   The entity manager.
41    */
42   public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityManagerInterface $entity_manager) {
43     parent::__construct($configuration, $plugin_id, $plugin_definition);
44
45     $this->entityManager = $entity_manager;
46   }
47
48   /**
49    * {@inheritdoc}
50    */
51   public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
52     return new static(
53       $configuration,
54       $plugin_id,
55       $plugin_definition,
56       $container->get('entity.manager')
57     );
58   }
59
60   /**
61    * {@inheritdoc}
62    */
63   public function defaultConfiguration() {
64     return [
65       'view_mode' => 'default',
66     ];
67   }
68
69   /**
70    * {@inheritdoc}
71    */
72   public function blockForm($form, FormStateInterface $form_state) {
73     $form['view_mode'] = [
74       '#type' => 'select',
75       '#options' => $this->entityManager->getViewModeOptions($this->getDerivativeId()),
76       '#title' => $this->t('View mode'),
77       '#default_value' => $this->configuration['view_mode'],
78     ];
79     return $form;
80   }
81
82   /**
83    * {@inheritdoc}
84    */
85   public function blockSubmit($form, FormStateInterface $form_state) {
86     $this->configuration['view_mode'] = $form_state->getValue('view_mode');
87   }
88
89   /**
90    * {@inheritdoc}
91    */
92   public function build() {
93     /** @var $entity \Drupal\Core\Entity\EntityInterface */
94     $entity = $this->getContextValue('entity');
95
96     $view_builder = $this->entityManager->getViewBuilder($entity->getEntityTypeId());
97     $build = $view_builder->view($entity, $this->configuration['view_mode']);
98
99     CacheableMetadata::createFromObject($this->getContext('entity'))
100       ->applyTo($build);
101
102     return $build;
103   }
104
105 }