Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / web / core / modules / filter / src / FilterPermissions.php
1 <?php
2
3 namespace Drupal\filter;
4
5 use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
6 use Drupal\Core\Entity\EntityManagerInterface;
7 use Drupal\Core\StringTranslation\StringTranslationTrait;
8 use Symfony\Component\DependencyInjection\ContainerInterface;
9
10 /**
11  * Provides dynamic permissions of the filter module.
12  */
13 class FilterPermissions implements ContainerInjectionInterface {
14
15   use StringTranslationTrait;
16
17   /**
18    * The entity manager.
19    *
20    * @var \Drupal\Core\Entity\EntityManagerInterface
21    */
22   protected $entityManager;
23
24   /**
25    * Constructs a new FilterPermissions instance.
26    *
27    * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
28    *   The entity manager.
29    */
30   public function __construct(EntityManagerInterface $entity_manager) {
31     $this->entityManager = $entity_manager;
32   }
33
34   /**
35    * {@inheritdoc}
36    */
37   public static function create(ContainerInterface $container) {
38     return new static($container->get('entity.manager'));
39   }
40
41   /**
42    * Returns an array of filter permissions.
43    *
44    * @return array
45    */
46   public function permissions() {
47     $permissions = [];
48     // Generate permissions for each text format. Warn the administrator that any
49     // of them are potentially unsafe.
50     /** @var \Drupal\filter\FilterFormatInterface[] $formats */
51     $formats = $this->entityManager->getStorage('filter_format')->loadByProperties(['status' => TRUE]);
52     uasort($formats, 'Drupal\Core\Config\Entity\ConfigEntityBase::sort');
53     foreach ($formats as $format) {
54       if ($permission = $format->getPermissionName()) {
55         $permissions[$permission] = [
56           'title' => $this->t('Use the <a href=":url">@label</a> text format', [':url' => $format->url(), '@label' => $format->label()]),
57           'description' => [
58             '#prefix' => '<em>',
59             '#markup' => $this->t('Warning: This permission may have security implications depending on how the text format is configured.'),
60             '#suffix' => '</em>',
61           ],
62         ];
63       }
64     }
65     return $permissions;
66   }
67
68 }