3 namespace Drupal\block\Plugin\migrate\process;
5 use Drupal\Core\Extension\ModuleHandlerInterface;
6 use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
7 use Drupal\migrate\Plugin\MigrationInterface;
8 use Drupal\migrate\MigrateExecutableInterface;
9 use Drupal\migrate\MigrateSkipRowException;
10 use Drupal\migrate\Plugin\MigrateProcessInterface;
11 use Drupal\migrate\ProcessPluginBase;
12 use Drupal\migrate\Row;
13 use Symfony\Component\DependencyInjection\ContainerInterface;
16 * @MigrateProcessPlugin(
17 * id = "block_visibility"
20 class BlockVisibility extends ProcessPluginBase implements ContainerFactoryPluginInterface {
25 * @var \Drupal\Core\Extension\ModuleHandlerInterface
27 protected $moduleHandler;
30 * The migration process plugin, configured for lookups in the d6_user_role
31 * and d7_user_role migrations.
33 * @var \Drupal\migrate\Plugin\MigrateProcessInterface
35 protected $migrationPlugin;
38 * Whether or not to skip blocks that use PHP for visibility. Only applies
39 * if the PHP module is not enabled.
43 protected $skipPHP = FALSE;
48 public function __construct(array $configuration, $plugin_id, $plugin_definition, ModuleHandlerInterface $module_handler, MigrateProcessInterface $migration_plugin) {
49 parent::__construct($configuration, $plugin_id, $plugin_definition);
50 $this->moduleHandler = $module_handler;
51 $this->migrationPlugin = $migration_plugin;
53 if (isset($configuration['skip_php'])) {
54 $this->skipPHP = $configuration['skip_php'];
61 public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration = NULL) {
62 $migration_configuration = [
72 $container->get('module_handler'),
73 $container->get('plugin.manager.migrate.process')->createInstance('migration', $migration_configuration, $migration)
80 public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) {
81 list($old_visibility, $pages, $roles) = $value;
85 // If the block is assigned to specific roles, add the user_role condition.
87 $visibility['user_role'] = [
90 'context_mapping' => [
91 'user' => '@user.current_user_context:current_user',
96 foreach ($roles as $key => $role_id) {
97 $roles[$key] = $this->migrationPlugin->transform($role_id, $migrate_executable, $row, $destination_property);
99 $visibility['user_role']['roles'] = array_combine($roles, $roles);
103 // 2 == BLOCK_VISIBILITY_PHP in Drupal 6 and 7.
104 if ($old_visibility == 2) {
105 // If the PHP module is present, migrate the visibility code unaltered.
106 if ($this->moduleHandler->moduleExists('php')) {
107 $visibility['php'] = [
109 // PHP code visibility could not be negated in Drupal 6 or 7.
114 // Skip the row if we're configured to. If not, we don't need to do
115 // anything else -- the block will simply have no PHP or request_path
116 // visibility configuration.
117 elseif ($this->skipPHP) {
118 throw new MigrateSkipRowException();
122 $paths = preg_split("(\r\n?|\n)", $pages);
123 foreach ($paths as $key => $path) {
124 $paths[$key] = $path === '<front>' ? $path : '/' . ltrim($path, '/');
126 $visibility['request_path'] = [
127 'id' => 'request_path',
128 'negate' => !$old_visibility,
129 'pages' => implode("\n", $paths),