You are here

class Entity in Entity Embed 8

Entity embed type.

Plugin annotation


@EmbedType(
  id = "entity",
  label = @Translation("Entity")
)

Hierarchy

Expanded class hierarchy of Entity

File

src/Plugin/EmbedType/Entity.php, line 25

Namespace

Drupal\entity_embed\Plugin\EmbedType
View source
class Entity extends EmbedTypeBase implements ContainerFactoryPluginInterface {
  use PluginDependencyTrait;

  /**
   * The entity type manager service.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

  /**
   * The entity type repository service.
   *
   * @var \Drupal\Core\Entity\EntityTypeRepositoryInterface
   */
  protected $entityTypeRepository;

  /**
   * The entity type bundle info service.
   *
   * @var \Drupal\Core\Entity\EntityTypeBundleInfoInterface
   */
  protected $entityTypeBundleInfo;

  /**
   * The Entity Embed Display plugin manager.
   *
   * @var \Drupal\entity_embed\EntityEmbedDisplay\EntityEmbedDisplayManager
   */
  protected $displayPluginManager;

  /**
   * {@inheritdoc}
   *
   * @param array $configuration
   *   A configuration array containing information about the plugin instance.
   * @param string $plugin_id
   *   The plugin_id for the plugin instance.
   * @param mixed $plugin_definition
   *   The plugin implementation definition.
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager service.
   * @param \Drupal\Core\Entity\EntityTypeRepositoryInterface $entity_type_repository
   *   The entity type repository service.
   * @param \Drupal\Core\Entity\EntityTypeBundleInfoInterface $bundle_info
   *   The entity type bundle info service.
   * @param \Drupal\entity_embed\EntityEmbedDisplay\EntityEmbedDisplayManager $display_plugin_manager
   *   The plugin manager.
   */
  public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entity_type_manager, EntityTypeRepositoryInterface $entity_type_repository, EntityTypeBundleInfoInterface $bundle_info, EntityEmbedDisplayManager $display_plugin_manager) {
    parent::__construct($configuration, $plugin_id, $plugin_definition);
    $this->entityTypeManager = $entity_type_manager;
    $this->entityTypeRepository = $entity_type_repository;
    $this->entityTypeBundleInfo = $bundle_info;
    $this->displayPluginManager = $display_plugin_manager;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
    return new static($configuration, $plugin_id, $plugin_definition, $container
      ->get('entity_type.manager'), $container
      ->get('entity_type.repository'), $container
      ->get('entity_type.bundle.info'), $container
      ->get('plugin.manager.entity_embed.display'));
  }

  /**
   * {@inheritdoc}
   */
  public function defaultConfiguration() {
    return [
      'entity_type' => 'node',
      'bundles' => [],
      'display_plugins' => [],
      'entity_browser' => '',
      'entity_browser_settings' => [
        'display_review' => 0,
      ],
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
    $embed_button = $form_state
      ->getTemporaryValue('embed_button');
    $entity_type_id = $this
      ->getConfigurationValue('entity_type');
    $form['entity_type'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Entity type'),
      '#options' => $this
        ->getEntityTypeOptions(),
      '#default_value' => $entity_type_id,
      '#description' => $this
        ->t("The entity type this button will embed."),
      '#required' => TRUE,
      '#ajax' => [
        'callback' => [
          $form_state
            ->getFormObject(),
          'updateTypeSettings',
        ],
        'effect' => 'fade',
      ],
      '#disabled' => !$embed_button
        ->isNew(),
    ];
    if ($entity_type_id) {
      $entity_type = $this->entityTypeManager
        ->getDefinition($entity_type_id);
      $form['bundles'] = [
        '#type' => 'checkboxes',
        '#title' => $entity_type
          ->getBundleLabel() ?: $this
          ->t('Bundles'),
        '#options' => $this
          ->getEntityBundleOptions($entity_type),
        '#default_value' => $this
          ->getConfigurationValue('bundles'),
        '#description' => $this
          ->t('If none are selected, all are allowed.'),
      ];
      $form['bundles']['#access'] = !empty($form['bundles']['#options']);

      // Allow option to limit Entity Embed Display plugins.
      $form['display_plugins'] = [
        '#type' => 'checkboxes',
        '#title' => $this
          ->t('Allowed Entity Embed Display plugins'),
        '#options' => $this->displayPluginManager
          ->getDefinitionOptionsForEntityType($entity_type_id),
        '#default_value' => $this
          ->getConfigurationValue('display_plugins'),
        '#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.'),
      ];
      $form['display_plugins']['#access'] = !empty($form['display_plugins']['#options']);

      /** @var \Drupal\entity_browser\EntityBrowserInterface[] $browsers */
      if ($this->entityTypeManager
        ->hasDefinition('entity_browser') && ($browsers = $this->entityTypeManager
        ->getStorage('entity_browser')
        ->loadMultiple())) {

        // Filter out unsupported displays & return array of ids and labels.
        $browsers = array_map(function ($item) {

          /** @var \Drupal\entity_browser\EntityBrowserInterface $item */
          return $item
            ->label();
        }, array_filter($browsers, function (EntityBrowserInterface $browser) {
          return !in_array($browser
            ->getDisplay()
            ->getPluginId(), [
            'modal',
            'standalone',
          ], TRUE);
        }));
        $options = [
          '_none' => $this
            ->t('None (autocomplete)'),
        ] + $browsers;
        $form['entity_browser'] = [
          '#type' => 'select',
          '#title' => $this
            ->t('Entity browser'),
          '#description' => $this
            ->t('Entity browser to be used to select entities to be embedded. Only compatible browsers will be available to be chosen.'),
          '#options' => $options,
          '#default_value' => $this
            ->getConfigurationValue('entity_browser'),
        ];
        $form['entity_browser_settings'] = [
          '#type' => 'details',
          '#title' => $this
            ->t('Entity browser settings'),
          '#open' => TRUE,
          '#states' => [
            'invisible' => [
              ':input[name="type_settings[entity_browser]"]' => [
                'value' => '_none',
              ],
            ],
          ],
        ];
        $form['entity_browser_settings']['display_review'] = [
          '#type' => 'checkbox',
          '#title' => 'Display the entity after selection',
          '#default_value' => $this
            ->getConfigurationValue('entity_browser_settings')['display_review'],
        ];
      }
      else {
        $form['entity_browser'] = [
          '#type' => 'value',
          '#value' => '',
        ];
      }
    }
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {

    // Filter down the bundles and allowed Entity Embed Display plugins.
    $bundles = $form_state
      ->getValue('bundles');
    $form_state
      ->setValue('bundles', array_keys(array_filter($bundles)));
    $display_plugins = $form_state
      ->getValue('display_plugins');
    $form_state
      ->setValue('display_plugins', array_keys(array_filter($display_plugins)));
    $entity_browser = $form_state
      ->getValue('entity_browser') == '_none' ? '' : $form_state
      ->getValue('entity_browser');
    $form_state
      ->setValue('entity_browser', $entity_browser);
    $form_state
      ->setValue('entity_browser_settings', $form_state
      ->getValue('entity_browser_settings', []));
    parent::submitConfigurationForm($form, $form_state);
  }

  /**
   * Builds a list of entity type options.
   *
   * Configuration entity types without a view builder are filtered out while
   * all other entity types are kept.
   *
   * @return array
   *   An array of entity type labels, keyed by entity type name.
   */
  protected function getEntityTypeOptions() {
    $options = $this->entityTypeRepository
      ->getEntityTypeLabels(TRUE);
    foreach ($options as $group => $group_types) {
      foreach (array_keys($group_types) as $entity_type_id) {

        // Filter out entity types that do not have a view builder class.
        if (!$this->entityTypeManager
          ->getDefinition($entity_type_id)
          ->hasViewBuilderClass()) {
          unset($options[$group][$entity_type_id]);
        }
        elseif (!$this->entityTypeManager
          ->getDefinition($entity_type_id)
          ->hasKey('uuid')) {
          unset($options[$group][$entity_type_id]);
        }
        elseif (!$this->displayPluginManager
          ->getDefinitionOptionsForEntityType($entity_type_id)) {
          unset($options[$group][$entity_type_id]);
        }
      }
    }
    return $options;
  }

  /**
   * Builds a list of entity type bundle options.
   *
   * Configuration entity types without a view builder are filtered out while
   * all other entity types are kept.
   *
   * @return array
   *   An array of bundle labels, keyed by bundle name.
   */
  protected function getEntityBundleOptions(EntityTypeInterface $entity_type) {
    $bundle_options = [];

    // If the entity has bundles, allow option to restrict to bundle(s).
    if ($entity_type
      ->hasKey('bundle')) {
      foreach ($this->entityTypeBundleInfo
        ->getBundleInfo($entity_type
        ->id()) as $bundle_id => $bundle_info) {
        $bundle_options[$bundle_id] = $bundle_info['label'];
      }
      natsort($bundle_options);
    }
    return $bundle_options;
  }

  /**
   * {@inheritdoc}
   */
  public function getDefaultIconUrl() {
    return file_create_url(drupal_get_path('module', 'entity_embed') . '/js/plugins/drupalentity/entity.png');
  }

  /**
   * {@inheritdoc}
   */
  public function calculateDependencies() {
    $this
      ->addDependencies(parent::calculateDependencies());
    $entity_type_id = $this
      ->getConfigurationValue('entity_type');
    $entity_type = $this->entityTypeManager
      ->getDefinition($entity_type_id);
    $this
      ->addDependency('module', $entity_type
      ->getProvider());

    // Calculate bundle dependencies.
    foreach ($this
      ->getConfigurationValue('bundles') as $bundle) {
      $bundle_dependency = $entity_type
        ->getBundleConfigDependency($bundle);
      $this
        ->addDependency($bundle_dependency['type'], $bundle_dependency['name']);
    }

    // Calculate display Entity Embed Display dependencies.
    foreach ($this
      ->getConfigurationValue('display_plugins') as $display_plugin) {
      $instance = $this->displayPluginManager
        ->createInstance($display_plugin);
      $this
        ->calculatePluginDependencies($instance);
    }
    $entity_browser = $this
      ->getConfigurationValue('entity_browser');
    if ($entity_browser && $this->entityTypeManager
      ->hasDefinition('entity_browser')) {
      $browser = $this->entityTypeManager
        ->getStorage('entity_browser')
        ->load($entity_browser);
      if ($browser) {
        $this
          ->addDependency($browser
          ->getConfigDependencyKey(), $browser
          ->getConfigDependencyName());
      }
    }
    return $this->dependencies;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
DependencyTrait::$dependencies protected property The object's dependencies.
DependencyTrait::addDependencies protected function Adds multiple dependencies.
DependencyTrait::addDependency protected function Adds a dependency.
EmbedTypeBase::getConfiguration public function Gets this plugin's configuration. Overrides ConfigurableInterface::getConfiguration
EmbedTypeBase::getConfigurationValue public function Gets a configuration value. Overrides EmbedTypeInterface::getConfigurationValue
EmbedTypeBase::setConfiguration public function Sets the configuration for this plugin instance. Overrides ConfigurableInterface::setConfiguration
EmbedTypeBase::setConfigurationValue public function Sets a configuration value. Overrides EmbedTypeInterface::setConfigurationValue
EmbedTypeBase::validateConfigurationForm public function Form validation handler. Overrides PluginFormInterface::validateConfigurationForm 1
Entity::$displayPluginManager protected property The Entity Embed Display plugin manager.
Entity::$entityTypeBundleInfo protected property The entity type bundle info service.
Entity::$entityTypeManager protected property The entity type manager service.
Entity::$entityTypeRepository protected property The entity type repository service.
Entity::buildConfigurationForm public function Form constructor. Overrides EmbedTypeBase::buildConfigurationForm
Entity::calculateDependencies public function Calculates dependencies for the configured plugin. Overrides EmbedTypeBase::calculateDependencies
Entity::create public static function Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface::create
Entity::defaultConfiguration public function Gets default configuration for this plugin. Overrides EmbedTypeBase::defaultConfiguration
Entity::getDefaultIconUrl public function Gets the default icon URL for the embed type. Overrides EmbedTypeInterface::getDefaultIconUrl
Entity::getEntityBundleOptions protected function Builds a list of entity type bundle options.
Entity::getEntityTypeOptions protected function Builds a list of entity type options.
Entity::submitConfigurationForm public function Form submission handler. Overrides EmbedTypeBase::submitConfigurationForm
Entity::__construct public function Overrides EmbedTypeBase::__construct
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 3
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.
PluginDependencyTrait::calculatePluginDependencies protected function Calculates and adds dependencies of a specific plugin instance. 1
PluginDependencyTrait::getPluginDependencies protected function Calculates and returns dependencies of a specific plugin instance.
PluginDependencyTrait::moduleHandler protected function Wraps the module handler. 1
PluginDependencyTrait::themeHandler protected function Wraps the theme handler. 1
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.