dc115abc99f6dd87dd8974e489f95ffa5dbc2b50
[yaffs-website] / web / core / modules / migrate_drupal / src / Plugin / migrate / field / FieldPluginBase.php
1 <?php
2
3 namespace Drupal\migrate_drupal\Plugin\migrate\field;
4
5 use Drupal\Core\Plugin\PluginBase;
6 use Drupal\migrate\Plugin\MigrationInterface;
7 use Drupal\migrate\Row;
8 use Drupal\migrate_drupal\Plugin\MigrateFieldInterface;
9
10 /**
11  * The base class for all field plugins.
12  *
13  * @see \Drupal\migrate\Plugin\MigratePluginManager
14  * @see \Drupal\migrate_drupal\Annotation\MigrateField
15  * @see \Drupal\migrate_drupal\Plugin\MigrateFieldInterface
16  * @see plugin_api
17  *
18  * @ingroup migration
19  */
20 abstract class FieldPluginBase extends PluginBase implements MigrateFieldInterface {
21
22   /**
23    * {@inheritdoc}
24    */
25   public function processField(MigrationInterface $migration) {
26     $process[0]['map'][$this->pluginId][$this->pluginId] = $this->pluginId;
27     $migration->mergeProcessOfProperty('type', $process);
28   }
29
30   /**
31    * {@inheritdoc}
32    */
33   public function processFieldInstance(MigrationInterface $migration) {
34     // Nothing to do by default with field instances.
35   }
36
37   /**
38    * {@inheritdoc}
39    */
40   public function processFieldWidget(MigrationInterface $migration) {
41     $process = [];
42     foreach ($this->getFieldWidgetMap() as $source_widget => $destination_widget) {
43       $process['type']['map'][$source_widget] = $destination_widget;
44     }
45     $migration->mergeProcessOfProperty('options/type', $process);
46   }
47
48   /**
49    * {@inheritdoc}
50    */
51   public function getFieldFormatterType(Row $row) {
52     return $row->getSourceProperty('formatter/type');
53   }
54
55   /**
56    * {@inheritdoc}
57    */
58   public function getFieldFormatterMap() {
59     return [];
60   }
61
62   /**
63    * {@inheritdoc}
64    */
65   public function getFieldWidgetType(Row $row) {
66     return $row->getSourceProperty('widget/type');
67   }
68
69   /**
70    * {@inheritdoc}
71    */
72   public function getFieldWidgetMap() {
73     // By default, use the plugin ID for the widget types.
74     return [
75       $this->pluginId => $this->pluginId . '_default',
76     ];
77   }
78
79   /**
80    * {@inheritdoc}
81    */
82   public function processFieldFormatter(MigrationInterface $migration) {
83     $process = [];
84
85     // Some migrate field plugin IDs are prefixed with 'd6_' or 'd7_'. Since the
86     // plugin ID is used in the static map as the module name, we have to remove
87     // this prefix from the plugin ID.
88     $plugin_id = preg_replace('/d[67]_/', '', $this->pluginId);
89     foreach ($this->getFieldFormatterMap() as $source_format => $destination_format) {
90       $process[0]['map'][$plugin_id][$source_format] = $destination_format;
91     }
92     $migration->mergeProcessOfProperty('options/type', $process);
93   }
94
95   /**
96    * {@inheritdoc}
97    */
98   public function processFieldValues(MigrationInterface $migration, $field_name, $data) {
99     $process = [
100       'plugin' => 'get',
101       'source' => $field_name,
102     ];
103     $migration->mergeProcessOfProperty($field_name, $process);
104   }
105
106   /**
107    * {@inheritdoc}
108    */
109   public function getFieldType(Row $row) {
110     $field_type = $row->getSourceProperty('type');
111
112     if (isset($this->pluginDefinition['type_map'][$field_type])) {
113       return $this->pluginDefinition['type_map'][$field_type];
114     }
115     else {
116       return $field_type;
117     }
118   }
119
120 }