660248494e110a1327ee08b5d5345ed18cb9021b
[yaffs-website] / web / core / modules / migrate / src / Plugin / migrate / destination / ComponentEntityDisplayBase.php
1 <?php
2
3 namespace Drupal\migrate\Plugin\migrate\destination;
4
5 use Drupal\migrate\Plugin\MigrationInterface;
6 use Drupal\migrate\Row;
7
8 /**
9  * Defines the base abstract class for component entity display.
10  */
11 abstract class ComponentEntityDisplayBase extends DestinationBase {
12
13   const MODE_NAME = '';
14
15   /**
16    * {@inheritdoc}
17    */
18   public function import(Row $row, array $old_destination_id_values = []) {
19     $values = [];
20     // array_intersect_key() won't work because the order is important because
21     // this is also the return value.
22     foreach (array_keys($this->getIds()) as $id) {
23       $values[$id] = $row->getDestinationProperty($id);
24     }
25     $entity = $this->getEntity($values['entity_type'], $values['bundle'], $values[static::MODE_NAME]);
26     if (!$row->getDestinationProperty('hidden')) {
27       $entity->setComponent($values['field_name'], $row->getDestinationProperty('options') ?: []);
28     }
29     else {
30       $entity->removeComponent($values['field_name']);
31     }
32     $entity->save();
33     return array_values($values);
34   }
35
36   /**
37    * {@inheritdoc}
38    */
39   public function getIds() {
40     $ids['entity_type']['type'] = 'string';
41     $ids['bundle']['type'] = 'string';
42     $ids[static::MODE_NAME]['type'] = 'string';
43     $ids['field_name']['type'] = 'string';
44     return $ids;
45   }
46
47   /**
48    * {@inheritdoc}
49    */
50   public function fields(MigrationInterface $migration = NULL) {
51     // This is intentionally left empty.
52   }
53
54   /**
55    * Gets the entity.
56    *
57    * @param string $entity_type
58    *   The entity type to retrieve.
59    * @param string $bundle
60    *   The entity bundle.
61    * @param string $mode
62    *   The display mode.
63    *
64    * @return \Drupal\Core\Entity\Display\EntityDisplayInterface
65    *   The entity display object.
66    */
67   protected abstract function getEntity($entity_type, $bundle, $mode);
68
69 }