public function BundleEntityDuplicator::duplicate in Entity API 8
Duplicates the bundle entity, its fields and displays.
Parameters
\Drupal\Core\Config\Entity\ConfigEntityInterface $bundle_entity: The bundle entity.
array $values: An array of values to set, keyed by property name. Needs to at least contain a new ID.
Return value
\Drupal\Core\Config\Entity\ConfigEntityInterface The new bundle entity, after it has been saved.
Throws
\InvalidArgumentException Thrown if the given entity is not a bundle entity, or if $values does not contain a new ID.
Overrides BundleEntityDuplicatorInterface::duplicate
File
- src/
BundleEntityDuplicator.php, line 30
Class
Namespace
Drupal\entityCode
public function duplicate(ConfigEntityInterface $bundle_entity, array $values) {
$entity_type = $bundle_entity
->getEntityType();
if (!$entity_type
->getBundleOf()) {
throw new \InvalidArgumentException(sprintf('The "%s" entity type is not a bundle entity type.', $entity_type
->id()));
}
$id_key = $entity_type
->getKey('id');
if (empty($values[$id_key])) {
throw new \InvalidArgumentException(sprintf('The $values[\'%s\'] key is empty or missing.', $id_key));
}
$entity = $bundle_entity
->createDuplicate();
foreach ($values as $property_name => $value) {
$entity
->set($property_name, $value);
}
$entity
->save();
$this
->duplicateFields($bundle_entity, $entity
->id());
$this
->duplicateDisplays($bundle_entity, $entity
->id());
return $entity;
}