Version 1
[yaffs-website] / web / core / modules / views / src / Plugin / Derivative / ViewsEntityRow.php
1 <?php
2
3 namespace Drupal\views\Plugin\Derivative;
4
5 use Drupal\Core\Entity\EntityManagerInterface;
6 use Drupal\Core\Plugin\Discovery\ContainerDeriverInterface;
7 use Drupal\views\ViewsData;
8 use Symfony\Component\DependencyInjection\ContainerInterface;
9
10 /**
11  * Provides views row plugin definitions for all non-special entity types.
12  *
13  * @ingroup views_row_plugins
14  *
15  * @see \Drupal\views\Plugin\views\row\EntityRow
16  */
17 class ViewsEntityRow implements ContainerDeriverInterface {
18
19   /**
20    * Stores all entity row plugin information.
21    *
22    * @var array
23    */
24   protected $derivatives = [];
25
26   /**
27    * The base plugin ID that the derivative is for.
28    *
29    * @var string
30    */
31   protected $basePluginId;
32
33   /**
34    * The entity manager.
35    *
36    * @var \Drupal\Core\Entity\EntityManagerInterface
37    */
38   protected $entityManager;
39
40   /**
41    * The views data service.
42    *
43    * @var \Drupal\views\ViewsData
44    */
45   protected $viewsData;
46
47   /**
48    * Constructs a ViewsEntityRow object.
49    *
50    * @param string $base_plugin_id
51    *   The base plugin ID.
52    * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
53    *   The entity manager.
54    * @param \Drupal\views\ViewsData $views_data
55    *   The views data service.
56    */
57   public function __construct($base_plugin_id, EntityManagerInterface $entity_manager, ViewsData $views_data) {
58     $this->basePluginId = $base_plugin_id;
59     $this->entityManager = $entity_manager;
60     $this->viewsData = $views_data;
61   }
62
63   /**
64    * {@inheritdoc}
65    */
66   public static function create(ContainerInterface $container, $base_plugin_id) {
67     return new static(
68       $base_plugin_id,
69       $container->get('entity.manager'),
70       $container->get('views.views_data')
71     );
72   }
73
74   /**
75    * {@inheritdoc}
76    */
77   public function getDerivativeDefinition($derivative_id, $base_plugin_definition) {
78     if (!empty($this->derivatives) && !empty($this->derivatives[$derivative_id])) {
79       return $this->derivatives[$derivative_id];
80     }
81     $this->getDerivativeDefinitions($base_plugin_definition);
82     return $this->derivatives[$derivative_id];
83   }
84
85   /**
86    * {@inheritdoc}
87    */
88   public function getDerivativeDefinitions($base_plugin_definition) {
89     foreach ($this->entityManager->getDefinitions() as $entity_type_id => $entity_type) {
90       // Just add support for entity types which have a views integration.
91       if (($base_table = $entity_type->getBaseTable()) && $this->viewsData->get($base_table) && $this->entityManager->hasHandler($entity_type_id, 'view_builder')) {
92         $this->derivatives[$entity_type_id] = [
93           'id' => 'entity:' . $entity_type_id,
94           'provider' => 'views',
95           'title' => $entity_type->getLabel(),
96           'help' => t('Display the @label', ['@label' => $entity_type->getLabel()]),
97           'base' => [$entity_type->getDataTable() ?: $entity_type->getBaseTable()],
98           'entity_type' => $entity_type_id,
99           'display_types' => ['normal'],
100           'class' => $base_plugin_definition['class'],
101         ];
102       }
103     }
104
105     return $this->derivatives;
106   }
107
108 }