learnosity-1.0.x-dev/src/LearnosityActivityEditorPermissions.php
src/LearnosityActivityEditorPermissions.php
<?php
namespace Drupal\learnosity;
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
use Drupal\Core\DependencyInjection\DeprecatedServicePropertyTrait;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Provides dynamic permissions of the filter module.
*/
class LearnosityActivityEditorPermissions implements ContainerInjectionInterface {
use StringTranslationTrait;
use DeprecatedServicePropertyTrait;
/**
* The entity type manager.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;
/**
* LearnosityActivityEditorPermissions constructor.
*
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* The entity type manager.
*/
public function __construct(EntityTypeManagerInterface $entity_type_manager) {
$this->entityTypeManager = $entity_type_manager;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static($container->get('entity_type.manager'));
}
/**
* Returns an array of editor permissions.
*
* @return array
* The array of permissions.
*
* @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
* @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
* @throws \Drupal\Core\Entity\EntityMalformedException
*/
public function permissions() {
$permissions = [];
// Generate permissions for each learnosity activity editor. Warn the
// administrator that any of them are potentially unsafe.
$formats = $this->entityTypeManager->getStorage('learnosity_activity_editor')->loadMultiple();
uasort($formats, 'Drupal\Core\Config\Entity\ConfigEntityBase::sort');
foreach ($formats as $format) {
if (!$format->isFallback() && $permission = $format->getPermissionName()) {
$permissions[$permission] = [
'title' => $this->t('Use the <a href=":url">@label</a> activity editor', [':url' => $format->toUrl()->toString(), '@label' => $format->label()]),
'description' => [
'#prefix' => '<em>',
'#markup' => $this->t('Warning: This permission may have security implications depending on how the editor is configured.'),
'#suffix' => '</em>',
],
];
}
}
return $permissions;
}
}
