View source
<?php
namespace Drupal\block\Plugin\migrate\process;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\migrate\MigrateLookupInterface;
use Drupal\migrate\Plugin\MigrationInterface;
use Drupal\migrate\MigrateExecutableInterface;
use Drupal\migrate\MigrateSkipRowException;
use Drupal\migrate\ProcessPluginBase;
use Drupal\migrate\Row;
use Symfony\Component\DependencyInjection\ContainerInterface;
class BlockVisibility extends ProcessPluginBase implements ContainerFactoryPluginInterface {
protected $moduleHandler;
protected $migrateLookup;
protected $skipPHP = FALSE;
public function __construct(array $configuration, $plugin_id, $plugin_definition, ModuleHandlerInterface $module_handler, MigrateLookupInterface $migrate_lookup) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->moduleHandler = $module_handler;
$this->migrateLookup = $migrate_lookup;
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) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('module_handler'), $container
->get('migrate.lookup'));
}
public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) {
[
$old_visibility,
$pages,
$roles,
] = $value;
$visibility = [];
if ($roles) {
$visibility['user_role'] = [
'id' => 'user_role',
'roles' => [],
'context_mapping' => [
'user' => '@user.current_user_context:current_user',
],
'negate' => FALSE,
];
foreach ($roles as $key => $role_id) {
$lookup_result = $this->migrateLookup
->lookup([
'd6_user_role',
'd7_user_role',
], [
$role_id,
]);
if ($lookup_result) {
$roles[$key] = $lookup_result[0]['id'];
}
}
$visibility['user_role']['roles'] = array_combine($roles, $roles);
}
if ($pages) {
if ($old_visibility == 2) {
if ($this->moduleHandler
->moduleExists('php')) {
$visibility['php'] = [
'id' => 'php',
'negate' => FALSE,
'php' => $pages,
];
}
elseif ($this->skipPHP) {
throw new MigrateSkipRowException(sprintf("The block with bid '%d' from module '%s' will have no PHP or request_path visibility configuration.", $row
->getSourceProperty('bid'), $row
->getSourceProperty('module')));
}
}
else {
$paths = preg_split("(\r\n?|\n)", $pages);
foreach ($paths as $key => $path) {
$paths[$key] = $path === '<front>' ? $path : '/' . ltrim($path, '/');
}
$visibility['request_path'] = [
'id' => 'request_path',
'negate' => !$old_visibility,
'pages' => implode("\n", $paths),
];
}
}
return $visibility;
}
}