3 namespace Drupal\migrate_drupal\Plugin\migrate\source;
5 use Drupal\Core\Entity\EntityManagerInterface;
6 use Drupal\Core\State\StateInterface;
7 use Drupal\migrate\Plugin\MigrationInterface;
10 * Drupal variable source from database.
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.
17 * source_module = "system",
20 class Variable extends DrupalSqlBase {
23 * The variable names to fetch.
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'];
40 protected function initializeIterator() {
41 return new \ArrayIterator([$this->values()]);
45 * Return the values of the variables specified in the plugin configuration.
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.
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());
62 public function count($refresh = FALSE) {
63 return intval($this->query()->countQuery()->execute()->fetchField() > 0);
69 public function fields() {
70 return array_combine($this->variables, $this->variables);
76 public function query() {
77 return $this->getDatabase()
78 ->select('variable', 'v')
79 ->fields('v', ['name', 'value'])
80 ->condition('name', $this->variables, 'IN');
86 public function getIds() {
87 $ids['id']['type'] = 'string';