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