3 namespace Drupal\filter;
5 use Drupal\Core\Config\ConfigFactoryInterface;
6 use Drupal\Core\Config\Entity\DraggableListBuilder;
7 use Drupal\Core\Entity\EntityInterface;
8 use Drupal\Core\Entity\EntityStorageInterface;
9 use Drupal\Core\Entity\EntityTypeInterface;
10 use Drupal\Core\Form\FormStateInterface;
11 use Drupal\Core\Messenger\MessengerInterface;
12 use Symfony\Component\DependencyInjection\ContainerInterface;
15 * Defines a class to build a listing of filter format entities.
17 * @see \Drupal\filter\Entity\FilterFormat
19 class FilterFormatListBuilder extends DraggableListBuilder {
24 protected $entitiesKey = 'formats';
27 * The config factory service.
29 * @var \Drupal\Core\Config\ConfigFactoryInterface
31 protected $configFactory;
36 * @var \Drupal\Core\Messenger\MessengerInterface
41 * Constructs a new FilterFormatListBuilder.
43 * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
44 * The entity type definition.
45 * @param \Drupal\Core\Entity\EntityStorageInterface $storage
46 * The entity storage class.
47 * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
49 * @param \Drupal\Core\Messenger\MessengerInterface $messenger
52 public function __construct(EntityTypeInterface $entity_type, EntityStorageInterface $storage, ConfigFactoryInterface $config_factory, MessengerInterface $messenger) {
53 parent::__construct($entity_type, $storage);
55 $this->configFactory = $config_factory;
56 $this->messenger = $messenger;
62 public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) {
65 $container->get('entity.manager')->getStorage($entity_type->id()),
66 $container->get('config.factory'),
67 $container->get('messenger')
74 public function getFormId() {
75 return 'filter_admin_overview';
81 public function load() {
82 // Only list enabled filters.
83 return array_filter(parent::load(), function ($entity) {
84 return $entity->status();
91 public function buildHeader() {
92 $header['label'] = $this->t('Name');
93 $header['roles'] = $this->t('Roles');
94 return $header + parent::buildHeader();
100 public function buildRow(EntityInterface $entity) {
101 // Check whether this is the fallback text format. This format is available
102 // to all roles and cannot be disabled via the admin interface.
103 $row['label'] = $entity->label();
105 if ($entity->isFallbackFormat()) {
106 $fallback_choice = $this->configFactory->get('filter.settings')->get('always_show_fallback_choice');
107 if ($fallback_choice) {
108 $row['roles']['#markup'] = $this->t('All roles may use this format');
111 $row['roles']['#markup'] = $this->t('This format is shown when no other formats are available');
113 // Emphasize the fallback role text since it is important to understand
114 // how it works which configuring filter formats. Additionally, it is not
115 // a list of roles unlike the other values in this column.
116 $row['roles']['#prefix'] = '<em>';
117 $row['roles']['#suffix'] = '</em>';
121 '#theme' => 'item_list',
122 '#items' => filter_get_roles_by_format($entity),
123 '#empty' => $this->t('No roles may use this format'),
124 '#context' => ['list_style' => 'comma-list'],
128 return $row + parent::buildRow($entity);
134 public function getDefaultOperations(EntityInterface $entity) {
135 $operations = parent::getDefaultOperations($entity);
137 if (isset($operations['edit'])) {
138 $operations['edit']['title'] = $this->t('Configure');
141 // The fallback format may not be disabled.
142 if ($entity->isFallbackFormat()) {
143 unset($operations['disable']);
152 public function buildForm(array $form, FormStateInterface $form_state) {
153 $form = parent::buildForm($form, $form_state);
154 $form['actions']['submit']['#value'] = $this->t('Save');
161 public function submitForm(array &$form, FormStateInterface $form_state) {
162 parent::submitForm($form, $form_state);
164 filter_formats_reset();
165 $this->messenger->addStatus($this->t('The text format ordering has been saved.'));