Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 namespace Drupal\Core\Entity;
|
Chris@0
|
4
|
Chris@0
|
5 /**
|
Chris@0
|
6 * Interface for entities having fields.
|
Chris@0
|
7 *
|
Chris@0
|
8 * This interface builds upon the general interfaces provided by the typed data
|
Chris@0
|
9 * API, while extending them with entity-specific additions. I.e., fieldable
|
Chris@0
|
10 * entities implement the ComplexDataInterface among others, thus it is complex
|
Chris@0
|
11 * data containing fields as its data properties. The contained fields have to
|
Chris@0
|
12 * implement \Drupal\Core\Field\FieldItemListInterface, which builds upon typed
|
Chris@0
|
13 * data interfaces as well.
|
Chris@0
|
14 *
|
Chris@0
|
15 * When implementing this interface which extends Traversable, make sure to list
|
Chris@0
|
16 * IteratorAggregate or Iterator before this interface in the implements clause.
|
Chris@0
|
17 *
|
Chris@0
|
18 * @see \Drupal\Core\TypedData\TypedDataManager
|
Chris@0
|
19 * @see \Drupal\Core\Field\FieldItemListInterface
|
Chris@0
|
20 *
|
Chris@0
|
21 * @ingroup entity_api
|
Chris@0
|
22 */
|
Chris@0
|
23 interface FieldableEntityInterface extends EntityInterface {
|
Chris@0
|
24
|
Chris@0
|
25 /**
|
Chris@0
|
26 * Provides base field definitions for an entity type.
|
Chris@0
|
27 *
|
Chris@0
|
28 * Implementations typically use the class
|
Chris@0
|
29 * \Drupal\Core\Field\BaseFieldDefinition for creating the field definitions;
|
Chris@0
|
30 * for example a 'name' field could be defined as the following:
|
Chris@0
|
31 * @code
|
Chris@0
|
32 * $fields['name'] = BaseFieldDefinition::create('string')
|
Chris@0
|
33 * ->setLabel(t('Name'));
|
Chris@0
|
34 * @endcode
|
Chris@0
|
35 *
|
Chris@0
|
36 * By definition, base fields are fields that exist for every bundle. To
|
Chris@0
|
37 * provide definitions for fields that should only exist on some bundles, use
|
Chris@0
|
38 * \Drupal\Core\Entity\FieldableEntityInterface::bundleFieldDefinitions().
|
Chris@0
|
39 *
|
Chris@0
|
40 * The definitions returned by this function can be overridden for all
|
Chris@0
|
41 * bundles by hook_entity_base_field_info_alter() or overridden on a
|
Chris@0
|
42 * per-bundle basis via 'base_field_override' configuration entities.
|
Chris@0
|
43 *
|
Chris@0
|
44 * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
|
Chris@0
|
45 * The entity type definition. Useful when a single class is used for multiple,
|
Chris@0
|
46 * possibly dynamic entity types.
|
Chris@0
|
47 *
|
Chris@0
|
48 * @return \Drupal\Core\Field\FieldDefinitionInterface[]
|
Chris@0
|
49 * An array of base field definitions for the entity type, keyed by field
|
Chris@0
|
50 * name.
|
Chris@0
|
51 *
|
Chris@0
|
52 * @see \Drupal\Core\Entity\EntityManagerInterface::getFieldDefinitions()
|
Chris@0
|
53 * @see \Drupal\Core\Entity\FieldableEntityInterface::bundleFieldDefinitions()
|
Chris@0
|
54 */
|
Chris@0
|
55 public static function baseFieldDefinitions(EntityTypeInterface $entity_type);
|
Chris@0
|
56
|
Chris@0
|
57 /**
|
Chris@0
|
58 * Provides field definitions for a specific bundle.
|
Chris@0
|
59 *
|
Chris@0
|
60 * This function can return definitions both for bundle fields (fields that
|
Chris@0
|
61 * are not defined in $base_field_definitions, and therefore might not exist
|
Chris@0
|
62 * on some bundles) as well as bundle-specific overrides of base fields
|
Chris@0
|
63 * (fields that are defined in $base_field_definitions, and therefore exist
|
Chris@0
|
64 * for all bundles). However, bundle-specific base field overrides can also
|
Chris@0
|
65 * be provided by 'base_field_override' configuration entities, and that is
|
Chris@0
|
66 * the recommended approach except in cases where an entity type needs to
|
Chris@0
|
67 * provide a bundle-specific base field override that is decoupled from
|
Chris@0
|
68 * configuration. Note that for most entity types, the bundles themselves are
|
Chris@0
|
69 * derived from configuration (e.g., 'node' bundles are managed via
|
Chris@0
|
70 * 'node_type' configuration entities), so decoupling bundle-specific base
|
Chris@0
|
71 * field overrides from configuration only makes sense for entity types that
|
Chris@0
|
72 * also decouple their bundles from configuration. In cases where both this
|
Chris@0
|
73 * function returns a bundle-specific override of a base field and a
|
Chris@0
|
74 * 'base_field_override' configuration entity exists, the latter takes
|
Chris@0
|
75 * precedence.
|
Chris@0
|
76 *
|
Chris@0
|
77 * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
|
Chris@0
|
78 * The entity type definition. Useful when a single class is used for multiple,
|
Chris@0
|
79 * possibly dynamic entity types.
|
Chris@0
|
80 * @param string $bundle
|
Chris@0
|
81 * The bundle.
|
Chris@0
|
82 * @param \Drupal\Core\Field\FieldDefinitionInterface[] $base_field_definitions
|
Chris@0
|
83 * The list of base field definitions.
|
Chris@0
|
84 *
|
Chris@0
|
85 * @return \Drupal\Core\Field\FieldDefinitionInterface[]
|
Chris@0
|
86 * An array of bundle field definitions, keyed by field name.
|
Chris@0
|
87 *
|
Chris@0
|
88 * @see \Drupal\Core\Entity\EntityManagerInterface::getFieldDefinitions()
|
Chris@0
|
89 * @see \Drupal\Core\Entity\FieldableEntityInterface::baseFieldDefinitions()
|
Chris@0
|
90 *
|
Chris@0
|
91 * @todo WARNING: This method will be changed in
|
Chris@0
|
92 * https://www.drupal.org/node/2346347.
|
Chris@0
|
93 */
|
Chris@0
|
94 public static function bundleFieldDefinitions(EntityTypeInterface $entity_type, $bundle, array $base_field_definitions);
|
Chris@0
|
95
|
Chris@0
|
96 /**
|
Chris@0
|
97 * Determines whether the entity has a field with the given name.
|
Chris@0
|
98 *
|
Chris@0
|
99 * @param string $field_name
|
Chris@0
|
100 * The field name.
|
Chris@0
|
101 *
|
Chris@0
|
102 * @return bool
|
Chris@0
|
103 * TRUE if the entity has a field with the given name. FALSE otherwise.
|
Chris@0
|
104 */
|
Chris@0
|
105 public function hasField($field_name);
|
Chris@0
|
106
|
Chris@0
|
107 /**
|
Chris@0
|
108 * Gets the definition of a contained field.
|
Chris@0
|
109 *
|
Chris@0
|
110 * @param string $name
|
Chris@0
|
111 * The name of the field.
|
Chris@0
|
112 *
|
Chris@0
|
113 * @return \Drupal\Core\Field\FieldDefinitionInterface|null
|
Chris@0
|
114 * The definition of the field or null if the field does not exist.
|
Chris@0
|
115 */
|
Chris@0
|
116 public function getFieldDefinition($name);
|
Chris@0
|
117
|
Chris@0
|
118 /**
|
Chris@0
|
119 * Gets an array of field definitions of all contained fields.
|
Chris@0
|
120 *
|
Chris@0
|
121 * @return \Drupal\Core\Field\FieldDefinitionInterface[]
|
Chris@0
|
122 * An array of field definitions, keyed by field name.
|
Chris@0
|
123 *
|
Chris@0
|
124 * @see \Drupal\Core\Entity\EntityManagerInterface::getFieldDefinitions()
|
Chris@0
|
125 */
|
Chris@0
|
126 public function getFieldDefinitions();
|
Chris@0
|
127
|
Chris@0
|
128 /**
|
Chris@0
|
129 * Gets an array of all field values.
|
Chris@0
|
130 *
|
Chris@0
|
131 * Gets an array of plain field values, including only non-computed values.
|
Chris@0
|
132 * Note that the structure varies by entity type and bundle.
|
Chris@0
|
133 *
|
Chris@0
|
134 * @return array
|
Chris@0
|
135 * An array of field values, keyed by field name.
|
Chris@0
|
136 */
|
Chris@0
|
137 public function toArray();
|
Chris@0
|
138
|
Chris@0
|
139 /**
|
Chris@0
|
140 * Gets a field item list.
|
Chris@0
|
141 *
|
Chris@0
|
142 * @param string $field_name
|
Chris@0
|
143 * The name of the field to get; e.g., 'title' or 'name'.
|
Chris@0
|
144 *
|
Chris@0
|
145 * @return \Drupal\Core\Field\FieldItemListInterface
|
Chris@0
|
146 * The field item list, containing the field items.
|
Chris@0
|
147 *
|
Chris@0
|
148 * @throws \InvalidArgumentException
|
Chris@0
|
149 * If an invalid field name is given.
|
Chris@0
|
150 */
|
Chris@0
|
151 public function get($field_name);
|
Chris@0
|
152
|
Chris@0
|
153 /**
|
Chris@0
|
154 * Sets a field value.
|
Chris@0
|
155 *
|
Chris@0
|
156 * @param string $field_name
|
Chris@0
|
157 * The name of the field to set; e.g., 'title' or 'name'.
|
Chris@0
|
158 * @param mixed $value
|
Chris@0
|
159 * The value to set, or NULL to unset the field.
|
Chris@0
|
160 * @param bool $notify
|
Chris@0
|
161 * (optional) Whether to notify the entity of the change. Defaults to
|
Chris@0
|
162 * TRUE. If the update stems from the entity, set it to FALSE to avoid
|
Chris@0
|
163 * being notified again.
|
Chris@0
|
164 *
|
Chris@0
|
165 * @return $this
|
Chris@0
|
166 *
|
Chris@0
|
167 * @throws \InvalidArgumentException
|
Chris@0
|
168 * If the specified field does not exist.
|
Chris@0
|
169 */
|
Chris@0
|
170 public function set($field_name, $value, $notify = TRUE);
|
Chris@0
|
171
|
Chris@0
|
172 /**
|
Chris@0
|
173 * Gets an array of all field item lists.
|
Chris@0
|
174 *
|
Chris@0
|
175 * @param bool $include_computed
|
Chris@0
|
176 * If set to TRUE, computed fields are included. Defaults to TRUE.
|
Chris@0
|
177 *
|
Chris@0
|
178 * @return \Drupal\Core\Field\FieldItemListInterface[]
|
Chris@0
|
179 * An array of field item lists implementing, keyed by field name.
|
Chris@0
|
180 */
|
Chris@0
|
181 public function getFields($include_computed = TRUE);
|
Chris@0
|
182
|
Chris@0
|
183 /**
|
Chris@0
|
184 * Gets an array of field item lists for translatable fields.
|
Chris@0
|
185 *
|
Chris@0
|
186 * @param bool $include_computed
|
Chris@0
|
187 * If set to TRUE, computed fields are included. Defaults to TRUE.
|
Chris@0
|
188 *
|
Chris@0
|
189 * @return \Drupal\Core\Field\FieldItemListInterface[]
|
Chris@0
|
190 * An array of field item lists implementing, keyed by field name.
|
Chris@0
|
191 */
|
Chris@0
|
192 public function getTranslatableFields($include_computed = TRUE);
|
Chris@0
|
193
|
Chris@0
|
194 /**
|
Chris@0
|
195 * Reacts to changes to a field.
|
Chris@0
|
196 *
|
Chris@0
|
197 * Note that this is invoked after any changes have been applied.
|
Chris@0
|
198 *
|
Chris@0
|
199 * @param string $field_name
|
Chris@0
|
200 * The name of the field which is changed.
|
Chris@0
|
201 *
|
Chris@0
|
202 * @throws \InvalidArgumentException
|
Chris@0
|
203 * When trying to assign a value to the language field that matches an
|
Chris@0
|
204 * existing translation.
|
Chris@0
|
205 * @throws \LogicException
|
Chris@0
|
206 * When trying to change:
|
Chris@0
|
207 * - The language of a translation.
|
Chris@0
|
208 * - The value of the flag identifying the default translation object.
|
Chris@0
|
209 */
|
Chris@0
|
210 public function onChange($field_name);
|
Chris@0
|
211
|
Chris@0
|
212 /**
|
Chris@0
|
213 * Validates the currently set values.
|
Chris@0
|
214 *
|
Chris@0
|
215 * @return \Drupal\Core\Entity\EntityConstraintViolationListInterface
|
Chris@0
|
216 * A list of constraint violations. If the list is empty, validation
|
Chris@0
|
217 * succeeded.
|
Chris@0
|
218 */
|
Chris@0
|
219 public function validate();
|
Chris@0
|
220
|
Chris@0
|
221 /**
|
Chris@0
|
222 * Checks whether entity validation is required before saving the entity.
|
Chris@0
|
223 *
|
Chris@0
|
224 * @return bool
|
Chris@0
|
225 * TRUE if validation is required, FALSE if not.
|
Chris@0
|
226 */
|
Chris@0
|
227 public function isValidationRequired();
|
Chris@0
|
228
|
Chris@0
|
229 /**
|
Chris@0
|
230 * Sets whether entity validation is required before saving the entity.
|
Chris@0
|
231 *
|
Chris@0
|
232 * @param bool $required
|
Chris@0
|
233 * TRUE if validation is required, FALSE otherwise.
|
Chris@0
|
234 *
|
Chris@0
|
235 * @return $this
|
Chris@0
|
236 */
|
Chris@0
|
237 public function setValidationRequired($required);
|
Chris@0
|
238
|
Chris@0
|
239 }
|