BundleEntityDuplicator.php in Entity API 8
File
src/BundleEntityDuplicator.php
View source
<?php
namespace Drupal\entity;
use Drupal\Core\Config\Entity\ConfigEntityInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
class BundleEntityDuplicator implements BundleEntityDuplicatorInterface {
protected $entityTypeManager;
public function __construct(EntityTypeManagerInterface $entity_type_manager) {
$this->entityTypeManager = $entity_type_manager;
}
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;
}
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) {
$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();
}
}
public function duplicateDisplays(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() . '.';
$form_displays = $this
->loadEntities('entity_form_display', $id_prefix);
$view_displays = $this
->loadEntities('entity_view_display', $id_prefix);
foreach ($form_displays as $form_display) {
$duplicate_form_display = $form_display
->createDuplicate();
$duplicate_form_display
->set('id', $bundle_of . '.' . $target_bundle_id . '.' . $form_display
->getMode());
$duplicate_form_display
->set('bundle', $target_bundle_id);
$duplicate_form_display
->save();
}
foreach ($view_displays as $view_display) {
$duplicate_view_display = $view_display
->createDuplicate();
$duplicate_view_display
->set('id', $bundle_of . '.' . $target_bundle_id . '.' . $view_display
->getMode());
$duplicate_view_display
->set('bundle', $target_bundle_id);
$duplicate_view_display
->save();
}
}
protected function loadEntities($entity_type_id, $id_prefix) {
$storage = $this->entityTypeManager
->getStorage($entity_type_id);
$ids = $storage
->getQuery()
->condition('id', $id_prefix, 'STARTS_WITH')
->execute();
return $ids ? $storage
->loadMultiple($ids) : [];
}
}