BlockVisibility.php 6.18 KB
Newer Older
1 2 3 4 5 6
<?php

namespace Drupal\block\Plugin\migrate\process;

use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
7
use Drupal\migrate\MigrateLookupInterface;
8
use Drupal\migrate\Plugin\MigrationInterface;
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
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;

/**
 * @MigrateProcessPlugin(
 *   id = "block_visibility"
 * )
 */
class BlockVisibility extends ProcessPluginBase implements ContainerFactoryPluginInterface {

  /**
   * The module handler.
   *
   * @var \Drupal\Core\Extension\ModuleHandlerInterface
   */
  protected $moduleHandler;

  /**
31 32 33 34
   * The migration process plugin.
   *
   * The plugin is configured for lookups in the d6_user_role and d7_user_role
   * migrations.
35 36
   *
   * @var \Drupal\migrate\Plugin\MigrateProcessInterface
37 38 39 40 41
   *
   * @deprecated in drupal:8.8.x and is removed from drupal:9.0.0. Use
   *   the migrate.lookup service instead.
   *
   * @see https://www.drupal.org/node/3047268
42 43 44
   */
  protected $migrationPlugin;

45 46 47 48 49 50 51
  /**
   * The migrate lookup service.
   *
   * @var \Drupal\migrate\MigrateLookupInterface
   */
  protected $migrateLookup;

52 53 54 55 56 57 58 59 60
  /**
   * Whether or not to skip blocks that use PHP for visibility. Only applies
   * if the PHP module is not enabled.
   *
   * @var bool
   */
  protected $skipPHP = FALSE;

  /**
61 62 63 64 65 66 67 68 69 70 71 72
   * Constructs a BlockVisibility object.
   *
   * @param array $configuration
   *   The plugin configuration.
   * @param string $plugin_id
   *   The plugin ID.
   * @param mixed $plugin_definition
   *   The plugin definition.
   * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
   *   The module handler service.
   * @param \Drupal\migrate\MigrateLookupInterface $migrate_lookup
   *   The migrate lookup service.
73
   */
74 75
  // @codingStandardsIgnoreLine
  public function __construct(array $configuration, $plugin_id, $plugin_definition, ModuleHandlerInterface $module_handler, $migrate_lookup) {
76
    parent::__construct($configuration, $plugin_id, $plugin_definition);
77 78 79 80 81 82 83 84
    if ($migrate_lookup instanceof MigrateProcessInterface) {
      @trigger_error('Passing a migration process plugin as the fifth argument to ' . __METHOD__ . ' is deprecated in drupal:8.8.0 and will throw an error in drupal:9.0.0. Pass the migrate.lookup service instead. See https://www.drupal.org/node/3047268', E_USER_DEPRECATED);
      $this->migrationPlugin = $migrate_lookup;
      $migrate_lookup = \Drupal::service('migrate.lookup');
    }
    elseif (!$migrate_lookup instanceof MigrateLookupInterface) {
      throw new \InvalidArgumentException("The fifth argument to " . __METHOD__ . " must be an instance of MigrateLookupInterface.");
    }
85
    $this->moduleHandler = $module_handler;
86
    $this->migrateLookup = $migrate_lookup;
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101

    if (isset($configuration['skip_php'])) {
      $this->skipPHP = $configuration['skip_php'];
    }
  }

  /**
   * {@inheritdoc}
   */
  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'),
102
      $container->get('migrate.lookup')
103 104 105 106 107 108 109 110 111
    );
  }

  /**
   * {@inheritdoc}
   */
  public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) {
    list($old_visibility, $pages, $roles) = $value;

112
    $visibility = [];
113 114 115

    // If the block is assigned to specific roles, add the user_role condition.
    if ($roles) {
116
      $visibility['user_role'] = [
117
        'id' => 'user_role',
118 119
        'roles' => [],
        'context_mapping' => [
120
          'user' => '@user.current_user_context:current_user',
121
        ],
122
        'negate' => FALSE,
123
      ];
124 125

      foreach ($roles as $key => $role_id) {
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
        // This BC layer is included because if the plugin constructor was
        // called in the legacy way with a migration_lookup process plugin, it
        // may have been preconfigured with a different migration to look up
        // against. While this is unlikely, for maximum BC we will continue to
        // use the plugin to do the lookup if it is provided, and support for
        // this will be removed in Drupal 9.
        if ($this->migrationPlugin) {
          $roles[$key] = $this->migrationPlugin->transform($role_id, $migrate_executable, $row, $destination_property);
        }
        else {
          $lookup_result = $this->migrateLookup->lookup(['d6_user_role', 'd7_user_role'], [$role_id]);
          if ($lookup_result) {
            $roles[$key] = $lookup_result[0]['id'];
          }
        }

142 143 144 145 146 147 148 149 150
      }
      $visibility['user_role']['roles'] = array_combine($roles, $roles);
    }

    if ($pages) {
      // 2 == BLOCK_VISIBILITY_PHP in Drupal 6 and 7.
      if ($old_visibility == 2) {
        // If the PHP module is present, migrate the visibility code unaltered.
        if ($this->moduleHandler->moduleExists('php')) {
151
          $visibility['php'] = [
152 153 154 155
            'id' => 'php',
            // PHP code visibility could not be negated in Drupal 6 or 7.
            'negate' => FALSE,
            'php' => $pages,
156
          ];
157 158 159 160 161
        }
        // Skip the row if we're configured to. If not, we don't need to do
        // anything else -- the block will simply have no PHP or request_path
        // visibility configuration.
        elseif ($this->skipPHP) {
162
          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'), $destination_property));
163 164 165 166 167 168 169
        }
      }
      else {
        $paths = preg_split("(\r\n?|\n)", $pages);
        foreach ($paths as $key => $path) {
          $paths[$key] = $path === '<front>' ? $path : '/' . ltrim($path, '/');
        }
170
        $visibility['request_path'] = [
171 172 173
          'id' => 'request_path',
          'negate' => !$old_visibility,
          'pages' => implode("\n", $paths),
174
        ];
175 176 177 178 179 180 181
      }
    }

    return $visibility;
  }

}