3 namespace Drupal\Core\TypedData;
6 * Interface for data definitions.
8 * Data definitions are used to describe data based upon available data types.
9 * For example, a plugin could describe its parameters using data definitions
10 * in order to specify what kind of data is required for it.
12 * Definitions that describe lists or complex data have to implement the
13 * respective interfaces, such that the metadata about contained list items or
14 * properties can be retrieved from the definition.
16 * @see \Drupal\Core\TypedData\DataDefinition
17 * @see \Drupal\Core\TypedData\ListDefinitionInterface
18 * @see \Drupal\Core\TypedData\ComplexDataDefinitionInterface
19 * @see \Drupal\Core\TypedData\DataReferenceDefinitionInterface
20 * @see \Drupal\Core\TypedData\TypedDataInterface
24 interface DataDefinitionInterface {
27 * Creates a new data definition object.
29 * This method is typically used by
30 * \Drupal\Core\TypedData\TypedDataManager::createDataDefinition() to build a
31 * definition object for an arbitrary data type. When the definition class is
32 * known, it is recommended to directly use the static create() method on that
33 * class instead; e.g.:
35 * $map_definition = \Drupal\Core\TypedData\MapDataDefinition::create();
38 * @param string $data_type
39 * The data type, for which a data definition should be created.
43 * @throws \InvalidArgumentException
44 * If an unsupported data type gets passed to the class; e.g., 'string' to a
45 * definition class handling 'entity:* data types.
47 public static function createFromDataType($data_type);
50 * Returns the data type of the data.
55 public function getDataType();
58 * Returns a human readable label.
60 * @return string|\Drupal\Core\StringTranslation\TranslatableMarkup
61 * The label. A string or an instance of TranslatableMarkup will be returned
62 * based on the way the label translation is handled.
64 public function getLabel();
67 * Returns a human readable description.
69 * Descriptions are usually used on user interfaces where the data is edited
73 * The description, or NULL if no description is available.
75 public function getDescription();
78 * Returns whether the data is multi-valued, i.e. a list of data items.
80 * This is equivalent to checking whether the data definition implements the
81 * \Drupal\Core\TypedData\ListDefinitionInterface interface.
84 * Whether the data is multi-valued.
86 public function isList();
89 * Determines whether the data is read-only.
92 * Whether the data is read-only.
94 public function isReadOnly();
97 * Determines whether the data value is computed.
99 * For example, data could be computed depending on some other values.
102 * Whether the data value is computed.
104 public function isComputed();
107 * Determines whether a data value is required.
109 * For required data a non-NULL value is mandatory.
112 * Whether a data value is required.
114 public function isRequired();
117 * Returns the class used for creating the typed data object.
119 * If not specified, the default class of the data type will be returned.
122 * The class used for creating the typed data object.
124 public function getClass();
127 * Returns the array of settings, as required by the used class.
129 * See the documentation of the class for supported or required settings.
132 * The array of settings.
134 public function getSettings();
137 * Returns the value of a given setting.
139 * @param string $setting_name
145 public function getSetting($setting_name);
148 * Returns an array of validation constraints.
150 * The validation constraints of a definition consist of any for it defined
151 * constraints and default constraints, which are generated based on the
152 * definition and its data type. See
153 * \Drupal\Core\TypedData\TypedDataManager::getDefaultConstraints().
155 * Constraints are defined via an array, having constraint plugin IDs as key
156 * and constraint options as values, e.g.
158 * $constraints = array(
159 * 'Range' => array('min' => 5, 'max' => 10),
160 * 'NotBlank' => array(),
163 * Options have to be specified using another array if the constraint has more
164 * than one or zero options. If it has exactly one option, the value should be
165 * specified without nesting it into another array:
167 * $constraints = array(
168 * 'EntityType' => 'node',
169 * 'Bundle' => 'article',
173 * Note that the specified constraints must be compatible with the data type,
174 * e.g. for data of type 'entity' the 'EntityType' and 'Bundle' constraints
177 * @see \Drupal\Core\Validation\ConstraintManager
180 * An array of validation constraint definitions, keyed by constraint name.
181 * Each constraint definition can be used for instantiating
182 * \Symfony\Component\Validator\Constraint objects.
184 * @see \Symfony\Component\Validator\Constraint
186 public function getConstraints();
189 * Returns a validation constraint.
191 * See \Drupal\Core\TypedData\DataDefinitionInterface::getConstraints() for
194 * @param string $constraint_name
195 * The name of the constraint, i.e. its plugin id.
198 * A validation constraint definition which can be used for instantiating a
199 * \Symfony\Component\Validator\Constraint object.
201 * @see \Symfony\Component\Validator\Constraint
203 public function getConstraint($constraint_name);
206 * Adds a validation constraint.
208 * See \Drupal\Core\TypedData\DataDefinitionInterface::getConstraints() for
211 * @param string $constraint_name
212 * The name of the constraint to add, i.e. its plugin id.
213 * @param array|null $options
214 * The constraint options as required by the constraint plugin, or NULL.
217 * The object itself for chaining.
219 public function addConstraint($constraint_name, $options = NULL);