PageRenderTime 50ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/web/core/modules/filter/src/FilterFormatFormBase.php

https://gitlab.com/mohamed_hussein/prodt
PHP | 235 lines | 168 code | 23 blank | 44 comment | 10 complexity | a8d0557dcd9203b851f167ab13e10251 MD5 | raw file
  1. <?php
  2. namespace Drupal\filter;
  3. use Drupal\Core\Entity\EntityForm;
  4. use Drupal\Core\Form\FormStateInterface;
  5. use Drupal\filter\Plugin\Filter\FilterNull;
  6. /**
  7. * Provides a base form for a filter format.
  8. */
  9. abstract class FilterFormatFormBase extends EntityForm {
  10. /**
  11. * {@inheritdoc}
  12. */
  13. public function form(array $form, FormStateInterface $form_state) {
  14. $format = $this->entity;
  15. $is_fallback = ($format->id() == $this->config('filter.settings')->get('fallback_format'));
  16. $form['#tree'] = TRUE;
  17. $form['#attached']['library'][] = 'filter/drupal.filter.admin';
  18. $form['name'] = [
  19. '#type' => 'textfield',
  20. '#title' => $this->t('Name'),
  21. '#default_value' => $format->label(),
  22. '#required' => TRUE,
  23. '#weight' => -30,
  24. ];
  25. $form['format'] = [
  26. '#type' => 'machine_name',
  27. '#required' => TRUE,
  28. '#default_value' => $format->id(),
  29. '#maxlength' => 255,
  30. '#machine_name' => [
  31. 'exists' => [$this, 'exists'],
  32. 'source' => ['name'],
  33. ],
  34. '#disabled' => !$format->isNew(),
  35. '#weight' => -20,
  36. ];
  37. // Add user role access selection.
  38. $form['roles'] = [
  39. '#type' => 'checkboxes',
  40. '#title' => $this->t('Roles'),
  41. '#options' => array_map('\Drupal\Component\Utility\Html::escape', user_role_names()),
  42. '#disabled' => $is_fallback,
  43. '#weight' => -10,
  44. ];
  45. if ($is_fallback) {
  46. $form['roles']['#description'] = $this->t('All roles for this text format must be enabled and cannot be changed.');
  47. }
  48. if (!$format->isNew()) {
  49. // If editing an existing text format, pre-select its current permissions.
  50. $form['roles']['#default_value'] = array_keys(filter_get_roles_by_format($format));
  51. }
  52. // Create filter plugin instances for all available filters, including both
  53. // enabled/configured ones as well as new and not yet unconfigured ones.
  54. $filters = $format->filters();
  55. foreach ($filters as $filter_id => $filter) {
  56. // When a filter is missing, it is replaced by the null filter. Remove it
  57. // here, so that saving the form will remove the missing filter.
  58. if ($filter instanceof FilterNull) {
  59. $this->messenger()->addWarning($this->t('The %filter filter is missing, and will be removed once this format is saved.', ['%filter' => $filter_id]));
  60. $filters->removeInstanceID($filter_id);
  61. }
  62. }
  63. // Filter status.
  64. $form['filters']['status'] = [
  65. '#type' => 'item',
  66. '#title' => $this->t('Enabled filters'),
  67. '#prefix' => '<div id="filters-status-wrapper">',
  68. '#suffix' => '</div>',
  69. // This item is used as a pure wrapping container with heading. Ignore its
  70. // value, since 'filters' should only contain filter definitions.
  71. // See https://www.drupal.org/node/1829202.
  72. '#input' => FALSE,
  73. ];
  74. // Filter order (tabledrag).
  75. $form['filters']['order'] = [
  76. '#type' => 'table',
  77. // For filter.admin.js
  78. '#attributes' => ['id' => 'filter-order'],
  79. '#title' => $this->t('Filter processing order'),
  80. '#tabledrag' => [
  81. [
  82. 'action' => 'order',
  83. 'relationship' => 'sibling',
  84. 'group' => 'filter-order-weight',
  85. ],
  86. ],
  87. '#tree' => FALSE,
  88. '#input' => FALSE,
  89. '#theme_wrappers' => ['form_element'],
  90. ];
  91. // Filter settings.
  92. $form['filter_settings'] = [
  93. '#type' => 'vertical_tabs',
  94. '#title' => $this->t('Filter settings'),
  95. ];
  96. foreach ($filters as $name => $filter) {
  97. $form['filters']['status'][$name] = [
  98. '#type' => 'checkbox',
  99. '#title' => $filter->getLabel(),
  100. '#default_value' => $filter->status,
  101. '#parents' => ['filters', $name, 'status'],
  102. '#description' => $filter->getDescription(),
  103. '#weight' => $filter->weight,
  104. ];
  105. $form['filters']['order'][$name]['#attributes']['class'][] = 'draggable';
  106. $form['filters']['order'][$name]['#weight'] = $filter->weight;
  107. $form['filters']['order'][$name]['filter'] = [
  108. '#markup' => $filter->getLabel(),
  109. ];
  110. $form['filters']['order'][$name]['weight'] = [
  111. '#type' => 'weight',
  112. '#title' => $this->t('Weight for @title', ['@title' => $filter->getLabel()]),
  113. '#title_display' => 'invisible',
  114. '#delta' => 50,
  115. '#default_value' => $filter->weight,
  116. '#parents' => ['filters', $name, 'weight'],
  117. '#attributes' => ['class' => ['filter-order-weight']],
  118. ];
  119. // Retrieve the settings form of the filter plugin. The plugin should not be
  120. // aware of the text format. Therefore, it only receives a set of minimal
  121. // base properties to allow advanced implementations to work.
  122. $settings_form = [
  123. '#parents' => ['filters', $name, 'settings'],
  124. '#tree' => TRUE,
  125. ];
  126. $settings_form = $filter->settingsForm($settings_form, $form_state);
  127. if (!empty($settings_form)) {
  128. $form['filters']['settings'][$name] = [
  129. '#type' => 'details',
  130. '#title' => $filter->getLabel(),
  131. '#open' => TRUE,
  132. '#weight' => $filter->weight,
  133. '#parents' => ['filters', $name, 'settings'],
  134. '#group' => 'filter_settings',
  135. ];
  136. $form['filters']['settings'][$name] += $settings_form;
  137. }
  138. }
  139. return parent::form($form, $form_state);
  140. }
  141. /**
  142. * Determines if the format already exists.
  143. *
  144. * @param string $format_id
  145. * The format ID
  146. *
  147. * @return bool
  148. * TRUE if the format exists, FALSE otherwise.
  149. */
  150. public function exists($format_id) {
  151. return (bool) $this->entityTypeManager
  152. ->getStorage('filter_format')
  153. ->getQuery()
  154. ->condition('format', $format_id)
  155. ->execute();
  156. }
  157. /**
  158. * {@inheritdoc}
  159. */
  160. public function validateForm(array &$form, FormStateInterface $form_state) {
  161. parent::validateForm($form, $form_state);
  162. // @todo Move trimming upstream.
  163. $format_format = trim($form_state->getValue('format'));
  164. $format_name = trim($form_state->getValue('name'));
  165. // Ensure that the values to be saved later are exactly the ones validated.
  166. $form_state->setValueForElement($form['format'], $format_format);
  167. $form_state->setValueForElement($form['name'], $format_name);
  168. $format_exists = $this->entityTypeManager
  169. ->getStorage('filter_format')
  170. ->getQuery()
  171. ->condition('format', $format_format, '<>')
  172. ->condition('name', $format_name)
  173. ->execute();
  174. if ($format_exists) {
  175. $form_state->setErrorByName('name', $this->t('Text format names must be unique. A format named %name already exists.', ['%name' => $format_name]));
  176. }
  177. }
  178. /**
  179. * {@inheritdoc}
  180. */
  181. public function submitForm(array &$form, FormStateInterface $form_state) {
  182. parent::submitForm($form, $form_state);
  183. // Add the submitted form values to the text format, and save it.
  184. $format = $this->entity;
  185. foreach ($form_state->getValues() as $key => $value) {
  186. if ($key != 'filters') {
  187. $format->set($key, $value);
  188. }
  189. else {
  190. foreach ($value as $instance_id => $config) {
  191. $format->setFilterConfig($instance_id, $config);
  192. }
  193. }
  194. }
  195. $format->save();
  196. // Save user permissions.
  197. if ($permission = $format->getPermissionName()) {
  198. foreach ($form_state->getValue('roles') as $rid => $enabled) {
  199. user_role_change_permissions($rid, [$permission => $enabled]);
  200. }
  201. }
  202. return $this->entity;
  203. }
  204. /**
  205. * {@inheritdoc}
  206. */
  207. protected function actions(array $form, FormStateInterface $form_state) {
  208. $actions = parent::actions($form, $form_state);
  209. $actions['submit']['#value'] = $this->t('Save configuration');
  210. return $actions;
  211. }
  212. }