public function Period::toEntity in Recurring Time Period 8
Creates an entity from this period.
This requires the entity class for the specified entity type to use \Drupal\recurring_period\Entity\PeriodEntityTrait, or have compatible entity keys.
Parameters
string $entity_type_id: The entity type ID of the entity to create.
array $values: An array of initial values for the new entity. If this contains a value for the label field, it will be taken over any label from the Period object.
Return value
\Drupal\Core\Entity\EntityBase The unsaved entity.
File
- src/
DateTime/ Period.php, line 74
Class
- Period
- Represents a period of time with specific start and end dates.
Namespace
Drupal\recurring_period\DatetimeCode
public function toEntity($entity_type_id, $values = []) {
$entity_type = \Drupal::entityTypeManager()
->getDefinition($entity_type_id);
if ($entity_type
->hasKey('date_range')) {
$values += [
$entity_type
->getKey('date_range') => [
'value' => $this
->getStartDate()
->format(DateTimeItemInterface::DATETIME_STORAGE_FORMAT),
'end_value' => $this
->getEndDate()
->format(DateTimeItemInterface::DATETIME_STORAGE_FORMAT),
],
];
}
else {
$values += [
$entity_type
->getKey('start_date') => $this
->getStartDate()
->format(DateTimeItemInterface::DATETIME_STORAGE_FORMAT),
$entity_type
->getKey('end_date') => $this
->getEndDate()
->format(DateTimeItemInterface::DATETIME_STORAGE_FORMAT),
];
}
if ($entity_type
->hasKey('label')) {
$values += [
$entity_type
->getKey('label') => $this
->getLabel(),
];
}
return \Drupal::entityTypeManager()
->getStorage($entity_type_id)
->create($values);
}