95275139cc2c93e4e9ca9c841b3d24524d103e3b
[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     return $this->select('field_config_instance', 'fci')
26       ->fields('fci')
27       ->condition('entity_type', $entity_type)
28       ->condition('bundle', isset($bundle) ? $bundle : $entity_type)
29       ->condition('deleted', 0)
30       ->execute()
31       ->fetchAllAssoc('field_name');
32   }
33
34   /**
35    * Retrieves field values for a single field of a single entity.
36    *
37    * @param string $entity_type
38    *   The entity type.
39    * @param string $field
40    *   The field name.
41    * @param int $entity_id
42    *   The entity ID.
43    * @param int|null $revision_id
44    *   (optional) The entity revision ID.
45    *
46    * @return array
47    *   The raw field values, keyed by delta.
48    *
49    * @todo Support multilingual field values.
50    */
51   protected function getFieldValues($entity_type, $field, $entity_id, $revision_id = NULL) {
52     $table = (isset($revision_id) ? 'field_revision_' : 'field_data_') . $field;
53     $query = $this->select($table, 't')
54       ->fields('t')
55       ->condition('entity_type', $entity_type)
56       ->condition('entity_id', $entity_id)
57       ->condition('deleted', 0);
58     if (isset($revision_id)) {
59       $query->condition('revision_id', $revision_id);
60     }
61     $values = [];
62     foreach ($query->execute() as $row) {
63       foreach ($row as $key => $value) {
64         $delta = $row['delta'];
65         if (strpos($key, $field) === 0) {
66           $column = substr($key, strlen($field) + 1);
67           $values[$delta][$column] = $value;
68         }
69       }
70     }
71     return $values;
72   }
73
74 }