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 / VariableTranslation.php
1 <?php
2
3 namespace Drupal\migrate_drupal\Plugin\migrate\source\d7;
4
5 use Drupal\Core\Entity\EntityManagerInterface;
6 use Drupal\Core\State\StateInterface;
7 use Drupal\migrate\Plugin\MigrationInterface;
8 use Drupal\migrate_drupal\Plugin\migrate\source\DrupalSqlBase;
9
10 /**
11  * Gets Drupal variable_store source from database.
12  *
13  * @MigrateSource(
14  *   id = "d7_variable_translation",
15  *   source_module = "i18n_variable",
16  * )
17  */
18 class VariableTranslation extends DrupalSqlBase {
19   /**
20    * The variable names to fetch.
21    *
22    * @var array
23    */
24   protected $variables;
25
26   /**
27    * {@inheritdoc}
28    */
29   public function __construct(array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration, StateInterface $state, EntityManagerInterface $entity_manager) {
30     parent::__construct($configuration, $plugin_id, $plugin_definition, $migration, $state, $entity_manager);
31     $this->variables = $this->configuration['variables'];
32   }
33
34   /**
35    * {@inheritdoc}
36    */
37   protected function initializeIterator() {
38     return new \ArrayIterator($this->values());
39   }
40
41   /**
42    * Return the values of the variables specified in the plugin configuration.
43    *
44    * @return array
45    *   An associative array where the keys are the variables specified in the
46    *   plugin configuration and the values are the values found in the source.
47    *   A key/value pair is added for the language code. Only those values are
48    *   returned that are actually in the database.
49    */
50   protected function values() {
51     $values = [];
52     $result = $this->prepareQuery()->execute()->FetchAllAssoc('realm_key');
53     foreach ($result as $variable_store) {
54       $values[]['language'] = $variable_store['realm_key'];
55     }
56     $result = $this->prepareQuery()->execute()->FetchAll();
57     foreach ($result as $variable_store) {
58       foreach ($values as $key => $value) {
59         if ($values[$key]['language'] === $variable_store['realm_key']) {
60           if ($variable_store['serialized']) {
61             $values[$key][$variable_store['name']] = unserialize($variable_store['value']);
62             break;
63           }
64           else {
65             $values[$key][$variable_store['name']] = $variable_store['value'];
66             break;
67           }
68         }
69       }
70     }
71     return $values;
72   }
73
74   /**
75    * {@inheritdoc}
76    */
77   public function count($refresh = FALSE) {
78     return $this->initializeIterator()->count();
79   }
80
81   /**
82    * {@inheritdoc}
83    */
84   public function fields() {
85     return array_combine($this->variables, $this->variables);
86   }
87
88   /**
89    * {@inheritdoc}
90    */
91   public function getIds() {
92     $ids['language']['type'] = 'string';
93     return $ids;
94   }
95
96   /**
97    * {@inheritdoc}
98    */
99   public function query() {
100     return $this->select('variable_store', 'vs')
101       ->fields('vs')
102       ->condition('realm', 'language')
103       ->condition('name', (array) $this->configuration['variables'], 'IN');
104   }
105
106 }