3 namespace Drupal\entity_embed\Plugin\EmbedType;
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;
21 * label = @Translation("Entity")
24 class Entity extends EmbedTypeBase implements ContainerFactoryPluginInterface {
25 use PluginDependencyTrait;
28 * The entity type manager service.
30 * @var \Drupal\Core\Entity\EntityTypeManagerInterface
32 protected $entityTypeManager;
35 * The entity type repository service.
37 * @var \Drupal\Core\Entity\EntityTypeRepositoryInterface
39 protected $entityTypeRepository;
42 * The entity type bundle info service.
44 * @var \Drupal\Core\Entity\EntityTypeBundleInfoInterface
46 protected $entityTypeBundleInfo;
49 * The Entity Embed Display plugin manager.
51 * @var \Drupal\entity_embed\EntityEmbedDisplay\EntityEmbedDisplayManager
53 protected $displayPluginManager;
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
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;
84 public static function create(ContainerInterface $container, array $configuration, $plugin_id, $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')
99 public function defaultConfiguration() {
101 'entity_type' => 'node',
103 'display_plugins' => [],
104 'entity_browser' => '',
105 'entity_browser_settings' => [],
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');
116 $form['entity_type'] = array(
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."),
124 'callback' => array($form_state->getFormObject(), 'updateTypeSettings'),
127 '#disabled' => !$embed_button->isNew(),
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.'),
139 $form['bundles']['#access'] = !empty($form['bundles']['#options']);
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.'),
149 $form['display_plugins']['#access'] = !empty($form['display_plugins']['#options']);
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);
156 /** @var \Drupal\entity_browser\EntityBrowserInterface $item */
157 return $item->label();
161 $options = ['_none' => $this->t('None (autocomplete)')] + array_combine($ids, $labels);
162 $form['entity_browser'] = [
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'),
169 $form['entity_browser_settings'] = [
170 '#type' => 'details',
171 '#title' => $this->t('Entity browser settings'),
175 ':input[name="type_settings[entity_browser]"]' => ['value' => '_none'],
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'],
186 $form['entity_browser'] = [
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'));
209 parent::submitConfigurationForm($form, $form_state);
213 * Builds a list of entity type options.
215 * Configuration entity types without a view builder are filtered out while
216 * all other entity types are kept.
219 * An array of entity type labels, keyed by entity type name.
221 protected function getEntityTypeOptions() {
222 $options = $this->entityTypeRepository->getEntityTypeLabels(TRUE);
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]);
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]);
234 // Filter out entity types that will not have any Entity Embed Display
236 if (!$this->displayPluginManager->getDefinitionOptionsForEntityType($entity_type_id)) {
237 unset($options[$group][$entity_type_id]);
246 * Builds a list of entity type bundle options.
248 * Configuration entity types without a view builder are filtered out while
249 * all other entity types are kept.
252 * An array of bundle labels, keyed by bundle name.
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'];
261 natsort($bundle_options);
263 return $bundle_options;
269 public function getDefaultIconUrl() {
270 return file_create_url(drupal_get_path('module', 'entity_embed') . '/js/plugins/drupalentity/entity.png');
276 public function calculateDependencies() {
277 $this->addDependencies(parent::calculateDependencies());
279 $entity_type_id = $this->getConfigurationValue('entity_type');
280 $entity_type = $this->entityTypeManager->getDefinition($entity_type_id);
281 $this->addDependency('module', $entity_type->getProvider());
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']);
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);
295 return $this->dependencies;