3 namespace Drupal\user\Plugin\migrate\source\d6;
5 use Drupal\migrate\Row;
6 use Drupal\migrate_drupal\Plugin\migrate\source\DrupalSqlBase;
9 * Drupal 6 role source from database.
15 class Role extends DrupalSqlBase {
18 * List of filter IDs per role IDs.
22 protected $filterPermissions = [];
27 public function query() {
28 $query = $this->select('role', 'r')
29 ->fields('r', ['rid', 'name'])
37 public function fields() {
39 'rid' => $this->t('Role ID.'),
40 'name' => $this->t('The name of the user role.'),
47 protected function initializeIterator() {
48 $filter_roles = $this->select('filter_formats', 'f')
49 ->fields('f', ['format', 'roles'])
52 foreach ($filter_roles as $format => $roles) {
53 // Drupal 6 code: $roles = ','. implode(',', $roles) .',';
54 // Remove the beginning and ending comma.
55 foreach (explode(',', trim($roles, ',')) as $rid) {
56 $this->filterPermissions[$rid][] = $format;
59 return parent::initializeIterator();
65 public function prepareRow(Row $row) {
66 $rid = $row->getSourceProperty('rid');
67 $permissions = $this->select('permission', 'p')
68 ->fields('p', ['perm'])
69 ->condition('rid', $rid)
73 // If a role has no permissions then set to an empty array. The role will
74 // be migrated and given the default D8 permissions.
76 $row->setSourceProperty('permissions', explode(', ', $permissions));
79 $row->setSourceProperty('permissions', []);
81 if (isset($this->filterPermissions[$rid])) {
82 $row->setSourceProperty("filter_permissions:$rid", $this->filterPermissions[$rid]);
84 return parent::prepareRow($row);
90 public function getIds() {
91 $ids['rid']['type'] = 'integer';