abstract class EntityDisplayBase

Base class for config entity types that store configuration for entity forms and displays.

Hierarchy

Expanded class hierarchy of EntityDisplayBase

2 files declare their use of EntityDisplayBase
EntityDisplay.php in drupal/core/modules/entity/lib/Drupal/entity/Plugin/Core/Entity/EntityDisplay.php
Contains \Drupal\entity\Plugin\Core\Entity\EntityDisplay.
EntityFormDisplay.php in drupal/core/modules/entity/lib/Drupal/entity/Plugin/Core/Entity/EntityFormDisplay.php
Contains \Drupal\entity\Plugin\Core\Entity\EntityFormDisplay.

File

drupal/core/modules/entity/lib/Drupal/entity/EntityDisplayBase.php, line 16
Contains \Drupal\entity\EntityDisplayBase.

Namespace

Drupal\entity
View source
abstract class EntityDisplayBase extends ConfigEntityBase implements EntityDisplayBaseInterface {

  /**
   * Unique ID for the config entity.
   *
   * @var string
   */
  public $id;

  /**
   * Unique UUID for the config entity.
   *
   * @var string
   */
  public $uuid;

  /**
   * Entity type to be displayed.
   *
   * @var string
   */
  public $targetEntityType;

  /**
   * Bundle to be displayed.
   *
   * @var string
   */
  public $bundle;

  /**
   * View or form mode to be displayed.
   *
   * @var string
   */
  public $mode;

  /**
   * List of component display options, keyed by component name.
   *
   * @var array
   */
  protected $content = array();

  /**
   * The original view or form mode that was requested (case of view/form modes
   * being configured to fall back to the 'default' display).
   *
   * @var string
   */
  public $originalMode;

  /**
   * The plugin objects used for this display, keyed by field name.
   *
   * @var array
   */
  protected $plugins = array();

  /**
   * Context in which this entity will be used (e.g. 'display', 'form').
   *
   * @var string
   */
  protected $displayContext;

  /**
   * The plugin manager used by this entity type.
   *
   * @var \Drupal\Component\Plugin\PluginManagerBase
   */
  protected $pluginManager;

  /**
   * {@inheritdoc}
   */
  public function __construct(array $values, $entity_type) {

    // @todo See http://drupal.org/node/1825044#comment-6847792: contact.module
    // currently produces invalid entities with a NULL bundle in some cases.
    // Add the validity checks back when http://drupal.org/node/1856556 is
    // fixed.
    // if (!isset($values['targetEntityType']) || !isset($values['bundle']) || !isset($values['mode'])) {
    //   throw new \InvalidArgumentException('Missing required properties for an EntityDisplay entity.');
    // }
    // A plugin manager and a context type needs to be set by extending classes.
    if (!isset($this->pluginManager)) {
      throw new \RuntimeException('Missing plugin manager.');
    }
    if (!isset($this->displayContext)) {
      throw new \RuntimeException('Missing display context type.');
    }
    parent::__construct($values, $entity_type);
    $this->originalMode = $this->mode;
  }

  /**
   * {@inheritdoc}
   */
  public function id() {
    return $this->targetEntityType . '.' . $this->bundle . '.' . $this->mode;
  }

  /**
   * {@inheritdoc}
   */
  public function save() {

    // Build an ID if none is set.
    if (empty($this->id)) {
      $this->id = $this
        ->id();
    }
    return parent::save();
  }

  /**
   * {@inheritdoc}
   */
  public function getExportProperties() {
    $names = array(
      'id',
      'uuid',
      'targetEntityType',
      'bundle',
      'mode',
      'content',
    );
    $properties = array();
    foreach ($names as $name) {
      $properties[$name] = $this
        ->get($name);
    }
    return $properties;
  }

  /**
   * {@inheritdoc}
   */
  public function createCopy($mode) {
    $display = $this
      ->createDuplicate();
    $display->mode = $display->originalMode = $mode;
    return $display;
  }

  /**
   * {@inheritdoc}
   */
  public function getComponents() {
    $result = array();
    foreach ($this->content as $name => $options) {
      if (!isset($options['visible']) || $options['visible'] === TRUE) {
        unset($options['visible']);
        $result[$name] = $options;
      }
    }
    return $result;
  }

  /**
   * {@inheritdoc}
   */
  public function getComponent($name) {

    // We always store 'extra fields', whether they are visible or hidden.
    $extra_fields = field_info_extra_fields($this->targetEntityType, $this->bundle, $this->displayContext);
    if (isset($extra_fields[$name])) {

      // If we have explicit settings, return an array or NULL depending on
      // visibility.
      if (isset($this->content[$name])) {
        if ($this->content[$name]['visible']) {
          return array(
            'weight' => $this->content[$name]['weight'],
          );
        }
        else {
          return NULL;
        }
      }

      // If no explicit settings for the extra field, look at the default
      // visibility in its definition.
      $definition = $extra_fields[$name];
      if (!isset($definition['visible']) || $definition['visible'] == TRUE) {
        return array(
          'weight' => $definition['weight'],
        );
      }
      else {
        return NULL;
      }
    }
    if (isset($this->content[$name])) {
      return $this->content[$name];
    }
  }

  /**
   * {@inheritdoc}
   */
  public function setComponent($name, array $options = array()) {

    // If no weight specified, make sure the field sinks at the bottom.
    if (!isset($options['weight'])) {
      $max = $this
        ->getHighestWeight();
      $options['weight'] = isset($max) ? $max + 1 : 0;
    }
    if ($instance = field_info_instance($this->targetEntityType, $name, $this->bundle)) {
      $field = field_info_field($instance['field_name']);
      $options = $this->pluginManager
        ->prepareConfiguration($field['type'], $options);

      // Clear the persisted plugin, if any.
      unset($this->plugins[$name]);
    }

    // We always store 'extra fields', whether they are visible or hidden.
    $extra_fields = field_info_extra_fields($this->targetEntityType, $this->bundle, $this->displayContext);
    if (isset($extra_fields[$name])) {
      $options['visible'] = TRUE;
    }
    $this->content[$name] = $options;
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function removeComponent($name) {
    $extra_fields = field_info_extra_fields($this->targetEntityType, $this->bundle, $this->displayContext);
    if (isset($extra_fields[$name])) {

      // 'Extra fields' are exposed in hooks and can appear at any given time.
      // Therefore we store extra fields that are explicitly being hidden, so
      // that we can differenciate with those that are simply not configured
      // yet.
      $this->content[$name] = array(
        'visible' => FALSE,
      );
    }
    else {
      unset($this->content[$name]);
      unset($this->plugins[$name]);
    }
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function getHighestWeight() {
    $weights = array();

    // Collect weights for the components in the display.
    foreach ($this->content as $options) {
      if (isset($options['weight'])) {
        $weights[] = $options['weight'];
      }
    }

    // Let other modules feedback about their own additions.
    $weights = array_merge($weights, module_invoke_all('field_info_max_weight', $this->targetEntityType, $this->bundle, $this->displayContext, $this->mode));
    return $weights ? max($weights) : NULL;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ConfigEntityBase::$originalID protected property The original ID of the configuration entity.
ConfigEntityBase::$status public property The enabled/disabled status of the configuration entity. 1
ConfigEntityBase::createDuplicate public function Overrides Entity::createDuplicate(). Overrides Entity::createDuplicate 2
ConfigEntityBase::disable public function Implements \Drupal\Core\Config\Entity\ConfigEntityInterface::disable(). Overrides ConfigEntityInterface::disable 1
ConfigEntityBase::enable public function Implements \Drupal\Core\Config\Entity\ConfigEntityInterface::enable(). Overrides ConfigEntityInterface::enable
ConfigEntityBase::get public function Overrides Entity::get(). Overrides Entity::get 2
ConfigEntityBase::getOriginalID public function Implements ConfigEntityInterface::getOriginalID(). Overrides ConfigEntityInterface::getOriginalID
ConfigEntityBase::isNew public function Overrides Entity::isNew(). Overrides Entity::isNew
ConfigEntityBase::set public function Overrides Entity::set(). Overrides Entity::set
ConfigEntityBase::setOriginalID public function Implements ConfigEntityInterface::setOriginalID(). Overrides ConfigEntityInterface::setOriginalID
ConfigEntityBase::setStatus public function Implements \Drupal\Core\Config\Entity\ConfigEntityInterface::setStatus(). Overrides ConfigEntityInterface::setStatus
ConfigEntityBase::sort public static function Helper callback for uasort() to sort configuration entities by weight and label. 2
ConfigEntityBase::status public function Implements \Drupal\Core\Config\Entity\ConfigEntityInterface::status(). Overrides ConfigEntityInterface::status 1
Entity::$enforceIsNew protected property Boolean indicating whether the entity should be forced to be new.
Entity::$entityType protected property The entity type.
Entity::$isDefaultRevision protected property Indicates whether this is the default revision. 1
Entity::$langcode public property The language code of the entity's default language. 7
Entity::$newRevision protected property Boolean indicating whether a new revision should be created on save.
Entity::access public function Implements \Drupal\Core\TypedData\AccessibleInterface::access(). Overrides AccessibleInterface::access
Entity::bundle public function Implements \Drupal\Core\Entity\EntityInterface::bundle(). Overrides EntityInterface::bundle 4
Entity::delete public function Implements \Drupal\Core\Entity\EntityInterface::delete(). Overrides EntityInterface::delete 3
Entity::enforceIsNew public function Implements \Drupal\Core\Entity\EntityInterface::enforceIsNew(). Overrides EntityInterface::enforceIsNew
Entity::entityInfo public function Implements \Drupal\Core\Entity\EntityInterface::entityInfo(). Overrides EntityInterface::entityInfo
Entity::entityType public function Implements \Drupal\Core\Entity\EntityInterface::entityType(). Overrides EntityInterface::entityType
Entity::getBCEntity public function Implements \Drupal\Core\Entity\EntityInterface::getBCEntity(). Overrides EntityInterface::getBCEntity 1
Entity::getConstraints public function Implements \Drupal\Core\TypedData\TypedDataInterface::getConstraints(). Overrides TypedDataInterface::getConstraints
Entity::getDefinition public function Implements \Drupal\Core\TypedData\TypedDataInterface::getDefinition(). Overrides TypedDataInterface::getDefinition
Entity::getIterator public function Implements \Drupal\Core\TypedData\ComplexDataInterface::getIterator(). 1
Entity::getName public function Implements \Drupal\Core\TypedData\TypedDataInterface::getName(). Overrides TypedDataInterface::getName
Entity::getNGEntity public function Implements \Drupal\Core\Entity\EntityInterface::getNGEntity(). Overrides EntityInterface::getNGEntity
Entity::getParent public function Implements \Drupal\Core\TypedData\TypedDataInterface::getParent(). Overrides TypedDataInterface::getParent
Entity::getProperties public function Implements \Drupal\Core\TypedData\ComplexDataInterface::getProperties(). Overrides ComplexDataInterface::getProperties 1
Entity::getPropertyDefinition public function Implements \Drupal\Core\TypedData\ComplexDataInterface::getPropertyDefinition(). Overrides ComplexDataInterface::getPropertyDefinition 1
Entity::getPropertyDefinitions public function Implements \Drupal\Core\TypedData\ComplexDataInterface::getPropertyDefinitions(). Overrides ComplexDataInterface::getPropertyDefinitions 1
Entity::getPropertyPath public function Implements \Drupal\Core\TypedData\TypedDataInterface::getPropertyPath(). Overrides TypedDataInterface::getPropertyPath
Entity::getPropertyValues public function Implements \Drupal\Core\TypedData\ComplexDataInterface::getPropertyValues(). Overrides ComplexDataInterface::getPropertyValues 1
Entity::getRevisionId public function Implements \Drupal\Core\Entity\EntityInterface::getRevisionId(). Overrides EntityInterface::getRevisionId 4
Entity::getRoot public function Implements \Drupal\Core\TypedData\TypedDataInterface::getRoot(). Overrides TypedDataInterface::getRoot
Entity::getString public function Implements \Drupal\Core\TypedData\TypedDataInterface::getString(). Overrides TypedDataInterface::getString
Entity::getTranslation public function Implements \Drupal\Core\TypedData\TranslatableInterface::getTranslation(). Overrides TranslatableInterface::getTranslation 1
Entity::getTranslationLanguages public function Implements \Drupal\Core\TypedData\TranslatableInterface::getTranslationLanguages(). Overrides TranslatableInterface::getTranslationLanguages 1
Entity::getType public function Implements \Drupal\Core\TypedData\TypedDataInterface::getType(). Overrides TypedDataInterface::getType 2
Entity::getValue public function Implements \Drupal\Core\TypedData\TypedDataInterface::getValue(). Overrides TypedDataInterface::getValue
Entity::isDefaultRevision public function Implements \Drupal\Core\Entity\EntityInterface::isDefaultRevision(). Overrides EntityInterface::isDefaultRevision 1
Entity::isEmpty public function Implements \Drupal\Core\TypedData\ComplexDataInterface::isEmpty(). Overrides ComplexDataInterface::isEmpty 1
Entity::isNewRevision public function Implements \Drupal\Core\Entity\EntityInterface::isNewRevision(). Overrides EntityInterface::isNewRevision
Entity::isTranslatable public function Implements \Drupal\Core\Entity\EntityInterface::isTranslatable(). Overrides EntityInterface::isTranslatable
Entity::label public function Implements \Drupal\Core\Entity\EntityInterface::label(). Overrides EntityInterface::label 4
Entity::language public function Implements \Drupal\Core\TypedData\TranslatableInterface::language(). Overrides TranslatableInterface::language 1
Entity::onChange public function Implements \Drupal\Core\TypedData\ComplexDataInterface::onChange(). Overrides ComplexDataInterface::onChange
Entity::setContext public function Implements \Drupal\Core\TypedData\TypedDataInterface::setContext(). Overrides TypedDataInterface::setContext
Entity::setNewRevision public function Implements \Drupal\Core\Entity\EntityInterface::setNewRevision(). Overrides EntityInterface::setNewRevision
Entity::setPropertyValues public function Implements \Drupal\Core\TypedData\ComplexDataInterface::setPropertyValues(). Overrides ComplexDataInterface::setPropertyValues 1
Entity::setValue public function Implements \Drupal\Core\TypedData\TypedDataInterface::setValue(). Overrides TypedDataInterface::setValue
Entity::translations public function Returns the languages the entity is translated to. 1
Entity::uri public function Implements \Drupal\Core\Entity\EntityInterface::uri(). Overrides EntityInterface::uri 8
Entity::uriRelationships public function Returns a list of URI relationships supported by this entity. Overrides EntityInterface::uriRelationships
Entity::uuid public function Implements \Drupal\Core\Entity\EntityInterface::uuid(). Overrides EntityInterface::uuid 1
Entity::validate public function Implements \Drupal\Core\TypedData\TypedDataInterface::validate(). Overrides TypedDataInterface::validate 1
EntityDisplayBase::$bundle public property Bundle to be displayed.
EntityDisplayBase::$content protected property List of component display options, keyed by component name.
EntityDisplayBase::$displayContext protected property Context in which this entity will be used (e.g. 'display', 'form').
EntityDisplayBase::$id public property Unique ID for the config entity.
EntityDisplayBase::$mode public property View or form mode to be displayed.
EntityDisplayBase::$originalMode public property The original view or form mode that was requested (case of view/form modes being configured to fall back to the 'default' display).
EntityDisplayBase::$pluginManager protected property The plugin manager used by this entity type.
EntityDisplayBase::$plugins protected property The plugin objects used for this display, keyed by field name.
EntityDisplayBase::$targetEntityType public property Entity type to be displayed.
EntityDisplayBase::$uuid public property Unique UUID for the config entity.
EntityDisplayBase::createCopy public function Creates a duplicate of the EntityDisplay object on a different view mode. Overrides EntityDisplayBaseInterface::createCopy
EntityDisplayBase::getComponent public function Gets the display options set for a component. Overrides EntityDisplayBaseInterface::getComponent
EntityDisplayBase::getComponents public function Gets the display options for all components. Overrides EntityDisplayBaseInterface::getComponents
EntityDisplayBase::getExportProperties public function Overrides \Drupal\Core\Entity\Entity::getExportProperties(). Overrides ConfigEntityBase::getExportProperties
EntityDisplayBase::getHighestWeight public function Returns the highest weight of the components in the display. Overrides EntityDisplayBaseInterface::getHighestWeight
EntityDisplayBase::id public function Implements \Drupal\Core\Entity\EntityInterface::id(). Overrides Entity::id
EntityDisplayBase::removeComponent public function Sets a component to be hidden. Overrides EntityDisplayBaseInterface::removeComponent
EntityDisplayBase::save public function Implements \Drupal\Core\Entity\EntityInterface::save(). Overrides Entity::save
EntityDisplayBase::setComponent public function Sets the display options for a component. Overrides EntityDisplayBaseInterface::setComponent
EntityDisplayBase::__construct public function Overrides Entity::__construct(). Overrides ConfigEntityBase::__construct 2