Overrides DatabaseStorageController::create().
array $values: An array of values to set, keyed by field name. The value has to be the plain value of an entity field, i.e. an array of field items. If no numerically indexed array is given, the value will be set for the first field item. For example, to set the first item of a 'name' field one can pass:
$values = array(
'name' => array(
0 => array(
'value' => 'the name',
),
),
);
or
$values = array(
'name' => array(
'value' => 'the name',
),
);
If the 'name' field is a defined as 'string_item' which supports setting its value by a string, it's also possible to just pass the name string:
$values = array(
'name' => 'the name',
);
\Drupal\Core\Entity\EntityInterface A new entity object.
Overrides DatabaseStorageController::create
public function create(array $values) {
// We have to determine the bundle first.
$bundle = FALSE;
if ($this->bundleKey) {
if (!isset($values[$this->bundleKey])) {
throw new EntityStorageException(t('Missing bundle for entity type @type', array(
'@type' => $this->entityType,
)));
}
$bundle = $values[$this->bundleKey];
}
$entity = new $this->entityClass(array(), $this->entityType, $bundle);
// Set all other given values.
foreach ($values as $name => $value) {
$entity->{$name} = $value;
}
// Assign a new UUID if there is none yet.
if ($this->uuidKey && !isset($entity->{$this->uuidKey}->value)) {
$uuid = new Uuid();
$entity->{$this->uuidKey} = $uuid
->generate();
}
// Modules might need to add or change the data initially held by the new
// entity object, for instance to fill-in default values.
$this
->invokeHook('create', $entity);
return $entity;
}