d1da3f43f4d4326c78cc7bc6ae9454b1d759d159
[yaffs-website] / web / core / modules / text / src / Plugin / migrate / field / d6 / TextField.php
1 <?php
2
3 namespace Drupal\text\Plugin\migrate\field\d6;
4
5 use Drupal\migrate\Plugin\MigrationInterface;
6 use Drupal\migrate\Row;
7 use Drupal\migrate_drupal\Plugin\migrate\field\FieldPluginBase;
8
9 /**
10  * @MigrateField(
11  *   id = "d6_text",
12  *   type_map = {
13  *     "text" = "text",
14  *     "text_long" = "text_long",
15  *     "text_with_summary" = "text_with_summary"
16  *   },
17  *   core = {6}
18  * )
19  */
20 class TextField extends FieldPluginBase {
21
22   /**
23    * {@inheritdoc}
24    */
25   public function getFieldWidgetMap() {
26     return [
27       'text_textfield' => 'text_textfield',
28     ];
29   }
30
31   /**
32    * {@inheritdoc}
33    */
34   public function getFieldFormatterMap() {
35     return [
36       'default' => 'text_default',
37       'trimmed' => 'text_trimmed',
38       'plain' => 'basic_string',
39     ];
40   }
41
42   /**
43    * {@inheritdoc}
44    */
45   public function processFieldValues(MigrationInterface $migration, $field_name, $field_info) {
46     $widget_type = isset($field_info['widget_type']) ? $field_info['widget_type'] : $field_info['widget']['type'];
47
48     if ($widget_type == 'optionwidgets_onoff') {
49       $process = [
50         'value' => [
51           'plugin' => 'static_map',
52           'source' => 'value',
53           'default_value' => 0,
54         ],
55       ];
56
57       $checked_value = explode("\n", $field_info['global_settings']['allowed_values'])[1];
58       if (strpos($checked_value, '|') !== FALSE) {
59         $checked_value = substr($checked_value, 0, strpos($checked_value, '|'));
60       }
61       $process['value']['map'][$checked_value] = 1;
62     }
63     else {
64       // See \Drupal\migrate_drupal\Plugin\migrate\source\d6\User::baseFields(),
65       // signature_format for an example of the YAML that represents this
66       // process array.
67       $process = [
68         'value' => 'value',
69         'format' => [
70           [
71             'plugin' => 'static_map',
72             'bypass' => TRUE,
73             'source' => 'format',
74             'map' => [0 => NULL],
75           ],
76           [
77             'plugin' => 'skip_on_empty',
78             'method' => 'process',
79           ],
80           [
81             'plugin' => 'migration',
82             'migration' => [
83               'd6_filter_format',
84               'd7_filter_format',
85             ],
86             'source' => 'format',
87           ],
88         ],
89       ];
90     }
91
92     $process = [
93       'plugin' => 'sub_process',
94       'source' => $field_name,
95       'process' => $process,
96     ];
97     $migration->setProcessOfProperty($field_name, $process);
98   }
99
100   /**
101    * {@inheritdoc}
102    */
103   public function getFieldType(Row $row) {
104     $widget_type = $row->getSourceProperty('widget_type');
105     $settings = $row->getSourceProperty('global_settings');
106
107     if ($widget_type == 'text_textfield') {
108       $field_type = $settings['text_processing'] ? 'text' : 'string';
109       if (empty($settings['max_length']) || $settings['max_length'] > 255) {
110         $field_type .= '_long';
111       }
112       return $field_type;
113     }
114
115     if ($widget_type == 'text_textarea') {
116       $field_type = $settings['text_processing'] ? 'text_long' : 'string_long';
117       return $field_type;
118     }
119
120     switch ($widget_type) {
121       case 'optionwidgets_buttons':
122       case 'optionwidgets_select':
123         return 'list_string';
124       case 'optionwidgets_onoff':
125         return 'boolean';
126       default:
127         return parent::getFieldType($row);
128     }
129   }
130
131 }