cab02040428abddf1591d75e4478c9506ffd4a9e
[yaffs-website] / web / core / modules / field / src / Plugin / migrate / source / d6 / Field.php
1 <?php
2
3 namespace Drupal\field\Plugin\migrate\source\d6;
4
5 use Drupal\migrate\Row;
6 use Drupal\migrate_drupal\Plugin\migrate\source\DrupalSqlBase;
7
8 /**
9  * Drupal 6 field source from database.
10  *
11  * @MigrateSource(
12  *   id = "d6_field",
13  *   source_module = "content"
14  * )
15  */
16 class Field extends DrupalSqlBase {
17
18   /**
19    * {@inheritdoc}
20    */
21   public function query() {
22     $query = $this->select('content_node_field', 'cnf')
23       ->fields('cnf', [
24         'field_name',
25         'type',
26         'global_settings',
27         'required',
28         'multiple',
29         'db_storage',
30         'module',
31         'db_columns',
32         'active',
33         'locked',
34       ])
35       ->distinct();
36     // Only import fields which are actually being used.
37     $query->innerJoin('content_node_field_instance', 'cnfi', 'cnfi.field_name = cnf.field_name');
38
39     return $query;
40   }
41
42   /**
43    * {@inheritdoc}
44    */
45   public function fields() {
46     return [
47       'field_name' => $this->t('Field name'),
48       'type' => $this->t('Type (text, integer, ....)'),
49       'widget_type' => $this->t('An instance-specific widget type'),
50       'global_settings' => $this->t('Global settings. Shared with every field instance.'),
51       'required' => $this->t('Required'),
52       'multiple' => $this->t('Multiple'),
53       'db_storage' => $this->t('DB storage'),
54       'module' => $this->t('Module'),
55       'db_columns' => $this->t('DB Columns'),
56       'active' => $this->t('Active'),
57       'locked' => $this->t('Locked'),
58     ];
59   }
60
61   /**
62    * {@inheritdoc}
63    */
64   public function prepareRow(Row $row) {
65     // The instance widget_type helps determine what D8 field type we'll use.
66     // Identify the distinct widget_types being used in D6.
67     $widget_types = $this->select('content_node_field_instance', 'cnfi')
68       ->fields('cnfi', ['widget_type'])
69       ->condition('field_name', $row->getSourceProperty('field_name'))
70       ->distinct()
71       ->orderBy('widget_type')
72       ->execute()
73       ->fetchCol();
74     // Arbitrarily use the first widget_type - if there are multiples, let the
75     // migrator know.
76     $row->setSourceProperty('widget_type', $widget_types[0]);
77     if (count($widget_types) > 1) {
78       $this->migration->getIdMap()->saveMessage(
79         ['field_name' => $row->getSourceProperty('field_name')],
80         $this->t('Widget types @types are used in Drupal 6 field instances: widget type @selected_type applied to the Drupal 8 base field', [
81           '@types' => implode(', ', $widget_types),
82           '@selected_type' => $widget_types[0],
83         ])
84       );
85     }
86
87     // Unserialize data.
88     $global_settings = unserialize($row->getSourceProperty('global_settings'));
89     $db_columns = unserialize($row->getSourceProperty('db_columns'));
90     $row->setSourceProperty('global_settings', $global_settings);
91     $row->setSourceProperty('db_columns', $db_columns);
92     return parent::prepareRow($row);
93   }
94
95   /**
96    * {@inheritdoc}
97    */
98   public function getIds() {
99     $ids['field_name'] = [
100       'type' => 'string',
101       'alias' => 'cnf',
102     ];
103     return $ids;
104   }
105
106 }