Version 1
[yaffs-website] / web / core / modules / filter / src / FilterFormatListBuilder.php
1 <?php
2
3 namespace Drupal\filter;
4
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;
12
13 /**
14  * Defines a class to build a listing of filter format entities.
15  *
16  * @see \Drupal\filter\Entity\FilterFormat
17  */
18 class FilterFormatListBuilder extends DraggableListBuilder {
19
20   /**
21    * {@inheritdoc}
22    */
23   protected $entitiesKey = 'formats';
24
25   /**
26    * The config factory service.
27    *
28    * @var \Drupal\Core\Config\ConfigFactoryInterface
29    */
30   protected $configFactory;
31
32   /**
33    * Constructs a new FilterFormatListBuilder.
34    *
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
40    *   The config factory.
41    */
42   public function __construct(EntityTypeInterface $entity_type, EntityStorageInterface $storage, ConfigFactoryInterface $config_factory) {
43     parent::__construct($entity_type, $storage);
44
45     $this->configFactory = $config_factory;
46   }
47
48   /**
49    * {@inheritdoc}
50    */
51   public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) {
52     return new static(
53       $entity_type,
54       $container->get('entity.manager')->getStorage($entity_type->id()),
55       $container->get('config.factory')
56     );
57   }
58
59   /**
60    * {@inheritdoc}
61    */
62   public function getFormId() {
63     return 'filter_admin_overview';
64   }
65
66   /**
67    * {@inheritdoc}
68    */
69   public function load() {
70     // Only list enabled filters.
71     return array_filter(parent::load(), function ($entity) {
72       return $entity->status();
73     });
74   }
75
76   /**
77    * {@inheritdoc}
78    */
79   public function buildHeader() {
80     $header['label'] = $this->t('Name');
81     $header['roles'] = $this->t('Roles');
82     return $header + parent::buildHeader();
83   }
84
85   /**
86    * {@inheritdoc}
87    */
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();
92     $row['roles'] = [];
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');
97       }
98       else {
99         $row['roles']['#markup'] = $this->t('This format is shown when no other formats are available');
100       }
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>';
106     }
107     else {
108       $row['roles'] = [
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'],
113       ];
114     }
115
116     return $row + parent::buildRow($entity);
117   }
118
119   /**
120    * {@inheritdoc}
121    */
122   public function getDefaultOperations(EntityInterface $entity) {
123     $operations = parent::getDefaultOperations($entity);
124
125     if (isset($operations['edit'])) {
126       $operations['edit']['title'] = $this->t('Configure');
127     }
128
129     // The fallback format may not be disabled.
130     if ($entity->isFallbackFormat()) {
131       unset($operations['disable']);
132     }
133
134     return $operations;
135   }
136
137   /**
138    * {@inheritdoc}
139    */
140   public function buildForm(array $form, FormStateInterface $form_state) {
141     $form = parent::buildForm($form, $form_state);
142     $form['actions']['submit']['#value'] = $this->t('Save');
143     return $form;
144   }
145
146   /**
147    * {@inheritdoc}
148    */
149   public function submitForm(array &$form, FormStateInterface $form_state) {
150     parent::submitForm($form, $form_state);
151
152     filter_formats_reset();
153     drupal_set_message($this->t('The text format ordering has been saved.'));
154   }
155
156 }