public function TypedDataManager::create

Creates a new typed data object instance.

Parameters

array $definition: The data definition array with the following array keys and values:

  • type: The data type of the data to wrap. Required.
  • label: A human readable label.
  • description: A human readable description.
  • list: Whether the data is multi-valued, i.e. a list of data items. Defaults to FALSE.
  • computed: A boolean specifying whether the data value is computed by the object, e.g. depending on some other values.
  • read-only: A boolean specifying whether the data is read-only. Defaults to TRUE for computed properties, to FALSE otherwise.
  • class: If set and 'list' is FALSE, the class to use for creating the typed data object; otherwise the default class of the data type will be used.
  • list class: If set and 'list' is TRUE, the class to use for creating the typed data object; otherwise the default list class of the data type will be used.
  • settings: An array of settings, as required by the used 'class'. See the documentation of the class for supported or required settings.
  • list settings: An array of settings as required by the used 'list class'. See the documentation of the list class for support or required settings.
  • constraints: An array of validation constraints. See \Drupal\Core\TypedData\TypedDataManager::getConstraints() for details.
  • required: A boolean specifying whether a non-NULL value is mandatory.

Further keys may be supported in certain usages, e.g. for further keys supported for entity field definitions see \Drupal\Core\Entity\StorageControllerInterface::getPropertyDefinitions().

mixed $value: (optional) The data value. If set, it has to match one of the supported data type format as documented for the data type classes.

string $name: (optional) If a property or list item is to be created, the name of the property or the delta of the list item.

mixed $parent: (optional) If a property or list item is to be created, the parent typed data object implementing either the ListInterface or the ComplexDataInterface.

Return value

\Drupal\Core\TypedData\TypedDataInterface The instantiated typed data object.

See also

\Drupal::typedData()

\Drupal\Core\TypedData\TypedDataManager::getPropertyInstance()

\Drupal\Core\TypedData\Type\Integer

\Drupal\Core\TypedData\Type\Float

\Drupal\Core\TypedData\Type\String

\Drupal\Core\TypedData\Type\Boolean

\Drupal\Core\TypedData\Type\Duration

\Drupal\Core\TypedData\Type\Date

\Drupal\Core\TypedData\Type\Uri

\Drupal\Core\TypedData\Type\Binary

\Drupal\Core\Entity\Field\EntityWrapper

2 calls to TypedDataManager::create()
TypedConfigManager::create in drupal/core/lib/Drupal/Core/Config/TypedConfigManager.php
Overrides \Drupal\Core\TypedData\TypedDataManager::create()
TypedDataManager::getPropertyInstance in drupal/core/lib/Drupal/Core/TypedData/TypedDataManager.php
Get a typed data instance for a property of a given typed data object.
1 method overrides TypedDataManager::create()

File

drupal/core/lib/Drupal/Core/TypedData/TypedDataManager.php, line 145
Contains \Drupal\Core\TypedData\TypedDataManager.

Class

TypedDataManager
Manages data type plugins.

Namespace

Drupal\Core\TypedData

Code

public function create(array $definition, $value = NULL, $name = NULL, $parent = NULL) {
  $wrapper = $this->factory
    ->createInstance($definition['type'], $definition, $name, $parent);
  if (isset($value)) {
    $wrapper
      ->setValue($value, FALSE);
  }
  return $wrapper;
}