Version 1
[yaffs-website] / web / core / modules / migrate_drupal / src / Plugin / migrate / source / d6 / i18nVariable.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 = "i18n_variable"
15  * )
16  */
17 class i18nVariable extends DrupalSqlBase {
18
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('language');
53     foreach ($result as $i18n_variable) {
54       $values[]['language'] = $i18n_variable->language;
55     }
56     $result = $this->prepareQuery()->execute()->FetchAll();
57     foreach ($result as $i18n_variable) {
58       foreach ($values as $key => $value) {
59         if ($values[$key]['language'] === $i18n_variable->language) {
60           $values[$key][$i18n_variable->name] = unserialize($i18n_variable->value);
61           break;
62         }
63       }
64     }
65     return $values;
66   }
67
68   /**
69    * {@inheritdoc}
70    */
71   public function count() {
72     return $this->initializeIterator()->count();
73   }
74
75   /**
76    * {@inheritdoc}
77    */
78   public function fields() {
79     return array_combine($this->variables, $this->variables);
80   }
81
82   /**
83    * {@inheritdoc}
84    */
85   public function query() {
86     return $this->getDatabase()
87       ->select('i18n_variable', 'v')
88       ->fields('v')
89       ->condition('name', (array) $this->configuration['variables'], 'IN');
90   }
91
92   /**
93    * {@inheritdoc}
94    */
95   public function getIds() {
96     $ids['language']['type'] = 'string';
97     return $ids;
98   }
99
100 }