Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / web / core / modules / migrate_drupal / src / Plugin / migrate / source / d7 / FieldableEntity.php
1 <?php
2
3 namespace Drupal\migrate_drupal\Plugin\migrate\source\d7;
4
5 use Drupal\migrate_drupal\Plugin\migrate\source\DrupalSqlBase;
6
7 /**
8  * Base class for D7 source plugins which need to collect field values from
9  * the Field API.
10  */
11 abstract class FieldableEntity extends DrupalSqlBase {
12
13   /**
14    * Returns all non-deleted field instances attached to a specific entity type.
15    *
16    * @param string $entity_type
17    *   The entity type ID.
18    * @param string|null $bundle
19    *   (optional) The bundle.
20    *
21    * @return array[]
22    *   The field instances, keyed by field name.
23    */
24   protected function getFields($entity_type, $bundle = NULL) {
25     $query = $this->select('field_config_instance', 'fci')
26       ->fields('fci')
27       ->condition('fci.entity_type', $entity_type)
28       ->condition('fci.bundle', isset($bundle) ? $bundle : $entity_type)
29       ->condition('fci.deleted', 0);
30
31     // Join the 'field_config' table and add the 'translatable' setting to the
32     // query.
33     $query->leftJoin('field_config', 'fc', 'fci.field_id = fc.id');
34     $query->addField('fc', 'translatable');
35
36     return $query->execute()->fetchAllAssoc('field_name');
37   }
38
39   /**
40    * Retrieves field values for a single field of a single entity.
41    *
42    * @param string $entity_type
43    *   The entity type.
44    * @param string $field
45    *   The field name.
46    * @param int $entity_id
47    *   The entity ID.
48    * @param int|null $revision_id
49    *   (optional) The entity revision ID.
50    * @param string $language
51    *   (optional) The field language.
52    *
53    * @return array
54    *   The raw field values, keyed by delta.
55    */
56   protected function getFieldValues($entity_type, $field, $entity_id, $revision_id = NULL, $language = NULL) {
57     $table = (isset($revision_id) ? 'field_revision_' : 'field_data_') . $field;
58     $query = $this->select($table, 't')
59       ->fields('t')
60       ->condition('entity_type', $entity_type)
61       ->condition('entity_id', $entity_id)
62       ->condition('deleted', 0);
63     if (isset($revision_id)) {
64       $query->condition('revision_id', $revision_id);
65     }
66     // Add 'language' as a query condition if it has been defined by Entity
67     // Translation.
68     if ($language) {
69       $query->condition('language', $language);
70     }
71     $values = [];
72     foreach ($query->execute() as $row) {
73       foreach ($row as $key => $value) {
74         $delta = $row['delta'];
75         if (strpos($key, $field) === 0) {
76           $column = substr($key, strlen($field) + 1);
77           $values[$delta][$column] = $value;
78         }
79       }
80     }
81     return $values;
82   }
83
84   /**
85    * Checks if an entity type uses Entity Translation.
86    *
87    * @param string $entity_type
88    *   The entity type.
89    *
90    * @return bool
91    *   Whether the entity type uses entity translation.
92    */
93   protected function isEntityTranslatable($entity_type) {
94     return in_array($entity_type, $this->variableGet('entity_translation_entity_types', []), TRUE);
95   }
96
97   /**
98    * Gets an entity source language from the 'entity_translation' table.
99    *
100    * @param string $entity_type
101    *   The entity type.
102    * @param int $entity_id
103    *   The entity ID.
104    *
105    * @return string|bool
106    *   The entity source language or FALSE if no source language was found.
107    */
108   protected function getEntityTranslationSourceLanguage($entity_type, $entity_id) {
109     try {
110       return $this->select('entity_translation', 'et')
111         ->fields('et', ['language'])
112         ->condition('entity_type', $entity_type)
113         ->condition('entity_id', $entity_id)
114         ->condition('source', '')
115         ->execute()
116         ->fetchField();
117     }
118     // The table might not exist.
119     catch (\Exception $e) {
120       return FALSE;
121     }
122   }
123
124 }