- First commit to issue fork.
- Open on Drupal.org βCore: 9.5.x + Environment: PHP 8.1 & MySQL 5.7last update
about 1 year ago Not currently mergeable. - @dieterholvoet opened merge request.
- Status changed to Needs review
about 1 year ago 9:36am 30 August 2023 - last update
about 1 year ago 27 pass, 6 fail - πΊπ¦Ukraine vselivanov Kyiv, Ukraine
I have this issue with cloning custom entity and fixed it with MR 53 and custom code.
With entity_clone 2.0.0-beta6 we have isClonable() method in src/EntityCloneClonableField.php which also checks
$field_definition instanceof FieldConfigInterface
But all custom reference fields are instance of BaseFieldDefinition class.
So we need additional changes to this method.I tried to remove this FieldConfigInterface check, but it adds a lot of not needed fields and caused recursions and "Circular reference detected" errors.
Finally I managed to clone custom entities with:
1. Apply patch from the MR 53 (attached it)
2. Extend EntityCloneClonableField class in the custom module and add hardcoded custom entity types check.Example with 2 files:
1. File modules/custom/my_module/src/MyModuleServiceProvider.php
<?php namespace Drupal\my_module; use Drupal\Core\DependencyInjection\ContainerBuilder; use Drupal\Core\DependencyInjection\ServiceProviderBase; /** * Overrides EntityCloneClonableField class. */ class MyModuleServiceProvider extends ServiceProviderBase { /** * {@inheritdoc} */ public function alter(ContainerBuilder $container) { if ($container->hasDefinition('entity_clone.clonable_field')) { /** @var \Symfony\Component\DependencyInjection\Definition $definition */ $definition = $container->getDefinition('entity_clone.clonable_field'); $definition->setClass('Drupal\my_module\EntityCloneClonableField'); } } }
2. File modules/custom/my_module/src/EntityCloneClonableField.php
<?php namespace Drupal\my_module; use Drupal\Core\Field\BaseFieldDefinition; use Drupal\Core\Field\EntityReferenceFieldItemListInterface; use Drupal\Core\Field\FieldDefinitionInterface; use Drupal\Core\Field\FieldItemListInterface; use Drupal\entity_clone\EntityCloneClonableField as EntityCloneClonableFieldSource; /** * Manage entity clone clonable field. */ class EntityCloneClonableField extends EntityCloneClonableFieldSource { /** * {@inheritDoc} */ public function isClonable(FieldDefinitionInterface $field_definition, FieldItemListInterface $field): bool { $isClonable = parent::isClonable($field_definition, $field); // Add custom entities fields as clonable. $customClonableEntityTypes = [ 'custom_entity_1', 'custom_entity_2', ]; if (!$isClonable && in_array($field_definition->getTargetEntityTypeId(), $customClonableEntityTypes) && $field_definition instanceof BaseFieldDefinition && $field instanceof EntityReferenceFieldItemListInterface && $field->count() > 0) { return TRUE; } return $isClonable; } }
There is one more place, where the field key variable needs to be changed: /src/EntityClone/Content/ContentEntityCloneFormBase.php line 205
Or in human terms: The part of the content clone form, which displays referenced entities for which cloning is enforced.