View source
<?php
namespace Drupal\block\Plugin\migrate\process;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\migrate\Entity\MigrationInterface;
use Drupal\migrate\MigrateExecutableInterface;
use Drupal\migrate\MigrateSkipRowException;
use Drupal\migrate\Plugin\MigrateProcessInterface;
use Drupal\migrate\ProcessPluginBase;
use Drupal\migrate\Row;
use Symfony\Component\DependencyInjection\ContainerInterface;
class BlockVisibility extends ProcessPluginBase implements ContainerFactoryPluginInterface {
protected $moduleHandler;
protected $migrationPlugin;
protected $skipPHP = FALSE;
public function __construct(array $configuration, $plugin_id, $plugin_definition, ModuleHandlerInterface $module_handler, MigrateProcessInterface $migration_plugin) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->moduleHandler = $module_handler;
$this->migrationPlugin = $migration_plugin;
if (isset($configuration['skip_php'])) {
$this->skipPHP = $configuration['skip_php'];
}
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration = NULL) {
$migration_configuration = array(
'migration' => array(
'd6_user_role',
'd7_user_role',
),
);
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('module_handler'), $container
->get('plugin.manager.migrate.process')
->createInstance('migration', $migration_configuration, $migration));
}
public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) {
list($old_visibility, $pages, $roles) = $value;
$visibility = array();
if ($roles) {
$visibility['user_role'] = array(
'id' => 'user_role',
'roles' => array(),
'context_mapping' => array(
'user' => '@user.current_user_context:current_user',
),
'negate' => FALSE,
);
foreach ($roles as $key => $role_id) {
$roles[$key] = $this->migrationPlugin
->transform($role_id, $migrate_executable, $row, $destination_property);
}
$visibility['user_role']['roles'] = array_combine($roles, $roles);
}
if ($pages) {
if ($old_visibility == 2) {
if ($this->moduleHandler
->moduleExists('php')) {
$visibility['php'] = array(
'id' => 'php',
'negate' => FALSE,
'php' => $pages,
);
}
elseif ($this->skipPHP) {
throw new MigrateSkipRowException();
}
}
else {
$paths = preg_split("(\r\n?|\n)", $pages);
foreach ($paths as $key => $path) {
$paths[$key] = $path === '<front>' ? $path : '/' . ltrim($path, '/');
}
$visibility['request_path'] = array(
'id' => 'request_path',
'negate' => !$old_visibility,
'pages' => implode("\n", $paths),
);
}
}
return $visibility;
}
}