You are here

public function ConfigEntityCloneBase::cloneEntity in Entity Clone 8

Clone an entity.

Parameters

\Drupal\Core\Entity\EntityInterface $entity: The entity.

\Drupal\Core\Entity\EntityInterface $cloned_entity: The cloned entity.

array $properties: All new properties to replace old.

Return value

\Drupal\Core\Entity\EntityInterface The new saved entity.

Overrides EntityCloneInterface::cloneEntity

4 calls to ConfigEntityCloneBase::cloneEntity()
ConfigWithFieldEntityClone::cloneEntity in src/EntityClone/Config/ConfigWithFieldEntityClone.php
Clone an entity.
FieldConfigEntityClone::cloneEntity in src/EntityClone/Config/FieldConfigEntityClone.php
Clone an entity.
LayoutBuilderEntityClone::cloneEntity in src/EntityClone/Config/LayoutBuilderEntityClone.php
Clone an entity.
MenuEntityClone::cloneEntity in src/EntityClone/Config/MenuEntityClone.php
Clone an entity.
4 methods override ConfigEntityCloneBase::cloneEntity()
ConfigWithFieldEntityClone::cloneEntity in src/EntityClone/Config/ConfigWithFieldEntityClone.php
Clone an entity.
FieldConfigEntityClone::cloneEntity in src/EntityClone/Config/FieldConfigEntityClone.php
Clone an entity.
LayoutBuilderEntityClone::cloneEntity in src/EntityClone/Config/LayoutBuilderEntityClone.php
Clone an entity.
MenuEntityClone::cloneEntity in src/EntityClone/Config/MenuEntityClone.php
Clone an entity.

File

src/EntityClone/Config/ConfigEntityCloneBase.php, line 57

Class

ConfigEntityCloneBase
Class ConfigEntityCloneBase.

Namespace

Drupal\entity_clone\EntityClone\Config

Code

public function cloneEntity(EntityInterface $entity, EntityInterface $cloned_entity, array $properties = []) {

  /** @var \Drupal\core\Config\Entity\ConfigEntityInterface $cloned_entity */
  $id_key = $this->entityTypeManager
    ->getDefinition($this->entityTypeId)
    ->getKey('id');
  $label_key = $this->entityTypeManager
    ->getDefinition($this->entityTypeId)
    ->getKey('label');

  // Set new entity properties.
  if (isset($properties['id'])) {
    if ($id_key) {
      $cloned_entity
        ->set($id_key, $properties['id']);
    }
    unset($properties['id']);
  }
  if (isset($properties['label'])) {
    if ($label_key) {
      $cloned_entity
        ->set($label_key, $properties['label']);
    }
    unset($properties['label']);
  }
  foreach ($properties as $key => $property) {
    $cloned_entity
      ->set($key, $property);
  }
  $cloned_entity
    ->save();
  return $cloned_entity;
}