e713f0e27645e1e4e02bc762c33429d15aa7dd9d
[yaffs-website] / web / core / modules / color / src / Plugin / migrate / source / d7 / Color.php
1 <?php
2
3 namespace Drupal\color\Plugin\migrate\source\d7;
4
5 use Drupal\Core\Extension\ThemeHandler;
6 use Drupal\migrate\Row;
7 use Drupal\migrate_drupal\Plugin\migrate\source\VariableMultiRow;
8 use Drupal\Core\Entity\EntityManagerInterface;
9 use Drupal\Core\State\StateInterface;
10 use Drupal\migrate\Plugin\MigrationInterface;
11 use Symfony\Component\DependencyInjection\ContainerInterface;
12
13 /**
14  * Drupal 7 color source from database.
15  *
16  * @MigrateSource(
17  *   id = "d7_color",
18  *   source_module = "color"
19  * )
20  */
21 class Color extends VariableMultiRow {
22
23   /**
24    * The theme handler.
25    *
26    * @var \Drupal\Core\Extension\ThemeHandler
27    */
28   protected $themeHandler;
29
30   /**
31    * {@inheritdoc}
32    */
33   public function __construct(array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration, StateInterface $state, EntityManagerInterface $entity_manager, ThemeHandler $theme_handler) {
34     parent::__construct($configuration, $plugin_id, $plugin_definition, $migration, $state, $entity_manager);
35     $this->themeHandler = $theme_handler;
36   }
37
38   /**
39    * {@inheritdoc}
40    */
41   public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration = NULL) {
42     return new static(
43       $configuration,
44       $plugin_id,
45       $plugin_definition,
46       $migration,
47       $container->get('state'),
48       $container->get('entity.manager'),
49       $container->get('theme_handler')
50     );
51   }
52
53   /**
54    * {@inheritdoc}
55    */
56   public function query() {
57     // Get color data for all themes.
58     $query = $this->select('variable', 'v')
59       ->fields('v', ['name', 'value'])
60       ->condition('name', 'color_%', 'LIKE');
61     return $query;
62   }
63
64   /**
65    * {@inheritdoc}
66    */
67   public function prepareRow(Row $row) {
68     $themes = $this->themeHandler->listInfo();
69     $themes_installed = [];
70     /** @var \Drupal\Core\Extension\Extension $theme */
71     foreach ($themes as $theme) {
72       if ($theme->status) {
73         $themes_installed[] = $theme->getName();
74       }
75     }
76
77     // The name is of the form 'color_theme_variable'.
78     $name = explode('_', $row->getSourceProperty('name'));
79
80     // Set theme_installed if this source theme is installed.
81     if (in_array($name[1], $themes_installed)) {
82       $row->setSourceProperty('theme_installed', TRUE);
83     }
84
85     return parent::prepareRow($row);
86   }
87
88   /**
89    * {@inheritdoc}
90    */
91   public function fields() {
92     return [
93       'name' => $this->t('A color variable for a theme.'),
94       'value' => $this->t('The value of a color variable.'),
95     ];
96   }
97
98 }