Version 1
[yaffs-website] / web / core / modules / block / src / Plugin / migrate / process / BlockVisibility.php
1 <?php
2
3 namespace Drupal\block\Plugin\migrate\process;
4
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;
14
15 /**
16  * @MigrateProcessPlugin(
17  *   id = "block_visibility"
18  * )
19  */
20 class BlockVisibility extends ProcessPluginBase implements ContainerFactoryPluginInterface {
21
22   /**
23    * The module handler.
24    *
25    * @var \Drupal\Core\Extension\ModuleHandlerInterface
26    */
27   protected $moduleHandler;
28
29   /**
30    * The migration process plugin, configured for lookups in the d6_user_role
31    * and d7_user_role migrations.
32    *
33    * @var \Drupal\migrate\Plugin\MigrateProcessInterface
34    */
35   protected $migrationPlugin;
36
37   /**
38    * Whether or not to skip blocks that use PHP for visibility. Only applies
39    * if the PHP module is not enabled.
40    *
41    * @var bool
42    */
43   protected $skipPHP = FALSE;
44
45   /**
46    * {@inheritdoc}
47    */
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;
52
53     if (isset($configuration['skip_php'])) {
54       $this->skipPHP = $configuration['skip_php'];
55     }
56   }
57
58   /**
59    * {@inheritdoc}
60    */
61   public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration = NULL) {
62     $migration_configuration = [
63       'migration' => [
64         'd6_user_role',
65         'd7_user_role',
66       ],
67     ];
68     return new static(
69       $configuration,
70       $plugin_id,
71       $plugin_definition,
72       $container->get('module_handler'),
73       $container->get('plugin.manager.migrate.process')->createInstance('migration', $migration_configuration, $migration)
74     );
75   }
76
77   /**
78    * {@inheritdoc}
79    */
80   public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) {
81     list($old_visibility, $pages, $roles) = $value;
82
83     $visibility = [];
84
85     // If the block is assigned to specific roles, add the user_role condition.
86     if ($roles) {
87       $visibility['user_role'] = [
88         'id' => 'user_role',
89         'roles' => [],
90         'context_mapping' => [
91           'user' => '@user.current_user_context:current_user',
92         ],
93         'negate' => FALSE,
94       ];
95
96       foreach ($roles as $key => $role_id) {
97         $roles[$key] = $this->migrationPlugin->transform($role_id, $migrate_executable, $row, $destination_property);
98       }
99       $visibility['user_role']['roles'] = array_combine($roles, $roles);
100     }
101
102     if ($pages) {
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'] = [
108             'id' => 'php',
109             // PHP code visibility could not be negated in Drupal 6 or 7.
110             'negate' => FALSE,
111             'php' => $pages,
112           ];
113         }
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();
119         }
120       }
121       else {
122         $paths = preg_split("(\r\n?|\n)", $pages);
123         foreach ($paths as $key => $path) {
124           $paths[$key] = $path === '<front>' ? $path : '/' . ltrim($path, '/');
125         }
126         $visibility['request_path'] = [
127           'id' => 'request_path',
128           'negate' => !$old_visibility,
129           'pages' => implode("\n", $paths),
130         ];
131       }
132     }
133
134     return $visibility;
135   }
136
137 }