Updated Drupal to 8.6. This goes with the following updates because it's possible...
[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 Drupal\Core\Messenger\MessengerInterface;
12 use Symfony\Component\DependencyInjection\ContainerInterface;
13
14 /**
15  * Defines a class to build a listing of filter format entities.
16  *
17  * @see \Drupal\filter\Entity\FilterFormat
18  */
19 class FilterFormatListBuilder extends DraggableListBuilder {
20
21   /**
22    * {@inheritdoc}
23    */
24   protected $entitiesKey = 'formats';
25
26   /**
27    * The config factory service.
28    *
29    * @var \Drupal\Core\Config\ConfigFactoryInterface
30    */
31   protected $configFactory;
32
33   /**
34    * The messenger.
35    *
36    * @var \Drupal\Core\Messenger\MessengerInterface
37    */
38   protected $messenger;
39
40   /**
41    * Constructs a new FilterFormatListBuilder.
42    *
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
48    *   The config factory.
49    * @param \Drupal\Core\Messenger\MessengerInterface $messenger
50    *   The messenger.
51    */
52   public function __construct(EntityTypeInterface $entity_type, EntityStorageInterface $storage, ConfigFactoryInterface $config_factory, MessengerInterface $messenger) {
53     parent::__construct($entity_type, $storage);
54
55     $this->configFactory = $config_factory;
56     $this->messenger = $messenger;
57   }
58
59   /**
60    * {@inheritdoc}
61    */
62   public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) {
63     return new static(
64       $entity_type,
65       $container->get('entity.manager')->getStorage($entity_type->id()),
66       $container->get('config.factory'),
67       $container->get('messenger')
68     );
69   }
70
71   /**
72    * {@inheritdoc}
73    */
74   public function getFormId() {
75     return 'filter_admin_overview';
76   }
77
78   /**
79    * {@inheritdoc}
80    */
81   public function load() {
82     // Only list enabled filters.
83     return array_filter(parent::load(), function ($entity) {
84       return $entity->status();
85     });
86   }
87
88   /**
89    * {@inheritdoc}
90    */
91   public function buildHeader() {
92     $header['label'] = $this->t('Name');
93     $header['roles'] = $this->t('Roles');
94     return $header + parent::buildHeader();
95   }
96
97   /**
98    * {@inheritdoc}
99    */
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();
104     $row['roles'] = [];
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');
109       }
110       else {
111         $row['roles']['#markup'] = $this->t('This format is shown when no other formats are available');
112       }
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>';
118     }
119     else {
120       $row['roles'] = [
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'],
125       ];
126     }
127
128     return $row + parent::buildRow($entity);
129   }
130
131   /**
132    * {@inheritdoc}
133    */
134   public function getDefaultOperations(EntityInterface $entity) {
135     $operations = parent::getDefaultOperations($entity);
136
137     if (isset($operations['edit'])) {
138       $operations['edit']['title'] = $this->t('Configure');
139     }
140
141     // The fallback format may not be disabled.
142     if ($entity->isFallbackFormat()) {
143       unset($operations['disable']);
144     }
145
146     return $operations;
147   }
148
149   /**
150    * {@inheritdoc}
151    */
152   public function buildForm(array $form, FormStateInterface $form_state) {
153     $form = parent::buildForm($form, $form_state);
154     $form['actions']['submit']['#value'] = $this->t('Save');
155     return $form;
156   }
157
158   /**
159    * {@inheritdoc}
160    */
161   public function submitForm(array &$form, FormStateInterface $form_state) {
162     parent::submitForm($form, $form_state);
163
164     filter_formats_reset();
165     $this->messenger->addStatus($this->t('The text format ordering has been saved.'));
166   }
167
168 }