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.
19 class Variable extends DrupalSqlBase {
22 * The variable names to fetch.
31 public function __construct(array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration, StateInterface $state, EntityManagerInterface $entity_manager) {
32 parent::__construct($configuration, $plugin_id, $plugin_definition, $migration, $state, $entity_manager);
33 $this->variables = $this->configuration['variables'];
39 protected function initializeIterator() {
40 return new \ArrayIterator([$this->values()]);
44 * Return the values of the variables specified in the plugin configuration.
47 * An associative array where the keys are the variables specified in the
48 * plugin configuration and the values are the values found in the source.
49 * Only those values are returned that are actually in the database.
51 protected function values() {
52 // Create an ID field so we can record migration in the map table.
53 // Arbitrarily, use the first variable name.
54 $values['id'] = reset($this->variables);
55 return $values + array_map('unserialize', $this->prepareQuery()->execute()->fetchAllKeyed());
61 public function count($refresh = FALSE) {
62 return intval($this->query()->countQuery()->execute()->fetchField() > 0);
68 public function fields() {
69 return array_combine($this->variables, $this->variables);
75 public function query() {
76 return $this->getDatabase()
77 ->select('variable', 'v')
78 ->fields('v', ['name', 'value'])
79 ->condition('name', $this->variables, 'IN');
85 public function getIds() {
86 $ids['id']['type'] = 'string';