View source
<?php
namespace Drupal\color\Plugin\migrate\source\d7;
use Drupal\Core\Extension\ThemeHandler;
use Drupal\migrate\Row;
use Drupal\migrate_drupal\Plugin\migrate\source\VariableMultiRow;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\State\StateInterface;
use Drupal\migrate\Plugin\MigrationInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class Color extends VariableMultiRow {
protected $themeHandler;
public function __construct(array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration, StateInterface $state, EntityTypeManagerInterface $entity_type_manager, ThemeHandler $theme_handler) {
parent::__construct($configuration, $plugin_id, $plugin_definition, $migration, $state, $entity_type_manager);
$this->themeHandler = $theme_handler;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration = NULL) {
return new static($configuration, $plugin_id, $plugin_definition, $migration, $container
->get('state'), $container
->get('entity_type.manager'), $container
->get('theme_handler'));
}
public function query() {
$query = $this
->select('variable', 'v')
->fields('v', [
'name',
'value',
])
->condition('name', 'color_%', 'LIKE');
return $query;
}
public function prepareRow(Row $row) {
$themes = $this->themeHandler
->listInfo();
$themes_installed = [];
foreach ($themes as $theme) {
if ($theme->status) {
$themes_installed[] = $theme
->getName();
}
}
$name = explode('_', $row
->getSourceProperty('name'));
if (in_array($name[1], $themes_installed)) {
$row
->setSourceProperty('theme_installed', TRUE);
}
return parent::prepareRow($row);
}
public function fields() {
return [
'name' => $this
->t('A color variable for a theme.'),
'value' => $this
->t('The value of a color variable.'),
];
}
}