3 namespace Drupal\migrate_drupal\Plugin\migrate\source\d7;
5 use Drupal\migrate_drupal\Plugin\migrate\source\DrupalSqlBase;
8 * Base class for D7 source plugins which need to collect field values from
11 abstract class FieldableEntity extends DrupalSqlBase {
14 * Returns all non-deleted field instances attached to a specific entity type.
16 * @param string $entity_type
18 * @param string|null $bundle
19 * (optional) The bundle.
22 * The field instances, keyed by field name.
24 protected function getFields($entity_type, $bundle = NULL) {
25 $query = $this->select('field_config_instance', 'fci')
27 ->condition('fci.entity_type', $entity_type)
28 ->condition('fci.bundle', isset($bundle) ? $bundle : $entity_type)
29 ->condition('fci.deleted', 0);
31 // Join the 'field_config' table and add the 'translatable' setting to the
33 $query->leftJoin('field_config', 'fc', 'fci.field_id = fc.id');
34 $query->addField('fc', 'translatable');
36 return $query->execute()->fetchAllAssoc('field_name');
40 * Retrieves field values for a single field of a single entity.
42 * @param string $entity_type
44 * @param string $field
46 * @param int $entity_id
48 * @param int|null $revision_id
49 * (optional) The entity revision ID.
50 * @param string $language
51 * (optional) The field language.
54 * The raw field values, keyed by delta.
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')
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);
66 // Add 'language' as a query condition if it has been defined by Entity
69 $query->condition('language', $language);
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;
85 * Checks if an entity type uses Entity Translation.
87 * @param string $entity_type
91 * Whether the entity type uses entity translation.
93 protected function isEntityTranslatable($entity_type) {
94 return in_array($entity_type, $this->variableGet('entity_translation_entity_types', []), TRUE);
98 * Gets an entity source language from the 'entity_translation' table.
100 * @param string $entity_type
102 * @param int $entity_id
105 * @return string|bool
106 * The entity source language or FALSE if no source language was found.
108 protected function getEntityTranslationSourceLanguage($entity_type, $entity_id) {
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', '')
118 // The table might not exist.
119 catch (\Exception $e) {