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