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