Interim commit.
[yaffs-website] / web / modules / contrib / entity_embed / src / Plugin / EmbedType / Entity.php
1 <?php
2
3 namespace Drupal\entity_embed\Plugin\EmbedType;
4
5 use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
6 use Drupal\Core\Entity\EntityTypeInterface;
7 use Drupal\Core\Entity\EntityTypeManagerInterface;
8 use Drupal\Core\Entity\EntityTypeRepositoryInterface;
9 use Drupal\Core\Form\FormStateInterface;
10 use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
11 use Drupal\Core\Plugin\PluginDependencyTrait;
12 use Drupal\embed\EmbedType\EmbedTypeBase;
13 use Drupal\entity_embed\EntityEmbedDisplay\EntityEmbedDisplayManager;
14 use Symfony\Component\DependencyInjection\ContainerInterface;
15
16 /**
17  * Entity embed type.
18  *
19  * @EmbedType(
20  *   id = "entity",
21  *   label = @Translation("Entity")
22  * )
23  */
24 class Entity extends EmbedTypeBase implements ContainerFactoryPluginInterface {
25   use PluginDependencyTrait;
26
27   /**
28    * The entity type manager service.
29    *
30    * @var \Drupal\Core\Entity\EntityTypeManagerInterface
31    */
32   protected $entityTypeManager;
33
34   /**
35    * The entity type repository service.
36    *
37    * @var \Drupal\Core\Entity\EntityTypeRepositoryInterface
38    */
39   protected $entityTypeRepository;
40
41   /**
42    * The entity type bundle info service.
43    *
44    * @var \Drupal\Core\Entity\EntityTypeBundleInfoInterface
45    */
46   protected $entityTypeBundleInfo;
47
48   /**
49    * The Entity Embed Display plugin manager.
50    *
51    * @var \Drupal\entity_embed\EntityEmbedDisplay\EntityEmbedDisplayManager
52    */
53   protected $displayPluginManager;
54
55   /**
56    * {@inheritdoc}
57    *
58    * @param array $configuration
59    *   A configuration array containing information about the plugin instance.
60    * @param string $plugin_id
61    *   The plugin_id for the plugin instance.
62    * @param mixed $plugin_definition
63    *   The plugin implementation definition.
64    * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
65    *   The entity type manager service.
66    * @param \Drupal\Core\Entity\EntityTypeRepositoryInterface $entity_type_repository
67    *   The entity type repository service.
68    * @param \Drupal\Core\Entity\EntityTypeBundleInfoInterface $bundle_info
69    *   The entity type bundle info service.
70    * @param \Drupal\entity_embed\EntityEmbedDisplay\EntityEmbedDisplayManager $display_plugin_manager
71    *   The plugin manager.
72    */
73   public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entity_type_manager, EntityTypeRepositoryInterface $entity_type_repository, EntityTypeBundleInfoInterface $bundle_info, EntityEmbedDisplayManager $display_plugin_manager) {
74     parent::__construct($configuration, $plugin_id, $plugin_definition);
75     $this->entityTypeManager = $entity_type_manager;
76     $this->entityTypeRepository = $entity_type_repository;
77     $this->entityTypeBundleInfo = $bundle_info;
78     $this->displayPluginManager = $display_plugin_manager;
79   }
80
81   /**
82    * {@inheritdoc}
83    */
84   public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
85     return new static(
86       $configuration,
87       $plugin_id,
88       $plugin_definition,
89       $container->get('entity_type.manager'),
90       $container->get('entity_type.repository'),
91       $container->get('entity_type.bundle.info'),
92       $container->get('plugin.manager.entity_embed.display')
93     );
94   }
95
96   /**
97    * {@inheritdoc}
98    */
99   public function defaultConfiguration() {
100     return [
101       'entity_type' => 'node',
102       'bundles' => [],
103       'display_plugins' => [],
104       'entity_browser' => '',
105       'entity_browser_settings' => [],
106     ];
107   }
108
109   /**
110    * {@inheritdoc}
111    */
112   public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
113     $embed_button = $form_state->getTemporaryValue('embed_button');
114     $entity_type_id = $this->getConfigurationValue('entity_type');
115
116     $form['entity_type'] = array(
117       '#type' => 'select',
118       '#title' => $this->t('Entity type'),
119       '#options' => $this->getEntityTypeOptions(),
120       '#default_value' => $entity_type_id,
121       '#description' => $this->t("Entity type for which this button is to enabled."),
122       '#required' => TRUE,
123       '#ajax' => array(
124         'callback' => array($form_state->getFormObject(), 'updateTypeSettings'),
125         'effect' => 'fade',
126       ),
127       '#disabled' => !$embed_button->isNew(),
128     );
129
130     if ($entity_type_id) {
131       $entity_type = $this->entityTypeManager->getDefinition($entity_type_id);
132       $form['bundles'] = array(
133         '#type' => 'checkboxes',
134         '#title' => $entity_type->getBundleLabel() ?: $this->t('Bundles'),
135         '#options' => $this->getEntityBundleOptions($entity_type),
136         '#default_value' => $this->getConfigurationValue('bundles'),
137         '#description' => $this->t('If none are selected, all are allowed.'),
138       );
139       $form['bundles']['#access'] = !empty($form['bundles']['#options']);
140
141       // Allow option to limit Entity Embed Display plugins.
142       $form['display_plugins'] = array(
143         '#type' => 'checkboxes',
144         '#title' => $this->t('Allowed Entity Embed Display plugins'),
145         '#options' => $this->displayPluginManager->getDefinitionOptionsForEntityType($entity_type_id),
146         '#default_value' => $this->getConfigurationValue('display_plugins'),
147         '#description' => $this->t('If none are selected, all are allowed. Note that these are the plugins which are allowed for this entity type, all of these might not be available for the selected entity.'),
148       );
149       $form['display_plugins']['#access'] = !empty($form['display_plugins']['#options']);
150
151       /** @var \Drupal\entity_browser\EntityBrowserInterface[] $browsers */
152       if ($this->entityTypeManager->hasDefinition('entity_browser') && ($browsers = $this->entityTypeManager->getStorage('entity_browser')->loadMultiple())) {
153         $ids = array_keys($browsers);
154         $labels = array_map(
155           function ($item) {
156             /** @var \Drupal\entity_browser\EntityBrowserInterface $item */
157             return $item->label();
158           },
159           $browsers
160         );
161         $options = ['_none' => $this->t('None (autocomplete)')] + array_combine($ids, $labels);
162         $form['entity_browser'] = [
163           '#type' => 'select',
164           '#title' => $this->t('Entity browser'),
165           '#description' => $this->t('Entity browser to be used to select entities to be embedded.'),
166           '#options' => $options,
167           '#default_value' => $this->getConfigurationValue('entity_browser'),
168         ];
169         $form['entity_browser_settings'] = [
170           '#type' => 'details',
171           '#title' => $this->t('Entity browser settings'),
172           '#open' => TRUE,
173           '#states' => [
174             'invisible' => [
175               ':input[name="type_settings[entity_browser]"]' => ['value' => '_none'],
176             ],
177           ],
178         ];
179         $form['entity_browser_settings']['display_review'] = [
180           '#type' => 'checkbox',
181           '#title' => 'Display the entity after selection',
182           '#default_value' => $this->getConfigurationValue('entity_browser_settings')['display_review'],
183         ];
184       }
185       else {
186         $form['entity_browser'] = [
187           '#type' => 'value',
188           '#value' => '',
189         ];
190       }
191     }
192
193     return $form;
194   }
195
196   /**
197    * {@inheritdoc}
198    */
199   public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
200     // Filter down the bundles and allowed Entity Embed Display plugins.
201     $bundles = $form_state->getValue('bundles');
202     $form_state->setValue('bundles', array_keys(array_filter($bundles)));
203     $display_plugins = $form_state->getValue('display_plugins');
204     $form_state->setValue('display_plugins', array_keys(array_filter($display_plugins)));
205     $entity_browser = $form_state->getValue('entity_browser') == '_none' ? '' : $form_state->getValue('entity_browser');
206     $form_state->setValue('entity_browser', $entity_browser);
207     $form_state->setValue('entity_browser_settings', $form_state->getValue('entity_browser_settings'));
208
209     parent::submitConfigurationForm($form, $form_state);
210   }
211
212   /**
213    * Builds a list of entity type options.
214    *
215    * Configuration entity types without a view builder are filtered out while
216    * all other entity types are kept.
217    *
218    * @return array
219    *   An array of entity type labels, keyed by entity type name.
220    */
221   protected function getEntityTypeOptions() {
222     $options = $this->entityTypeRepository->getEntityTypeLabels(TRUE);
223
224     foreach ($options as $group => $group_types) {
225       foreach (array_keys($group_types) as $entity_type_id) {
226         // Filter out entity types that do not have a view builder class.
227         if (!$this->entityTypeManager->getDefinition($entity_type_id)->hasViewBuilderClass()) {
228           unset($options[$group][$entity_type_id]);
229         }
230         // Filter out entity types that do not support UUIDs.
231         if (!$this->entityTypeManager->getDefinition($entity_type_id)->hasKey('uuid')) {
232           unset($options[$group][$entity_type_id]);
233         }
234         // Filter out entity types that will not have any Entity Embed Display
235         // plugins.
236         if (!$this->displayPluginManager->getDefinitionOptionsForEntityType($entity_type_id)) {
237           unset($options[$group][$entity_type_id]);
238         }
239       }
240     }
241
242     return $options;
243   }
244
245   /**
246    * Builds a list of entity type bundle options.
247    *
248    * Configuration entity types without a view builder are filtered out while
249    * all other entity types are kept.
250    *
251    * @return array
252    *   An array of bundle labels, keyed by bundle name.
253    */
254   protected function getEntityBundleOptions(EntityTypeInterface $entity_type) {
255     $bundle_options = array();
256     // If the entity has bundles, allow option to restrict to bundle(s).
257     if ($entity_type->hasKey('bundle')) {
258       foreach ($this->entityTypeBundleInfo->getBundleInfo($entity_type->id()) as $bundle_id => $bundle_info) {
259         $bundle_options[$bundle_id] = $bundle_info['label'];
260       }
261       natsort($bundle_options);
262     }
263     return $bundle_options;
264   }
265
266   /**
267    * {@inheritdoc}
268    */
269   public function getDefaultIconUrl() {
270     return file_create_url(drupal_get_path('module', 'entity_embed') . '/js/plugins/drupalentity/entity.png');
271   }
272
273   /**
274    * {@inheritdoc}
275    */
276   public function calculateDependencies() {
277     $this->addDependencies(parent::calculateDependencies());
278
279     $entity_type_id = $this->getConfigurationValue('entity_type');
280     $entity_type = $this->entityTypeManager->getDefinition($entity_type_id);
281     $this->addDependency('module', $entity_type->getProvider());
282
283     // Calculate bundle dependencies.
284     foreach ($this->getConfigurationValue('bundles') as $bundle) {
285       $bundle_dependency = $entity_type->getBundleConfigDependency($bundle);
286       $this->addDependency($bundle_dependency['type'], $bundle_dependency['name']);
287     }
288
289     // Calculate display Entity Embed Display dependencies.
290     foreach ($this->getConfigurationValue('display_plugins') as $display_plugin) {
291       $instance = $this->displayPluginManager->createInstance($display_plugin);
292       $this->calculatePluginDependencies($instance);
293     }
294
295     return $this->dependencies;
296   }
297
298 }