public function BundleEntityDuplicator::duplicateFields in Entity API 8
Duplicates the bundle entity's fields.
Parameters
\Drupal\Core\Config\Entity\ConfigEntityInterface $bundle_entity: The bundle entity.
string $target_bundle_id: The target bundle ID.
Throws
\InvalidArgumentException Thrown if the given entity is not a bundle entity.
Overrides BundleEntityDuplicatorInterface::duplicateFields
1 call to BundleEntityDuplicator::duplicateFields()
- BundleEntityDuplicator::duplicate in src/
BundleEntityDuplicator.php - Duplicates the bundle entity, its fields and displays.
File
- src/
BundleEntityDuplicator.php, line 54
Class
Namespace
Drupal\entityCode
public function duplicateFields(ConfigEntityInterface $bundle_entity, $target_bundle_id) {
$entity_type = $bundle_entity
->getEntityType();
$bundle_of = $entity_type
->getBundleOf();
if (!$bundle_of) {
throw new \InvalidArgumentException(sprintf('The "%s" entity type is not a bundle entity type.', $entity_type
->id()));
}
if (empty($target_bundle_id)) {
throw new \InvalidArgumentException('The $target_bundle_id must not be empty.');
}
$id_prefix = $bundle_of . '.' . $bundle_entity
->id() . '.';
$fields = $this
->loadEntities('field_config', $id_prefix);
foreach ($fields as $field) {
/** @var \Drupal\Core\Field\FieldConfigInterface $field */
$duplicate_field = $field
->createDuplicate();
$duplicate_field
->set('id', $bundle_of . '.' . $target_bundle_id . '.' . $field
->getName());
$duplicate_field
->set('bundle', $target_bundle_id);
$duplicate_field
->save();
}
}