diff --git a/clone.patch b/clone.patch new file mode 100644 index 0000000..e69de29 diff --git a/entity_clone.services.yml b/entity_clone.services.yml index 20cf5db..17f4537 100644 --- a/entity_clone.services.yml +++ b/entity_clone.services.yml @@ -7,3 +7,5 @@ services: arguments: ['@entity_type.manager'] tags: - { name: event_subscriber } + entity_clone.clonable_field: + class: Drupal\entity_clone\EntityCloneClonableField diff --git a/src/EntityClone/Content/ContentEntityCloneBase.php b/src/EntityClone/Content/ContentEntityCloneBase.php index 449ac8f..08110d3 100644 --- a/src/EntityClone/Content/ContentEntityCloneBase.php +++ b/src/EntityClone/Content/ContentEntityCloneBase.php @@ -7,10 +7,10 @@ use Drupal\Core\Entity\EntityInterface; use Drupal\Core\Entity\EntityTypeInterface; use Drupal\Core\Entity\EntityTypeManagerInterface; use Drupal\Core\Entity\FieldableEntityInterface; -use Drupal\Core\Field\FieldDefinitionInterface; use Drupal\Core\Field\FieldConfigInterface; use Drupal\Core\Field\FieldItemListInterface; use Drupal\entity_clone\EntityClone\EntityCloneInterface; +use Drupal\entity_clone\EntityCloneClonableField; use Symfony\Component\DependencyInjection\ContainerInterface; /** @@ -33,16 +33,26 @@ class ContentEntityCloneBase implements EntityHandlerInterface, EntityCloneInter protected $entityTypeId; /** + * The entity clone clonable field service. + * + * @var \Drupal\entity_clone\EntityCloneClonableField + */ + protected $entityCloneClonableField; + + /** * Constructs a new ContentEntityCloneBase. * * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager * The entity type manager. * @param string $entity_type_id * The entity type ID. + * * @param \Drupal\entity_clone\EntityCloneClonableField $entity_clone_clonable_field + * The entity clone clonable field service. */ - public function __construct(EntityTypeManagerInterface $entity_type_manager, $entity_type_id) { + public function __construct(EntityTypeManagerInterface $entity_type_manager, $entity_type_id, EntityCloneClonableField $entity_clone_clonable_field) { $this->entityTypeManager = $entity_type_manager; $this->entityTypeId = $entity_type_id; + $this->entityCloneClonableField = $entity_clone_clonable_field; } /** @@ -51,7 +61,8 @@ class ContentEntityCloneBase implements EntityHandlerInterface, EntityCloneInter public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) { return new static( $container->get('entity_type.manager'), - $entity_type->id() + $entity_type->id(), + $container->get('entity_clone.clonable_field') ); } @@ -64,12 +75,9 @@ class ContentEntityCloneBase implements EntityHandlerInterface, EntityCloneInter $already_cloned[$entity->getEntityTypeId()][$entity->id()] = $cloned_entity; if ($cloned_entity instanceof FieldableEntityInterface && $entity instanceof FieldableEntityInterface) { foreach ($cloned_entity->getFieldDefinitions() as $field_id => $field_definition) { - if ($this->fieldIsClonable($field_definition)) { - $field = $entity->get($field_id); - /** @var \Drupal\Core\Field\Plugin\Field\FieldType\EntityReferenceItem $value */ - if ($field->count() > 0) { - $cloned_entity->set($field_id, $this->cloneReferencedEntities($field, $field_definition, $properties, $already_cloned)); - } + $field = $entity->get($field_id); + if ($this->entityCloneClonableField->isClonable($field_definition, $field)) { + $cloned_entity->set($field_id, $this->cloneReferencedEntities($field, $field_definition, $properties, $already_cloned)); } } } @@ -80,28 +88,6 @@ class ContentEntityCloneBase implements EntityHandlerInterface, EntityCloneInter } /** - * Determines if a field is clonable. - * - * @param \Drupal\Core\Field\FieldDefinitionInterface $field_definition - * The field definition. - * - * @return bool - * TRUE if th field is clonable; FALSE otherwise. - */ - protected function fieldIsClonable(FieldDefinitionInterface $field_definition) { - $clonable_field_types = [ - 'entity_reference', - 'entity_reference_revisions', - ]; - - $type_is_clonable = in_array($field_definition->getType(), $clonable_field_types, TRUE); - if (($field_definition instanceof FieldConfigInterface) && $type_is_clonable) { - return TRUE; - } - return FALSE; - } - - /** * Sets the cloned entity's label. * * @param \Drupal\Core\Entity\EntityInterface $original_entity diff --git a/src/EntityClone/Content/ContentEntityCloneFormBase.php b/src/EntityClone/Content/ContentEntityCloneFormBase.php index a5f0a78..74fe8e0 100644 --- a/src/EntityClone/Content/ContentEntityCloneFormBase.php +++ b/src/EntityClone/Content/ContentEntityCloneFormBase.php @@ -13,6 +13,7 @@ use Drupal\Core\Field\FieldItemListInterface; use Drupal\Core\Form\FormStateInterface; use Drupal\Core\StringTranslation\TranslationManager; use Drupal\entity_clone\EntityClone\EntityCloneFormInterface; +use Drupal\entity_clone\EntityCloneClonableField; use Drupal\entity_clone\EntityCloneSettingsManager; use Symfony\Component\DependencyInjection\ContainerInterface; @@ -50,6 +51,13 @@ class ContentEntityCloneFormBase implements EntityHandlerInterface, EntityCloneF protected $discoveredEntities = []; /** + * The entity clone clonable field service. + * + * @var \Drupal\entity_clone\EntityCloneClonableField + */ + protected $entityCloneClonableField; + + /** * Constructs a new ContentEntityCloneFormBase. * * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager @@ -62,11 +70,13 @@ class ContentEntityCloneFormBase implements EntityHandlerInterface, EntityCloneF public function __construct( EntityTypeManagerInterface $entity_type_manager, TranslationManager $translation_manager, - EntityCloneSettingsManager $entity_clone_settings_manager + EntityCloneSettingsManager $entity_clone_settings_manager, + EntityCloneClonableField $entity_clone_clonable_field ) { $this->entityTypeManager = $entity_type_manager; $this->translationManager = $translation_manager; $this->entityCloneSettingsManager = $entity_clone_settings_manager; + $this->entityCloneClonableField = $entity_clone_clonable_field; } /** @@ -76,7 +86,8 @@ class ContentEntityCloneFormBase implements EntityHandlerInterface, EntityCloneF return new static( $container->get('entity_type.manager'), $container->get('string_translation'), - $container->get('entity_clone.settings.manager') + $container->get('entity_clone.settings.manager'), + $container->get('entity_clone.clonable_field') ); } @@ -91,12 +102,9 @@ class ContentEntityCloneFormBase implements EntityHandlerInterface, EntityCloneF if ($entity instanceof FieldableEntityInterface) { $discovered_entities[$entity->getEntityTypeId()][$entity->id()] = $entity; foreach ($entity->getFieldDefinitions() as $field_id => $field_definition) { - if ($field_definition instanceof FieldConfigInterface && in_array($field_definition->getType(), ['entity_reference', 'entity_reference_revisions'], TRUE)) { - $field = $entity->get($field_id); - /** @var \Drupal\Core\Field\Plugin\Field\FieldType\EntityReferenceItem $value */ - if ($field->count() > 0) { - $form['recursive'] = array_merge($form['recursive'], $this->getRecursiveFormElement($field_definition, $field_id, $field, $discovered_entities)); - } + $field = $entity->get($field_id); + if ($this->entityCloneClonableField->isClonable($field_definition, $field)) { + $form['recursive'] = array_merge($form['recursive'], $this->getRecursiveFormElement($field_definition, $field_id, $field, $discovered_entities)); } } diff --git a/src/EntityCloneClonableField.php b/src/EntityCloneClonableField.php new file mode 100644 index 0000000..7760e68 --- /dev/null +++ b/src/EntityCloneClonableField.php @@ -0,0 +1,29 @@ +count() > 0; + } + +}