1c94214fd5a79dca74fed929f5a65fd574807c8a
[yaffs-website] / 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  * Provides a destination plugin for migrating entity display components.
10  *
11  * Display modes provide different presentations for viewing ('view modes') or
12  * editing ('form modes') content. This destination plugin is an abstract base
13  * class for migrating fields and other components into view and form modes.
14  *
15  * @see \Drupal\migrate\Plugin\migrate\destination\PerComponentEntityDisplay
16  * @see \Drupal\migrate\Plugin\migrate\destination\PerComponentEntityFormDisplay
17  */
18 abstract class ComponentEntityDisplayBase extends DestinationBase {
19
20   const MODE_NAME = '';
21
22   /**
23    * {@inheritdoc}
24    */
25   public function import(Row $row, array $old_destination_id_values = []) {
26     $values = [];
27     // array_intersect_key() won't work because the order is important because
28     // this is also the return value.
29     foreach (array_keys($this->getIds()) as $id) {
30       $values[$id] = $row->getDestinationProperty($id);
31     }
32     $entity = $this->getEntity($values['entity_type'], $values['bundle'], $values[static::MODE_NAME]);
33     if (!$row->getDestinationProperty('hidden')) {
34       $entity->setComponent($values['field_name'], $row->getDestinationProperty('options') ?: []);
35     }
36     else {
37       $entity->removeComponent($values['field_name']);
38     }
39     $entity->save();
40     return array_values($values);
41   }
42
43   /**
44    * {@inheritdoc}
45    */
46   public function getIds() {
47     $ids['entity_type']['type'] = 'string';
48     $ids['bundle']['type'] = 'string';
49     $ids[static::MODE_NAME]['type'] = 'string';
50     $ids['field_name']['type'] = 'string';
51     return $ids;
52   }
53
54   /**
55    * {@inheritdoc}
56    */
57   public function fields(MigrationInterface $migration = NULL) {
58     // This is intentionally left empty.
59   }
60
61   /**
62    * Gets the entity.
63    *
64    * @param string $entity_type
65    *   The entity type to retrieve.
66    * @param string $bundle
67    *   The entity bundle.
68    * @param string $mode
69    *   The display mode.
70    *
71    * @return \Drupal\Core\Entity\Display\EntityDisplayInterface
72    *   The entity display object.
73    */
74   abstract protected function getEntity($entity_type, $bundle, $mode);
75
76 }