ContentEntityInterface.php 4.5 KB
Newer Older
1 2 3 4
<?php

/**
 * @file
5
 * Contains \Drupal\Core\Entity\ContentEntityInterface.
6 7
 */

8
namespace Drupal\Core\Entity;
9

10 11 12
use Drupal\Core\TypedData\ComplexDataInterface;
use Drupal\Core\TypedData\TranslatableInterface;

13 14
/**
 * Defines a common interface for all content entity objects.
15 16 17 18 19
 *
 * This interface builds upon the general interfaces provided by the typed data
 * API, while extending them with content entity-specific additions. I.e., a
 * content entity implements the ComplexDataInterface among others, thus is
 * complex data containing fields as its data properties. The contained fields
20
 * have to implement \Drupal\Core\Field\FieldItemListInterface,
21 22 23 24 25 26
 * which builds upon typed data interfaces as well.
 *
 * When implementing this interface which extends Traversable, make sure to list
 * IteratorAggregate or Iterator before this interface in the implements clause.
 *
 * @see \Drupal\Core\TypedData\TypedDataManager
27
 * @see \Drupal\Core\Field\FieldItemListInterface
28
 */
29 30 31 32 33 34 35 36 37 38 39 40 41 42
interface ContentEntityInterface extends EntityInterface, RevisionableInterface, TranslatableInterface, ComplexDataInterface {

  /**
   * Marks the translation identified by the given language code as existing.
   *
   * @param string $langcode
   *   The language code identifying the translation to be initialized.
   *
   * @todo Remove this as soon as translation metadata have been converted to
   *    regular fields.
   */
  public function initTranslation($langcode);

  /**
43
   * Provides base field definitions for an entity type.
44
   *
45 46 47 48 49 50 51 52
   * Implementations typically use the class \Drupal\Core\Field\FieldDefinition
   * for creating the field definitions; for example a 'name' field could be
   * defined as the following:
   * @code
   * $fields['name'] = FieldDefinition::create('string')
   *   ->setLabel(t('Name'));
   * @endcode
   *
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
   * If some elements in a field definition need to vary by bundle, use
   * \Drupal\Core\Entity\ContentEntityInterface::bundleFieldDefinitions().
   *
   * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
   *   The entity type definition. Useful when a single class is used for multiple,
   *   possibly dynamic entity types.
   *
   * @return \Drupal\Core\Field\FieldDefinitionInterface[]
   *   An array of base field definitions for the entity type, keyed by field
   *   name.
   *
   * @see \Drupal\Core\Entity\EntityManagerInterface::getFieldDefinitions()
   * @see \Drupal\Core\Entity\ContentEntityInterface::bundleFieldDefinitions()
   */
  public static function baseFieldDefinitions(EntityTypeInterface $entity_type);

  /**
   * Provides or alters field definitions for a specific bundle.
   *
   * The field definitions returned here for the bundle take precedence on the
   * base field definitions specified by baseFieldDefinitions() for the entity
   * type.
   *
   * @todo Provide a better DX for field overrides.
   *   See https://drupal.org/node/2145115.
   *
   * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
   *   The entity type definition. Useful when a single class is used for multiple,
   *   possibly dynamic entity types.
   * @param string $bundle
   *   The bundle.
   * @param \Drupal\Core\Field\FieldDefinitionInterface[] $base_field_definitions
   *   The list of base field definitions.
86
   *
87
   * @return \Drupal\Core\Field\FieldDefinitionInterface[]
88
   *   An array of bundle field definitions, keyed by field name.
89
   *
90
   * @see \Drupal\Core\Entity\EntityManagerInterface::getFieldDefinitions()
91
   * @see \Drupal\Core\Entity\ContentEntityInterface::baseFieldDefinitions()
92
   */
93
  public static function bundleFieldDefinitions(EntityTypeInterface $entity_type, $bundle, array $base_field_definitions);
94

95 96 97 98 99 100 101 102 103 104 105
  /**
   * Returns whether the entity has a field with the given name.
   *
   * @param string $field_name
   *   The field name.
   *
   * @return bool
   *   TRUE if the entity has a field with the given name. FALSE otherwise.
   */
  public function hasField($field_name);

106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
  /**
   * Gets the definition of a contained field.
   *
   * @param string $name
   *   The name of the field.
   *
   * @return \Drupal\Core\Field\FieldDefinitionInterface|false
   *   The definition of the field or FALSE if the field does not exist.
   */
  public function getFieldDefinition($name);

  /**
   * Gets an array of field definitions of all contained fields.
   *
   * @return \Drupal\Core\Field\FieldDefinitionInterface[]
   *   An array of field definitions, keyed by field name.
   *
   * @see \Drupal\Core\Entity\EntityManagerInterface::getFieldDefinitions()
   */
  public function getFieldDefinitions();

127
}