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 Symfony\Component\DependencyInjection\ContainerInterface;
14 * Defines a class to build a listing of filter format entities.
16 * @see \Drupal\filter\Entity\FilterFormat
18 class FilterFormatListBuilder extends DraggableListBuilder {
23 protected $entitiesKey = 'formats';
26 * The config factory service.
28 * @var \Drupal\Core\Config\ConfigFactoryInterface
30 protected $configFactory;
33 * Constructs a new FilterFormatListBuilder.
35 * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
36 * The entity type definition.
37 * @param \Drupal\Core\Entity\EntityStorageInterface $storage
38 * The entity storage class.
39 * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
42 public function __construct(EntityTypeInterface $entity_type, EntityStorageInterface $storage, ConfigFactoryInterface $config_factory) {
43 parent::__construct($entity_type, $storage);
45 $this->configFactory = $config_factory;
51 public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) {
54 $container->get('entity.manager')->getStorage($entity_type->id()),
55 $container->get('config.factory')
62 public function getFormId() {
63 return 'filter_admin_overview';
69 public function load() {
70 // Only list enabled filters.
71 return array_filter(parent::load(), function ($entity) {
72 return $entity->status();
79 public function buildHeader() {
80 $header['label'] = $this->t('Name');
81 $header['roles'] = $this->t('Roles');
82 return $header + parent::buildHeader();
88 public function buildRow(EntityInterface $entity) {
89 // Check whether this is the fallback text format. This format is available
90 // to all roles and cannot be disabled via the admin interface.
91 $row['label'] = $entity->label();
93 if ($entity->isFallbackFormat()) {
94 $fallback_choice = $this->configFactory->get('filter.settings')->get('always_show_fallback_choice');
95 if ($fallback_choice) {
96 $row['roles']['#markup'] = $this->t('All roles may use this format');
99 $row['roles']['#markup'] = $this->t('This format is shown when no other formats are available');
101 // Emphasize the fallback role text since it is important to understand
102 // how it works which configuring filter formats. Additionally, it is not
103 // a list of roles unlike the other values in this column.
104 $row['roles']['#prefix'] = '<em>';
105 $row['roles']['#suffix'] = '</em>';
109 '#theme' => 'item_list',
110 '#items' => filter_get_roles_by_format($entity),
111 '#empty' => $this->t('No roles may use this format'),
112 '#context' => ['list_style' => 'comma-list'],
116 return $row + parent::buildRow($entity);
122 public function getDefaultOperations(EntityInterface $entity) {
123 $operations = parent::getDefaultOperations($entity);
125 if (isset($operations['edit'])) {
126 $operations['edit']['title'] = $this->t('Configure');
129 // The fallback format may not be disabled.
130 if ($entity->isFallbackFormat()) {
131 unset($operations['disable']);
140 public function buildForm(array $form, FormStateInterface $form_state) {
141 $form = parent::buildForm($form, $form_state);
142 $form['actions']['submit']['#value'] = $this->t('Save');
149 public function submitForm(array &$form, FormStateInterface $form_state) {
150 parent::submitForm($form, $form_state);
152 filter_formats_reset();
153 drupal_set_message($this->t('The text format ordering has been saved.'));