6433ed33d7cde94182f8d7a0be4e736d2cd06b13
[yaffs-website] / web / core / modules / text / src / Plugin / migrate / field / d7 / TextField.php
1 <?php
2
3 namespace Drupal\text\Plugin\migrate\field\d7;
4
5 use Drupal\migrate\Row;
6 use Drupal\migrate\MigrateSkipRowException;
7 use Drupal\migrate_drupal\Plugin\migrate\field\FieldPluginBase;
8
9 /**
10  * @MigrateField(
11  *   id = "d7_text",
12  *   type_map = {
13  *     "text" = "text",
14  *     "text_long" = "text_long",
15  *     "text_with_summary" = "text_with_summary"
16  *   },
17  *   core = {7},
18  *   source_module = "text",
19  *   destination_module = "text",
20  * )
21  */
22 class TextField extends FieldPluginBase {
23
24   /**
25    * {@inheritdoc}
26    */
27   public function getFieldFormatterType(Row $row) {
28     $field_type = $this->getFieldType($row);
29     $formatter_type = $row->getSourceProperty('formatter/type');
30
31     switch ($field_type) {
32       case 'string':
33         $formatter_type = str_replace('text_default', 'string', $formatter_type);
34         break;
35       case 'string_long':
36         $formatter_type = str_replace('text_default', 'basic_string', $formatter_type);
37         break;
38     }
39
40     return $formatter_type;
41   }
42
43   /**
44    * {@inheritdoc}
45    */
46   public function getFieldWidgetType(Row $row) {
47     $field_type = $this->getFieldType($row);
48     $widget_type = $row->getSourceProperty('widget/type');
49
50     switch ($field_type) {
51       case 'string':
52         $widget_type = str_replace('text_textfield', 'string_textfield', $widget_type);
53         break;
54       case 'string_long':
55         $widget_type = str_replace('text_textarea', 'string_textarea', $widget_type);
56         break;
57     }
58
59     return $widget_type;
60   }
61
62   /**
63    * {@inheritdoc}
64    */
65   public function getFieldType(Row $row) {
66     $type = $row->getSourceProperty('type');
67     $plain_text = FALSE;
68     $filtered_text = FALSE;
69
70     foreach ($row->getSourceProperty('instances') as $instance) {
71       // Check if this field has plain text instances, filtered text instances,
72       // or both.
73       $data = unserialize($instance['data']);
74       switch ($data['settings']['text_processing']) {
75         case '0':
76           $plain_text = TRUE;
77           break;
78         case '1':
79           $filtered_text = TRUE;
80           break;
81       }
82     }
83
84     if (in_array($type, ['text', 'text_long'])) {
85       // If a text or text_long field has only plain text instances, migrate it
86       // to a string or string_long field.
87       if ($plain_text && !$filtered_text) {
88         $type = str_replace(['text', 'text_long'], ['string', 'string_long'], $type);
89       }
90       // If a text or text_long field has both plain text and filtered text
91       // instances, skip the row.
92       elseif ($plain_text && $filtered_text) {
93         $field_name = $row->getSourceProperty('field_name');
94         throw new MigrateSkipRowException("Can't migrate source field $field_name configured with both plain text and filtered text processing. See https://www.drupal.org/docs/8/upgrade/known-issues-when-upgrading-from-drupal-6-or-7-to-drupal-8#plain-text");
95       }
96     }
97     elseif ($type == 'text_with_summary' && $plain_text) {
98       // If a text_with_summary field has plain text instances, skip the row
99       // since there's no such thing as a string_with_summary field.
100       $field_name = $row->getSourceProperty('field_name');
101       throw new MigrateSkipRowException("Can't migrate source field $field_name of type text_with_summary configured with plain text processing. See https://www.drupal.org/docs/8/upgrade/known-issues-when-upgrading-from-drupal-6-or-7-to-drupal-8#plain-text");
102     }
103
104     return $type;
105   }
106
107 }