LayoutBuilderEntityClone.php in Entity Clone 8
File
src/EntityClone/Config/LayoutBuilderEntityClone.php
View source
<?php
namespace Drupal\entity_clone\EntityClone\Config;
use Drupal\Component\Plugin\DerivativeInspectionInterface;
use Drupal\Component\Plugin\PluginBase;
use Drupal\Component\Uuid\UuidInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\layout_builder\SectionComponent;
use Drupal\layout_builder\Section;
use Symfony\Component\DependencyInjection\ContainerInterface;
class LayoutBuilderEntityClone extends ConfigEntityCloneBase {
protected $uuidGenerator;
public function __construct(EntityTypeManagerInterface $entity_type_manager, $entity_type_id, UuidInterface $uuid) {
parent::__construct($entity_type_manager, $entity_type_id);
$this->uuidGenerator = $uuid;
}
public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) {
return new static($container
->get('entity_type.manager'), $entity_type
->id(), $container
->get('uuid'));
}
public function cloneEntity(EntityInterface $entity, EntityInterface $cloned_entity, array $properties = []) {
$cloned_entity = parent::cloneEntity($entity, $cloned_entity, $properties);
if ($cloned_entity
->isLayoutBuilderEnabled()) {
$cloned_entity
->removeAllSections();
foreach ($entity
->getSections() as $section) {
$cloned_components = [];
foreach ($section
->getComponents() as $section_component) {
$plugin = $section_component
->getPlugin();
$component_array = $section_component
->toArray();
$deriver_id = $plugin
->getPluginDefinition()['id'];
switch ($deriver_id) {
case 'field_block':
case 'extra_field_block':
$full_id = explode(PluginBase::DERIVATIVE_SEPARATOR, $plugin
->getPluginId());
$field_name = end($full_id);
$derivative_id = $cloned_entity
->getTargetEntityTypeId() . PluginBase::DERIVATIVE_SEPARATOR . $cloned_entity
->getTargetBundle() . PluginBase::DERIVATIVE_SEPARATOR . $field_name;
break;
case 'inline_block':
$derivative_id = $plugin
->getDerivativeId();
break;
default:
if ($plugin instanceof DerivativeInspectionInterface) {
$derivative_id = $plugin
->getDerivativeId();
}
else {
$derivative_id = '';
}
break;
}
$cloned_plugin_id = $deriver_id . (!empty($derivative_id) ? PluginBase::DERIVATIVE_SEPARATOR . $derivative_id : '');
$component_array['uuid'] = $this->uuidGenerator
->generate();
$component_array['configuration']['id'] = $cloned_plugin_id;
$cloned_components[] = SectionComponent::fromArray($component_array);
}
$third_party_settings = [];
foreach ($section
->getThirdPartyProviders() as $third_party_provider) {
$third_party_settings[$third_party_provider] = $section
->getThirdPartySettings($third_party_provider);
}
$cloned_section = new Section($section
->getLayoutId(), $section
->getLayoutSettings(), $cloned_components, $third_party_settings);
$cloned_entity
->appendSection($cloned_section);
}
$cloned_entity
->save();
}
return $cloned_entity;
}
}