You are here

abstract class EntityEmbedDisplayBase in Entity Embed 8

Defines a base Entity Embed Display implementation.

Hierarchy

Expanded class hierarchy of EntityEmbedDisplayBase

See also

\Drupal\entity_embed\Annotation\EntityEmbedDisplay

\Drupal\entity_embed\EntityEmbedDisplay\EntityEmbedDisplayInterface

\Drupal\entity_embed\EntityEmbedDisplay\EntityEmbedDisplayManager

Plugin API

File

src/EntityEmbedDisplay/EntityEmbedDisplayBase.php, line 26

Namespace

Drupal\entity_embed\EntityEmbedDisplay
View source
abstract class EntityEmbedDisplayBase extends PluginBase implements ContainerFactoryPluginInterface, EntityEmbedDisplayInterface {

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

  /**
   * The language manager.
   *
   * @var \Drupal\Core\Language\LanguageManagerInterface
   */
  protected $languageManager;

  /**
   * The context for the plugin.
   *
   * @var array
   */
  public $context = [];

  /**
   * The attributes on the embedded entity.
   *
   * @var array
   */
  public $attributes = [];

  /**
   * Constructs an EntityEmbedDisplayBase object.
   *
   * @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\Language\LanguageManagerInterface $language_manager
   *   The language manager.
   */
  public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entity_type_manager, LanguageManagerInterface $language_manager) {
    parent::__construct($configuration, $plugin_id, $plugin_definition);
    $this
      ->setConfiguration($configuration);
    $this->entityTypeManager = $entity_type_manager;
    $this->languageManager = $language_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('language_manager'));
  }

  /**
   * {@inheritdoc}
   */
  public function access(AccountInterface $account = NULL) {

    // @todo Add a hook_entity_embed_display_access()?
    // Check that the plugin's registered entity types matches the current
    // entity type.
    return AccessResult::allowedIf($this
      ->isValidEntityType())
      ->addCacheTags([
      'entity_types',
    ]);
  }

  /**
   * Validates that this display plugin applies to the current entity type.
   *
   * This checks the plugin annotation's 'entity_types' value, which should be
   * an array of entity types that this plugin can process, or FALSE if the
   * plugin applies to all entity types.
   *
   * @return bool
   *   TRUE if the plugin can display the current entity type, or FALSE
   *   otherwise.
   */
  protected function isValidEntityType() {

    // First, determine whether or not the entity type id is valid. Return FALSE
    // if the specified id is not valid.
    $entity_type = $this
      ->getEntityTypeFromContext();
    if (!$this->entityTypeManager
      ->getDefinition($entity_type)) {
      return FALSE;
    }
    $definition = $this
      ->getPluginDefinition();
    if ($definition['entity_types'] === FALSE) {
      return TRUE;
    }
    else {
      return in_array($entity_type, $definition['entity_types']);
    }
  }

  /**
   * {@inheritdoc}
   */
  public abstract function build();

  /**
   * {@inheritdoc}
   */
  public function calculateDependencies() {
    return [];
  }

  /**
   * {@inheritdoc}
   */
  public function defaultConfiguration() {
    return [];
  }

  /**
   * {@inheritdoc}
   */
  public function getConfiguration() {
    return $this->configuration;
  }

  /**
   * {@inheritdoc}
   */
  public function setConfiguration(array $configuration) {
    $this->configuration = NestedArray::mergeDeep($this
      ->defaultConfiguration(), $configuration);
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
    return $form;
  }

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

    // Do nothing.
  }

  /**
   * {@inheritdoc}
   */
  public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
    if (!$form_state
      ->getErrors()) {
      $this->configuration = array_intersect_key($form_state
        ->getValues(), $this
        ->defaultConfiguration());
    }
  }

  /**
   * Gets a configuration value.
   *
   * @param string $name
   *   The name of the plugin configuration value.
   * @param mixed $default
   *   The default value to return if the configuration value does not exist.
   *
   * @return mixed
   *   The currently set configuration value, or the value of $default if the
   *   configuration value is not set.
   */
  public function getConfigurationValue($name, $default = NULL) {
    $configuration = $this
      ->getConfiguration();
    return array_key_exists($name, $configuration) ? $configuration[$name] : $default;
  }

  /**
   * Sets the value for a defined context.
   *
   * @param string $name
   *   The name of the context in the plugin definition.
   * @param mixed $value
   *   The value to set the context to. The value has to validate against the
   *   provided context definition.
   */
  public function setContextValue($name, $value) {
    $this->context[$name] = $value;
  }

  /**
   * Gets the values for all defined contexts.
   *
   * @return array
   *   An array of set context values, keyed by context name.
   */
  public function getContextValues() {
    return $this->context;
  }

  /**
   * Gets the value for a defined context.
   *
   * @param string $name
   *   The name of the context in the plugin configuration.
   *
   * @return mixed
   *   The currently set context value.
   */
  public function getContextValue($name) {
    return !empty($this->context[$name]) ? $this->context[$name] : NULL;
  }

  /**
   * Returns whether or not value is set for a defined context.
   *
   * @param string $name
   *   The name of the context in the plugin configuration.
   *
   * @return bool
   *   True if context value exists, false otherwise.
   */
  public function hasContextValue($name) {
    return array_key_exists($name, $this->context);
  }

  /**
   * Gets the entity type from the current context.
   *
   * @return string
   *   The entity type id.
   */
  public function getEntityTypeFromContext() {
    if ($this
      ->hasContextValue('entity')) {
      return $this
        ->getContextValue('entity')
        ->getEntityTypeId();
    }
    else {
      return $this
        ->getContextValue('entity_type');
    }
  }

  /**
   * Gets the entity from the current context.
   *
   * @todo Where does this come from? The value must come from somewhere, yet
   * this does not implement any context-related interfaces. This is an *input*,
   * so we need cache contexts and possibly cache tags to reflect where this
   * came from. We need that for *everything* that this class does that relies
   * on this, plus any of its subclasses. Right now, this is effectively a
   * global that breaks cacheability metadata.
   *
   * @return \Drupal\Core\Entity\EntityInterface
   *   The entity from the current context.
   */
  public function getEntityFromContext() {
    if ($this
      ->hasContextValue('entity')) {
      return $this
        ->getContextValue('entity');
    }
  }

  /**
   * Sets the values for all attributes.
   *
   * @param array $attributes
   *   An array of attributes, keyed by attribute name.
   */
  public function setAttributes(array $attributes) {
    $this->attributes = $attributes;
  }

  /**
   * Gets the values for all attributes.
   *
   * @return array
   *   An array of set attribute values, keyed by attribute name.
   */
  public function getAttributeValues() {
    return $this->attributes;
  }

  /**
   * Gets the value for an attribute.
   *
   * @param string $name
   *   The name of the attribute.
   * @param mixed $default
   *   The default value to return if the attribute value does not exist.
   *
   * @return mixed
   *   The currently set attribute value.
   */
  public function getAttributeValue($name, $default = NULL) {
    $attributes = $this
      ->getAttributeValues();
    return array_key_exists($name, $attributes) ? $attributes[$name] : $default;
  }

  /**
   * Checks if an attribute is set.
   *
   * @param string $name
   *   The name of the attribute.
   *
   * @return bool
   *   Returns TRUE if value is set.
   */
  public function hasAttribute($name) {
    return array_key_exists($name, $this
      ->getAttributeValues());
  }

  /**
   * Gets the current language code.
   *
   * @return string
   *   The langcode present in the 'data-langcode', if present, or the current
   *   langcode from the language manager, otherwise.
   */
  public function getLangcode() {
    $langcode = $this
      ->getAttributeValue('data-langcode');
    if (empty($langcode)) {
      $langcode = $this->languageManager
        ->getCurrentLanguage(LanguageInterface::TYPE_CONTENT)
        ->getId();
    }
    return $langcode;
  }

}

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
EntityEmbedDisplayBase::$attributes public property The attributes on the embedded entity.
EntityEmbedDisplayBase::$context public property The context for the plugin.
EntityEmbedDisplayBase::$entityTypeManager protected property The entity type manager service.
EntityEmbedDisplayBase::$languageManager protected property The language manager.
EntityEmbedDisplayBase::access public function Indicates whether this Entity Embed display can be used. Overrides EntityEmbedDisplayInterface::access 1
EntityEmbedDisplayBase::build abstract public function Builds the renderable array for this Entity Embed display plugin. Overrides EntityEmbedDisplayInterface::build 1
EntityEmbedDisplayBase::buildConfigurationForm public function Form constructor. Overrides PluginFormInterface::buildConfigurationForm 1
EntityEmbedDisplayBase::calculateDependencies public function Calculates dependencies for the configured plugin. Overrides DependentPluginInterface::calculateDependencies 1
EntityEmbedDisplayBase::create public static function Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface::create 1
EntityEmbedDisplayBase::defaultConfiguration public function Gets default configuration for this plugin. Overrides ConfigurableInterface::defaultConfiguration 1
EntityEmbedDisplayBase::getAttributeValue public function Gets the value for an attribute.
EntityEmbedDisplayBase::getAttributeValues public function Gets the values for all attributes.
EntityEmbedDisplayBase::getConfiguration public function Gets this plugin's configuration. Overrides ConfigurableInterface::getConfiguration
EntityEmbedDisplayBase::getConfigurationValue public function Gets a configuration value.
EntityEmbedDisplayBase::getContextValue public function Gets the value for a defined context.
EntityEmbedDisplayBase::getContextValues public function Gets the values for all defined contexts.
EntityEmbedDisplayBase::getEntityFromContext public function Gets the entity from the current context.
EntityEmbedDisplayBase::getEntityTypeFromContext public function Gets the entity type from the current context.
EntityEmbedDisplayBase::getLangcode public function Gets the current language code.
EntityEmbedDisplayBase::hasAttribute public function Checks if an attribute is set.
EntityEmbedDisplayBase::hasContextValue public function Returns whether or not value is set for a defined context.
EntityEmbedDisplayBase::isValidEntityType protected function Validates that this display plugin applies to the current entity type.
EntityEmbedDisplayBase::setAttributes public function Sets the values for all attributes.
EntityEmbedDisplayBase::setConfiguration public function Sets the configuration for this plugin instance. Overrides ConfigurableInterface::setConfiguration
EntityEmbedDisplayBase::setContextValue public function Sets the value for a defined context.
EntityEmbedDisplayBase::submitConfigurationForm public function Form submission handler. Overrides PluginFormInterface::submitConfigurationForm 1
EntityEmbedDisplayBase::validateConfigurationForm public function Form validation handler. Overrides PluginFormInterface::validateConfigurationForm
EntityEmbedDisplayBase::__construct public function Constructs an EntityEmbedDisplayBase object. Overrides PluginBase::__construct 1
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.
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.