Further Drupal 8.6.4 changes. Some core files were not committed before a commit...
[yaffs-website] / web / core / modules / migrate_drupal / src / Plugin / migrate / source / Variable.php
1 <?php
2
3 namespace Drupal\migrate_drupal\Plugin\migrate\source;
4
5 use Drupal\Core\Entity\EntityManagerInterface;
6 use Drupal\Core\State\StateInterface;
7 use Drupal\migrate\Plugin\MigrationInterface;
8
9 /**
10  * Drupal variable source from database.
11  *
12  * This source class always returns a single row and as such is not a good
13  * example for any normal source class returning multiple rows.
14  *
15  * @MigrateSource(
16  *   id = "variable",
17  *   source_module = "system",
18  * )
19  */
20 class Variable extends DrupalSqlBase {
21
22   /**
23    * The variable names to fetch.
24    *
25    * @var array
26    */
27   protected $variables;
28
29   /**
30    * {@inheritdoc}
31    */
32   public function __construct(array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration, StateInterface $state, EntityManagerInterface $entity_manager) {
33     parent::__construct($configuration, $plugin_id, $plugin_definition, $migration, $state, $entity_manager);
34     $this->variables = $this->configuration['variables'];
35   }
36
37   /**
38    * {@inheritdoc}
39    */
40   protected function initializeIterator() {
41     return new \ArrayIterator([$this->values()]);
42   }
43
44   /**
45    * Return the values of the variables specified in the plugin configuration.
46    *
47    * @return array
48    *   An associative array where the keys are the variables specified in the
49    *   plugin configuration and the values are the values found in the source.
50    *   Only those values are returned that are actually in the database.
51    */
52   protected function values() {
53     // Create an ID field so we can record migration in the map table.
54     // Arbitrarily, use the first variable name.
55     $values['id'] = reset($this->variables);
56     return $values + array_map('unserialize', $this->prepareQuery()->execute()->fetchAllKeyed());
57   }
58
59   /**
60    * {@inheritdoc}
61    */
62   public function count($refresh = FALSE) {
63     return intval($this->query()->countQuery()->execute()->fetchField() > 0);
64   }
65
66   /**
67    * {@inheritdoc}
68    */
69   public function fields() {
70     return array_combine($this->variables, $this->variables);
71   }
72
73   /**
74    * {@inheritdoc}
75    */
76   public function query() {
77     return $this->getDatabase()
78       ->select('variable', 'v')
79       ->fields('v', ['name', 'value'])
80       ->condition('name', $this->variables, 'IN');
81   }
82
83   /**
84    * {@inheritdoc}
85    */
86   public function getIds() {
87     $ids['id']['type'] = 'string';
88     return $ids;
89   }
90
91 }