From 1695ef29ec578cb33c4385296c9d053a89988375 Mon Sep 17 00:00:00 2001 From: Alexander Shumenko Date: Sun, 19 Jul 2015 13:57:04 +0300 Subject: [PATCH] Filter format module patch --- core/modules/filter/filter.routing.yml | 8 +++ core/modules/filter/src/Entity/FilterFormat.php | 23 +++++++-- .../src/FilterFormatAccessControlHandler.php | 2 +- .../modules/filter/src/FilterFormatListBuilder.php | 26 ++++++---- core/modules/filter/src/Form/FilterDisableForm.php | 2 +- core/modules/filter/src/Form/FilterEnableForm.php | 57 ++++++++++++++++++++++ core/modules/filter/src/Tests/FilterAdminTest.php | 9 ++++ 7 files changed, 112 insertions(+), 15 deletions(-) create mode 100644 core/modules/filter/src/Form/FilterEnableForm.php diff --git a/core/modules/filter/filter.routing.yml b/core/modules/filter/filter.routing.yml index 9fff1b1..16155b6 100644 --- a/core/modules/filter/filter.routing.yml +++ b/core/modules/filter/filter.routing.yml @@ -46,3 +46,11 @@ entity.filter_format.disable: _title: 'Disable text format' requirements: _entity_access: 'filter_format.disable' + +entity.filter_format.enable: + path: '/admin/config/content/formats/manage/{filter_format}/enable' + defaults: + _entity_form: 'filter_format.enable' + _title: 'Enable text format' + requirements: + _entity_access: 'filter_format.enable' diff --git a/core/modules/filter/src/Entity/FilterFormat.php b/core/modules/filter/src/Entity/FilterFormat.php index cf61f7f..a004bdf 100644 --- a/core/modules/filter/src/Entity/FilterFormat.php +++ b/core/modules/filter/src/Entity/FilterFormat.php @@ -25,7 +25,8 @@ * "form" = { * "add" = "Drupal\filter\FilterFormatAddForm", * "edit" = "Drupal\filter\FilterFormatEditForm", - * "disable" = "Drupal\filter\Form\FilterDisableForm" + * "disable" = "Drupal\filter\Form\FilterDisableForm", + * "enable" = "Drupal\filter\Form\FilterEnableForm" * }, * "list_builder" = "Drupal\filter\FilterFormatListBuilder", * "access" = "Drupal\filter\FilterFormatAccessControlHandler", @@ -40,7 +41,8 @@ * }, * links = { * "edit-form" = "/admin/config/content/formats/manage/{filter_format}", - * "disable" = "/admin/config/content/formats/manage/{filter_format}/disable" + * "disable" = "/admin/config/content/formats/manage/{filter_format}/disable", + * "enable" = "/admin/config/content/formats/manage/{filter_format}/enable" * }, * config_export = { * "name", @@ -182,7 +184,7 @@ public function toArray() { public function disable() { parent::disable(); - // Allow modules to react on text format deletion. + // Allow modules to react on text format disabling. \Drupal::moduleHandler()->invokeAll('filter_format_disable', array($this)); // Clear the filter cache whenever a text format is disabled. @@ -194,6 +196,21 @@ public function disable() { /** * {@inheritdoc} */ + public function enable() { + parent::enable(); + + // Allow modules to react on text format enabling. + \Drupal::moduleHandler()->invokeAll('filter_format_enable', array($this)); + + // Clear the filter cache whenever a text format is enabled. + filter_formats_reset(); + + return $this; + } + + /** + * {@inheritdoc} + */ public function preSave(EntityStorageInterface $storage) { // Ensure the filters have been sorted before saving. $this->filters()->sort(); diff --git a/core/modules/filter/src/FilterFormatAccessControlHandler.php b/core/modules/filter/src/FilterFormatAccessControlHandler.php index 337dcf7..fb0f780 100644 --- a/core/modules/filter/src/FilterFormatAccessControlHandler.php +++ b/core/modules/filter/src/FilterFormatAccessControlHandler.php @@ -46,7 +46,7 @@ protected function checkAccess(EntityInterface $filter_format, $operation, $lang return AccessResult::forbidden(); } - if (in_array($operation, array('disable', 'update'))) { + if (in_array($operation, array('disable', 'update', 'enable'))) { return parent::checkAccess($filter_format, $operation, $langcode, $account); } diff --git a/core/modules/filter/src/FilterFormatListBuilder.php b/core/modules/filter/src/FilterFormatListBuilder.php index 8f46cbc..5df6c75 100644 --- a/core/modules/filter/src/FilterFormatListBuilder.php +++ b/core/modules/filter/src/FilterFormatListBuilder.php @@ -72,16 +72,6 @@ public function getFormId() { /** * {@inheritdoc} */ - public function load() { - // Only list enabled filters. - return array_filter(parent::load(), function ($entity) { - return $entity->status(); - }); - } - - /** - * {@inheritdoc} - */ public function buildHeader() { $header['label'] = $this->t('Name'); $header['roles'] = $this->t('Roles'); @@ -111,6 +101,12 @@ public function buildRow(EntityInterface $entity) { $roles_markup = $roles ? implode(', ', $roles) : $this->t('No roles may use this format'); } + // Show warning message for disabled text formats. + if (!$entity->status()) { + $roles_markup = SafeMarkup::placeholder($this->t('This format has been disabled and is no longer available. + Any content stored with this format will not be displayed.')); + } + $row['roles'] = !empty($this->weightKey) ? array('#markup' => $roles_markup) : $roles_markup; return $row + parent::buildRow($entity); @@ -131,6 +127,16 @@ public function getDefaultOperations(EntityInterface $entity) { unset($operations['disable']); } + // Remove Disable and Configure operation for already disabled formats. + if (!$entity->status()) { + if (isset($operations['disable'])) { + unset($operations['disable']); + } + if (isset($operations['edit'])) { + unset($operations['edit']); + } + } + return $operations; } diff --git a/core/modules/filter/src/Form/FilterDisableForm.php b/core/modules/filter/src/Form/FilterDisableForm.php index 5795d8f..95761b6 100644 --- a/core/modules/filter/src/Form/FilterDisableForm.php +++ b/core/modules/filter/src/Form/FilterDisableForm.php @@ -41,7 +41,7 @@ public function getConfirmText() { * {@inheritdoc} */ public function getDescription() { - return $this->t('Disabled text formats are completely removed from the administrative interface, and any content stored with that format will not be displayed. This action cannot be undone.'); + return $this->t('Any content stored with that format will not be displayed.'); } /** diff --git a/core/modules/filter/src/Form/FilterEnableForm.php b/core/modules/filter/src/Form/FilterEnableForm.php new file mode 100644 index 0000000..70f03c0 --- /dev/null +++ b/core/modules/filter/src/Form/FilterEnableForm.php @@ -0,0 +1,57 @@ +t('Are you sure you want to enable the text format %format?', array('%format' => $this->entity->label())); + } + + /** + * {@inheritdoc} + */ + public function getCancelUrl() { + return new Url('filter.admin_overview'); + } + + /** + * {@inheritdoc} + */ + public function getConfirmText() { + return $this->t('Enable'); + } + + /** + * {@inheritdoc} + */ + public function getDescription() { + return $this->t('You are going to enable text format. Any content stored with this format will be displayed again.'); + } + + /** + * {@inheritdoc} + */ + public function submitForm(array &$form, FormStateInterface $form_state) { + $this->entity->enable()->save(); + drupal_set_message($this->t('Enabled text format %format.', array('%format' => $this->entity->label()))); + + $form_state->setRedirectUrl($this->getCancelUrl()); + } + +} diff --git a/core/modules/filter/src/Tests/FilterAdminTest.php b/core/modules/filter/src/Tests/FilterAdminTest.php index 7e6a2d3..54e42fd 100644 --- a/core/modules/filter/src/Tests/FilterAdminTest.php +++ b/core/modules/filter/src/Tests/FilterAdminTest.php @@ -161,6 +161,15 @@ function testFormatAdmin() { $this->drupalGet('admin/config/content/formats/manage/' . $format_id); $this->assertResponse(404, 'Disabled text format no longer exists.'); + // Enable text format. + $this->assertLinkByHref('admin/config/content/formats/manage/' . $format_id . '/enable'); + $this->drupalGet('admin/config/content/formats/manage/' . $format_id . '/enable'); + $this->drupalPostForm(NULL, array(), t('Enable')); + + // Verify that enable text format available. + $this->drupalGet('admin/config/content/formats/manage/' . $format_id); + $this->assertResponse(200, 'Text format exists and enabled.'); + // Attempt to create a format of the same machine name as the disabled // format but with a different human readable name. $edit = array( -- 1.9.1