82ad3e99325681fa45d799e2d846286665c5e22b
[yaffs-website] / web / core / modules / user / src / Plugin / migrate / source / d6 / Role.php
1 <?php
2
3 namespace Drupal\user\Plugin\migrate\source\d6;
4
5 use Drupal\migrate\Row;
6 use Drupal\migrate_drupal\Plugin\migrate\source\DrupalSqlBase;
7
8 /**
9  * Drupal 6 role source from database.
10  *
11  * @MigrateSource(
12  *   id = "d6_user_role"
13  * )
14  */
15 class Role extends DrupalSqlBase {
16
17   /**
18    * List of filter IDs per role IDs.
19    *
20    * @var array
21    */
22   protected $filterPermissions = [];
23
24   /**
25    * {@inheritdoc}
26    */
27   public function query() {
28     $query = $this->select('role', 'r')
29       ->fields('r', ['rid', 'name'])
30       ->orderBy('r.rid');
31     return $query;
32   }
33
34   /**
35    * {@inheritdoc}
36    */
37   public function fields() {
38     return [
39       'rid' => $this->t('Role ID.'),
40       'name' => $this->t('The name of the user role.'),
41     ];
42   }
43
44   /**
45    * {@inheritdoc}
46    */
47   protected function initializeIterator() {
48     $filter_roles = $this->select('filter_formats', 'f')
49       ->fields('f', ['format', 'roles'])
50       ->execute()
51       ->fetchAllKeyed();
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;
57       }
58     }
59     return parent::initializeIterator();
60   }
61
62   /**
63    * {@inheritdoc}
64    */
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)
70       ->execute()
71       ->fetchField();
72
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.
75     if ($permissions) {
76       $row->setSourceProperty('permissions', explode(', ', $permissions));
77     }
78     else {
79       $row->setSourceProperty('permissions', []);
80     }
81     if (isset($this->filterPermissions[$rid])) {
82       $row->setSourceProperty("filter_permissions:$rid", $this->filterPermissions[$rid]);
83     }
84     return parent::prepareRow($row);
85   }
86
87   /**
88    * {@inheritdoc}
89    */
90   public function getIds() {
91     $ids['rid']['type'] = 'integer';
92     return $ids;
93   }
94
95 }