class ConfigEntityCloneBase in Entity Clone 8
Class ConfigEntityCloneBase.
Hierarchy
- class \Drupal\entity_clone\EntityClone\Config\ConfigEntityCloneBase implements EntityHandlerInterface, EntityCloneInterface
Expanded class hierarchy of ConfigEntityCloneBase
1 file declares its use of ConfigEntityCloneBase
- entity_clone.module in ./
entity_clone.module - Contains entity_clone.module.
File
- src/
EntityClone/ Config/ ConfigEntityCloneBase.php, line 15
Namespace
Drupal\entity_clone\EntityClone\ConfigView source
class ConfigEntityCloneBase implements EntityHandlerInterface, EntityCloneInterface {
/**
* The entity type manager.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;
/**
* The entity type ID.
*
* @var string
*/
protected $entityTypeId;
/**
* Constructs a new ConfigEntityCloneBase.
*
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* The entity type manager.
* @param string $entity_type_id
* The entity type ID.
*/
public function __construct(EntityTypeManagerInterface $entity_type_manager, $entity_type_id) {
$this->entityTypeManager = $entity_type_manager;
$this->entityTypeId = $entity_type_id;
}
/**
* {@inheritdoc}
*/
public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) {
return new static($container
->get('entity_type.manager'), $entity_type
->id());
}
/**
* {@inheritdoc}
*/
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;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
ConfigEntityCloneBase:: |
protected | property | The entity type ID. | |
ConfigEntityCloneBase:: |
protected | property | The entity type manager. | |
ConfigEntityCloneBase:: |
public | function |
Clone an entity. Overrides EntityCloneInterface:: |
4 |
ConfigEntityCloneBase:: |
public static | function |
Instantiates a new instance of this entity handler. Overrides EntityHandlerInterface:: |
1 |
ConfigEntityCloneBase:: |
public | function | Constructs a new ConfigEntityCloneBase. | 1 |