Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / web / core / modules / user / src / RoleListBuilder.php
1 <?php
2
3 namespace Drupal\user;
4
5 use Drupal\Core\Config\Entity\DraggableListBuilder;
6 use Drupal\Core\Entity\EntityInterface;
7 use Drupal\Core\Entity\EntityStorageInterface;
8 use Drupal\Core\Entity\EntityTypeInterface;
9 use Drupal\Core\Form\FormStateInterface;
10 use Drupal\Core\Messenger\MessengerInterface;
11 use Symfony\Component\DependencyInjection\ContainerInterface;
12
13 /**
14  * Defines a class to build a listing of user role entities.
15  *
16  * @see \Drupal\user\Entity\Role
17  */
18 class RoleListBuilder extends DraggableListBuilder {
19
20   /**
21    * The messenger.
22    *
23    * @var \Drupal\Core\Messenger\MessengerInterface
24    */
25   protected $messenger;
26
27   /**
28    * RoleListBuilder constructor.
29    *
30    * @param \Drupal\Core\Entity\EntityTypeInterface $entityType
31    *   The entity type definition.
32    * @param \Drupal\Core\Entity\EntityStorageInterface $storage
33    *   The entity storage class.
34    * @param \Drupal\Core\Messenger\MessengerInterface $messenger
35    *   The messenger.
36    */
37   public function __construct(EntityTypeInterface $entityType,
38                               EntityStorageInterface $storage,
39                               MessengerInterface $messenger) {
40     parent::__construct($entityType, $storage);
41     $this->messenger = $messenger;
42   }
43
44   /**
45    * {@inheritdoc}
46    */
47   public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) {
48     return new static(
49       $entity_type,
50       $container->get('entity.manager')->getStorage($entity_type->id()),
51       $container->get('messenger')
52     );
53   }
54
55   /**
56    * {@inheritdoc}
57    */
58   public function getFormId() {
59     return 'user_admin_roles_form';
60   }
61
62   /**
63    * {@inheritdoc}
64    */
65   public function buildHeader() {
66     $header['label'] = t('Name');
67     return $header + parent::buildHeader();
68   }
69
70   /**
71    * {@inheritdoc}
72    */
73   public function buildRow(EntityInterface $entity) {
74     $row['label'] = $entity->label();
75     return $row + parent::buildRow($entity);
76   }
77
78   /**
79    * {@inheritdoc}
80    */
81   public function getDefaultOperations(EntityInterface $entity) {
82     $operations = parent::getDefaultOperations($entity);
83
84     if ($entity->hasLinkTemplate('edit-permissions-form')) {
85       $operations['permissions'] = [
86         'title' => t('Edit permissions'),
87         'weight' => 20,
88         'url' => $entity->urlInfo('edit-permissions-form'),
89       ];
90     }
91     return $operations;
92   }
93
94   /**
95    * {@inheritdoc}
96    */
97   public function submitForm(array &$form, FormStateInterface $form_state) {
98     parent::submitForm($form, $form_state);
99
100     $this->messenger->addStatus($this->t('The role settings have been updated.'));
101   }
102
103 }