Version 1
[yaffs-website] / web / core / modules / field / src / Plugin / migrate / source / d7 / FieldInstance.php
1 <?php
2
3 namespace Drupal\field\Plugin\migrate\source\d7;
4
5 use Drupal\migrate\Row;
6 use Drupal\migrate_drupal\Plugin\migrate\source\DrupalSqlBase;
7
8 /**
9  * Drupal 7 field instances source from database.
10  *
11  * @MigrateSource(
12  *   id = "d7_field_instance",
13  *   source_provider = "field"
14  * )
15  */
16 class FieldInstance extends DrupalSqlBase {
17
18   /**
19    * {@inheritdoc}
20    */
21   public function query() {
22     $query = $this->select('field_config_instance', 'fci')
23       ->fields('fci')
24       ->condition('fci.deleted', 0)
25       ->condition('fc.active', 1)
26       ->condition('fc.deleted', 0)
27       ->condition('fc.storage_active', 1)
28       ->fields('fc', ['type']);
29
30     $query->innerJoin('field_config', 'fc', 'fci.field_id = fc.id');
31     $query->addField('fc', 'data', 'field_data');
32
33     // Optionally filter by entity type and bundle.
34     if (isset($this->configuration['entity_type'])) {
35       $query->condition('fci.entity_type', $this->configuration['entity_type']);
36
37       if (isset($this->configuration['bundle'])) {
38         $query->condition('fci.bundle', $this->configuration['bundle']);
39       }
40     }
41
42     return $query;
43   }
44
45   /**
46    * {@inheritdoc}
47    */
48   public function fields() {
49     return [
50       'field_name' => $this->t('The machine name of field.'),
51       'entity_type' => $this->t('The entity type.'),
52       'bundle' => $this->t('The entity bundle.'),
53       'default_value' => $this->t('Default value'),
54       'instance_settings' => $this->t('Field instance settings.'),
55       'widget_settings' => $this->t('Widget settings.'),
56       'display_settings' => $this->t('Display settings.'),
57       'field_settings' => $this->t('Field settings.'),
58     ];
59   }
60
61   /**
62    * {@inheritdoc}
63    */
64   public function prepareRow(Row $row) {
65     $data = unserialize($row->getSourceProperty('data'));
66
67     $row->setSourceProperty('label', $data['label']);
68     $row->setSourceProperty('description', $data['description']);
69     $row->setSourceProperty('required', $data['required']);
70
71     $default_value = !empty($data['default_value']) ? $data['default_value'] : [];
72     if ($data['widget']['type'] == 'email_textfield' && $default_value) {
73       $default_value[0]['value'] = $default_value[0]['email'];
74       unset($default_value[0]['email']);
75     }
76     $row->setSourceProperty('default_value', $default_value);
77
78     // Settings.
79     $row->setSourceProperty('instance_settings', $data['settings']);
80     $row->setSourceProperty('widget_settings', $data['widget']);
81     $row->setSourceProperty('display_settings', $data['display']);
82
83     // This is for parity with the d6_field_instance plugin.
84     $row->setSourceProperty('widget_type', $data['widget']['type']);
85
86     $field_data = unserialize($row->getSourceProperty('field_data'));
87     $row->setSourceProperty('field_settings', $field_data['settings']);
88
89     $translatable = FALSE;
90     if ($row->getSourceProperty('entity_type') == 'node') {
91       // language_content_type_[bundle] may be
92       //   - 0: no language support
93       //   - 1: language assignment support
94       //   - 2: node translation support
95       //   - 4: entity translation support
96       if ($this->variableGet('language_content_type_' . $row->getSourceProperty('bundle'), 0) == 2) {
97         $translatable = TRUE;
98       }
99     }
100     else {
101       // This is not a node entity. Get the translatable value from the source
102       // field_config table.
103       $data = unserialize($row->getSourceProperty('field_data'));
104       $translatable = $data['translatable'];
105     }
106     $row->setSourceProperty('translatable', $translatable);
107
108     return parent::prepareRow($row);
109   }
110
111   /**
112    * {@inheritdoc}
113    */
114   public function getIds() {
115     return [
116       'entity_type' => [
117         'type' => 'string',
118         'alias' => 'fci',
119       ],
120       'bundle' => [
121         'type' => 'string',
122         'alias' => 'fci',
123       ],
124       'field_name' => [
125         'type' => 'string',
126         'alias' => 'fci',
127       ],
128     ];
129   }
130
131 }