ae6d6e9bad2f809b45e6d701a8a05d9820a7d3d8
[yaffs-website] / web / core / modules / migrate / src / Plugin / migrate / destination / Entity.php
1 <?php
2
3 namespace Drupal\migrate\Plugin\migrate\destination;
4
5 use Drupal\Component\Plugin\DependentPluginInterface;
6 use Drupal\Core\Entity\DependencyTrait;
7 use Drupal\Core\Entity\EntityStorageInterface;
8 use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
9 use Drupal\migrate\Plugin\MigrationInterface;
10 use Drupal\migrate\Row;
11 use Symfony\Component\DependencyInjection\ContainerInterface;
12
13 /**
14  * Provides entity destination plugin.
15  *
16  * @MigrateDestination(
17  *   id = "entity",
18  *   deriver = "Drupal\migrate\Plugin\Derivative\MigrateEntity"
19  * )
20  */
21 abstract class Entity extends DestinationBase implements ContainerFactoryPluginInterface, DependentPluginInterface {
22
23   use DependencyTrait;
24
25   /**
26    * The entity storage.
27    *
28    * @var \Drupal\Core\Entity\EntityStorageInterface
29    */
30   protected $storage;
31
32   /**
33    * The list of the bundles of this entity type.
34    *
35    * @var array
36    */
37   protected $bundles;
38
39   /**
40    * Construct a new entity.
41    *
42    * @param array $configuration
43    *   A configuration array containing information about the plugin instance.
44    * @param string $plugin_id
45    *   The plugin_id for the plugin instance.
46    * @param mixed $plugin_definition
47    *   The plugin implementation definition.
48    * @param \Drupal\migrate\Plugin\MigrationInterface $migration
49    *   The migration.
50    * @param \Drupal\Core\Entity\EntityStorageInterface $storage
51    *   The storage for this entity type.
52    * @param array $bundles
53    *   The list of bundles this entity type has.
54    */
55   public function __construct(array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration, EntityStorageInterface $storage, array $bundles) {
56     parent::__construct($configuration, $plugin_id, $plugin_definition, $migration);
57     $this->storage = $storage;
58     $this->bundles = $bundles;
59     $this->supportsRollback = TRUE;
60   }
61
62   /**
63    * {@inheritdoc}
64    */
65   public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration = NULL) {
66     $entity_type_id = static::getEntityTypeId($plugin_id);
67     return new static(
68       $configuration,
69       $plugin_id,
70       $plugin_definition,
71       $migration,
72       $container->get('entity.manager')->getStorage($entity_type_id),
73       array_keys($container->get('entity.manager')->getBundleInfo($entity_type_id))
74     );
75   }
76
77   /**
78    * Finds the entity type from configuration or plugin ID.
79    *
80    * @param string $plugin_id
81    *   The plugin ID.
82    *
83    * @return string
84    *   The entity type.
85    */
86   protected static function getEntityTypeId($plugin_id) {
87     // Remove "entity:".
88     return substr($plugin_id, 7);
89   }
90
91   /**
92    * Gets the bundle for the row taking into account the default.
93    *
94    * @param \Drupal\migrate\Row $row
95    *   The current row we're importing.
96    *
97    * @return string
98    *   The bundle for this row.
99    */
100   public function getBundle(Row $row) {
101     $default_bundle = isset($this->configuration['default_bundle']) ? $this->configuration['default_bundle'] : '';
102     $bundle_key = $this->getKey('bundle');
103     return $row->getDestinationProperty($bundle_key) ?: $default_bundle;
104   }
105
106   /**
107    * {@inheritdoc}
108    */
109   public function fields(MigrationInterface $migration = NULL) {
110     // TODO: Implement fields() method.
111   }
112
113   /**
114    * Creates or loads an entity.
115    *
116    * @param \Drupal\migrate\Row $row
117    *   The row object.
118    * @param array $old_destination_id_values
119    *   The old destination IDs.
120    *
121    * @return \Drupal\Core\Entity\EntityInterface
122    *   The entity we are importing into.
123    */
124   protected function getEntity(Row $row, array $old_destination_id_values) {
125     $entity_id = reset($old_destination_id_values) ?: $this->getEntityId($row);
126     if (!empty($entity_id) && ($entity = $this->storage->load($entity_id))) {
127       // Allow updateEntity() to change the entity.
128       $entity = $this->updateEntity($entity, $row) ?: $entity;
129     }
130     else {
131       // Attempt to ensure we always have a bundle.
132       if ($bundle = $this->getBundle($row)) {
133         $row->setDestinationProperty($this->getKey('bundle'), $bundle);
134       }
135
136       // Stubs might need some required fields filled in.
137       if ($row->isStub()) {
138         $this->processStubRow($row);
139       }
140       $entity = $this->storage->create($row->getDestination());
141       $entity->enforceIsNew();
142     }
143     return $entity;
144   }
145
146   /**
147    * Gets the entity ID of the row.
148    *
149    * @param \Drupal\migrate\Row $row
150    *   The row of data.
151    *
152    * @return string
153    *   The entity ID for the row that we are importing.
154    */
155   protected function getEntityId(Row $row) {
156     return $row->getDestinationProperty($this->getKey('id'));
157   }
158
159   /**
160    * Returns a specific entity key.
161    *
162    * @param string $key
163    *   The name of the entity key to return.
164    *
165    * @return string|bool
166    *   The entity key, or FALSE if it does not exist.
167    *
168    * @see \Drupal\Core\Entity\EntityTypeInterface::getKeys()
169    */
170   protected function getKey($key) {
171     return $this->storage->getEntityType()->getKey($key);
172   }
173
174   /**
175    * {@inheritdoc}
176    */
177   public function rollback(array $destination_identifier) {
178     // Delete the specified entity from Drupal if it exists.
179     $entity = $this->storage->load(reset($destination_identifier));
180     if ($entity) {
181       $entity->delete();
182     }
183   }
184
185   /**
186    * {@inheritdoc}
187    */
188   public function calculateDependencies() {
189     $this->addDependency('module', $this->storage->getEntityType()->getProvider());
190     return $this->dependencies;
191   }
192
193 }