Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / web / core / modules / field / src / Plugin / migrate / source / d7 / Field.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 source from database.
10  *
11  * @internal
12  *
13  * This class is marked as internal and should not be extended. Use
14  * Drupal\migrate_drupal\Plugin\migrate\source\DrupalSqlBase instead.
15  *
16  * @MigrateSource(
17  *   id = "d7_field",
18  *   source_module = "field_sql_storage"
19  * )
20  */
21 class Field extends DrupalSqlBase {
22
23   /**
24    * {@inheritdoc}
25    */
26   public function query() {
27     $query = $this->select('field_config', 'fc')
28       ->distinct()
29       ->fields('fc')
30       ->fields('fci', ['entity_type'])
31       ->condition('fc.active', 1)
32       ->condition('fc.storage_active', 1)
33       ->condition('fc.deleted', 0)
34       ->condition('fci.deleted', 0);
35     $query->join('field_config_instance', 'fci', 'fc.id = fci.field_id');
36
37     // If the Drupal 7 Title module is enabled, we don't want to migrate the
38     // fields it provides. The values of those fields will be migrated to the
39     // base fields they were replacing.
40     if ($this->moduleExists('title')) {
41       $title_fields = [
42         'title_field',
43         'name_field',
44         'description_field',
45         'subject_field',
46       ];
47       $query->condition('fc.field_name', $title_fields, 'NOT IN');
48     }
49
50     return $query;
51   }
52
53   /**
54    * {@inheritdoc}
55    */
56   public function fields() {
57     return [
58       'id' => $this->t('The field ID.'),
59       'field_name' => $this->t('The field name.'),
60       'type' => $this->t('The field type.'),
61       'module' => $this->t('The module that implements the field type.'),
62       'active' => $this->t('The field status.'),
63       'storage_type' => $this->t('The field storage type.'),
64       'storage_module' => $this->t('The module that implements the field storage type.'),
65       'storage_active' => $this->t('The field storage status.'),
66       'locked' => $this->t('Locked'),
67       'data' => $this->t('The field data.'),
68       'cardinality' => $this->t('Cardinality'),
69       'translatable' => $this->t('Translatable'),
70       'deleted' => $this->t('Deleted'),
71       'instances' => $this->t('The field instances.'),
72     ];
73   }
74
75   /**
76    * {@inheritdoc}
77    */
78   public function prepareRow(Row $row, $keep = TRUE) {
79     foreach (unserialize($row->getSourceProperty('data')) as $key => $value) {
80       $row->setSourceProperty($key, $value);
81     }
82
83     $instances = $this->select('field_config_instance', 'fci')
84       ->fields('fci')
85       ->condition('field_name', $row->getSourceProperty('field_name'))
86       ->condition('entity_type', $row->getSourceProperty('entity_type'))
87       ->execute()
88       ->fetchAll();
89     $row->setSourceProperty('instances', $instances);
90
91     return parent::prepareRow($row);
92   }
93
94   /**
95    * {@inheritdoc}
96    */
97   public function getIds() {
98     return [
99       'field_name' => [
100         'type' => 'string',
101         'alias' => 'fc',
102       ],
103       'entity_type' => [
104         'type' => 'string',
105         'alias' => 'fci',
106       ],
107     ];
108   }
109
110 }