diff --git a/core/modules/filter/filter.module b/core/modules/filter/filter.module index 4ecb908..ab174b4 100644 --- a/core/modules/filter/filter.module +++ b/core/modules/filter/filter.module @@ -6,9 +6,8 @@ */ use Drupal\Core\Cache\CacheBackendInterface; -use Drupal\Core\Language\Language; use Drupal\Core\Template\Attribute; -use Drupal\filter\Plugin\Core\Entity\FilterFormat; +use Drupal\filter\FilterFormatInterface; use Drupal\user\UserInterface; /** @@ -162,19 +161,6 @@ function filter_local_actions() { } /** - * Determines if a text format exists. - * - * @param $format_id - * The ID of the text format to check. - * - * @return bool - * TRUE if the text format exists, FALSE otherwise. - */ -function filter_format_exists($format_id) { - return entity_load('filter_format', $format_id); -} - -/** * Implements hook_permission(). */ function filter_permission() { @@ -193,7 +179,7 @@ function filter_permission() { if (!empty($permission)) { // Only link to the text format configuration page if the user who is // viewing this will have access to that page. - $format_name_replacement = user_access('administer filters') ? l($format->name, 'admin/config/content/formats/manage/' . $format->format) : drupal_placeholder($format->name); + $format_name_replacement = user_access('administer filters') ? l($format->label(), 'admin/config/content/formats/manage/' . $format->id()) : drupal_placeholder($format->label()); $perms[$permission] = array( 'title' => t("Use the !text_format text format", array('!text_format' => $format_name_replacement,)), 'description' => drupal_placeholder(t('Warning: This permission may have security implications depending on how the text format is configured.')), @@ -211,7 +197,7 @@ function filter_permission() { * account will be returned. All enabled formats will be returned otherwise. * Defaults to NULL. * - * @return array + * @return \Drupal\filter\FilterFormatInterface[] * An array of text format objects, keyed by the format ID and ordered by * weight. * @@ -257,15 +243,15 @@ function filter_formats_reset() { /** * Retrieves a list of roles that are allowed to use a given text format. * - * @param $format + * @param \Drupal\filter\FilterFormatInterface $format * An object representing the text format. * * @return * An array of role names, keyed by role ID. */ -function filter_get_roles_by_format($format) { +function filter_get_roles_by_format(FilterFormatInterface $format) { // Handle the fallback format upfront (all roles have access to this format). - if ($format->format == filter_fallback_format()) { + if ($format->isFallbackFormat()) { return user_role_names(); } // Do not list any roles if the permission does not exist. @@ -279,7 +265,7 @@ function filter_get_roles_by_format($format) { * @param $rid * The user role ID to retrieve text formats for. * - * @return + * @return \Drupal\filter\FilterFormatInterface[] * An array of text format objects that are allowed for the role, keyed by * the text format ID and ordered by weight. */ @@ -288,7 +274,7 @@ function filter_get_formats_by_role($rid) { foreach (filter_formats() as $format) { $roles = filter_get_roles_by_format($format); if (isset($roles[$rid])) { - $formats[$format->format] = $format; + $formats[$format->id()] = $format; } } return $formats; @@ -344,10 +330,12 @@ function filter_default_format(UserInterface $account = NULL) { function filter_get_filter_types_by_format($format_id) { $filter_types = array(); - $filters = entity_load('filter_format', $format_id)->filters(); - foreach ($filters as $filter) { - if ($filter->status) { - $filter_types[] = $filter->getType(); + if ($format_id) { + $filters = entity_load('filter_format', $format_id)->filters(); + foreach ($filters as $filter) { + if ($filter->status) { + $filter_types[] = $filter->getType(); + } } } @@ -657,7 +645,7 @@ function check_markup($text, $format_id = NULL, $langcode = '', $cache = FALSE, // automatically flushed when the text format is updated. // @see \Drupal\filter\Plugin\Core\Entity\FilterFormat::save() if ($cache) { - cache('filter')->set($cache_id, $text, CacheBackendInterface::CACHE_PERMANENT, array('filter_format' => $format->format)); + cache('filter')->set($cache_id, $text, CacheBackendInterface::CACHE_PERMANENT, array('filter_format' => $format->id())); } return $text; @@ -759,9 +747,10 @@ function filter_process_format($element) { '#attributes' => array('class' => array('filter-guidelines')), '#weight' => 20, ); + $options = array(); foreach ($formats as $format) { - $options[$format->format] = $format->name; - $element['format']['guidelines'][$format->format] = array( + $options[$format->id()] = $format->label(); + $element['format']['guidelines'][$format->id()] = array( '#theme' => 'filter_guidelines', '#format' => $format, ); @@ -900,7 +889,7 @@ function _filter_tips($format_id, $long = FALSE) { if ($filter->status) { $tip = $filter->tips($long); if (isset($tip)) { - $tips[$format->name][$name] = array('tip' => $tip, 'id' => $name); + $tips[$format->label()][$name] = array('tip' => $tip, 'id' => $name); } } } diff --git a/core/modules/filter/lib/Drupal/filter/Access/FormatDisableCheck.php b/core/modules/filter/lib/Drupal/filter/Access/FormatDisableCheck.php index de5acea..36ff28b 100644 --- a/core/modules/filter/lib/Drupal/filter/Access/FormatDisableCheck.php +++ b/core/modules/filter/lib/Drupal/filter/Access/FormatDisableCheck.php @@ -28,7 +28,7 @@ public function applies(Route $route) { */ public function access(Route $route, Request $request) { if ($format = $request->attributes->get('filter_format')) { - return user_access('administer filters') && ($format->format != filter_fallback_format()); + return user_access('administer filters') && !$format->isFallbackFormat(); } return FALSE; diff --git a/core/modules/filter/lib/Drupal/filter/FilterFormatAccessController.php b/core/modules/filter/lib/Drupal/filter/FilterFormatAccessController.php index 1ebdaf4..1bc04d5 100644 --- a/core/modules/filter/lib/Drupal/filter/FilterFormatAccessController.php +++ b/core/modules/filter/lib/Drupal/filter/FilterFormatAccessController.php @@ -22,7 +22,7 @@ class FilterFormatAccessController extends EntityAccessController { protected function checkAccess(EntityInterface $entity, $operation, $langcode, UserInterface $account) { // Handle special cases up front. All users have access to the fallback // format. - if ($entity->id() == filter_fallback_format()) { + if ($entity->isFallbackFormat()) { return TRUE; } // Check the permission if one exists; otherwise, we have a non-existent diff --git a/core/modules/filter/lib/Drupal/filter/FilterFormatFormControllerBase.php b/core/modules/filter/lib/Drupal/filter/FilterFormatFormControllerBase.php index f31224b..0d54c2b 100644 --- a/core/modules/filter/lib/Drupal/filter/FilterFormatFormControllerBase.php +++ b/core/modules/filter/lib/Drupal/filter/FilterFormatFormControllerBase.php @@ -78,7 +78,7 @@ public function form(array $form, array &$form_state) { '#default_value' => $format->id(), '#maxlength' => 255, '#machine_name' => array( - 'exists' => 'filter_format_exists', + 'exists' => array($this, 'exists'), 'source' => array('name'), ), '#disabled' => !$format->isNew(), @@ -189,6 +189,22 @@ public function form(array $form, array &$form_state) { } /** + * Determines if the format already exists. + * + * @param string $format_id + * The format ID + * + * @return bool + * TRUE if the format exists, FALSE otherwise. + */ + public function exists($format_id) { + return (bool) $this->queryFactory + ->get('filter_format') + ->condition('format', $format_id) + ->execute(); + } + + /** * {@inheritdoc} */ public function validate(array $form, array &$form_state) { diff --git a/core/modules/filter/lib/Drupal/filter/FilterFormatInterface.php b/core/modules/filter/lib/Drupal/filter/FilterFormatInterface.php index 92f28cf..1da5060 100644 --- a/core/modules/filter/lib/Drupal/filter/FilterFormatInterface.php +++ b/core/modules/filter/lib/Drupal/filter/FilterFormatInterface.php @@ -52,7 +52,7 @@ public function isFallbackFormat(); /** * Returns the machine-readable permission name for the text format. * - * @return string|false + * @return string|bool * The machine-readable permission name, or FALSE if the text format is * malformed or is the fallback format (which is available to all users). */ diff --git a/core/modules/filter/lib/Drupal/filter/Plugin/Core/Entity/FilterFormat.php b/core/modules/filter/lib/Drupal/filter/Plugin/Core/Entity/FilterFormat.php index 647f135..84015e3 100644 --- a/core/modules/filter/lib/Drupal/filter/Plugin/Core/Entity/FilterFormat.php +++ b/core/modules/filter/lib/Drupal/filter/Plugin/Core/Entity/FilterFormat.php @@ -264,11 +264,7 @@ public function isFallbackFormat() { * {@inheritdoc} */ public function getPermissionName() { - $format = $this->id(); - if ($format && $format != filter_fallback_format()) { - return "use text format $format"; - } - return FALSE; + return !$this->isFallbackFormat() ? 'use text format ' . $this->id() : FALSE; } } diff --git a/core/modules/filter/lib/Drupal/filter/Tests/FilterAdminTest.php b/core/modules/filter/lib/Drupal/filter/Tests/FilterAdminTest.php index 94d7465..dd89f9a 100644 --- a/core/modules/filter/lib/Drupal/filter/Tests/FilterAdminTest.php +++ b/core/modules/filter/lib/Drupal/filter/Tests/FilterAdminTest.php @@ -127,7 +127,7 @@ function testFilterAdmin() { $plain = 'plain_text'; // Check that the fallback format exists and cannot be disabled. - $this->assertTrue($plain == filter_fallback_format(), 'The fallback format is set to plain text.'); + $this->assertTrue($plain->isFallbackFormat(), 'The fallback format is set to plain text.'); $this->drupalGet('admin/config/content/formats'); $this->assertNoRaw('admin/config/content/formats/manage/' . $plain . '/disable', 'Disable link for the fallback format not found.'); $this->drupalGet('admin/config/content/formats/manage/' . $plain . '/disable'); diff --git a/core/modules/filter/lib/Drupal/filter/Tests/FilterFormatAccessTest.php b/core/modules/filter/lib/Drupal/filter/Tests/FilterFormatAccessTest.php index 7fcc593..413daf2 100644 --- a/core/modules/filter/lib/Drupal/filter/Tests/FilterFormatAccessTest.php +++ b/core/modules/filter/lib/Drupal/filter/Tests/FilterFormatAccessTest.php @@ -292,7 +292,7 @@ function testFormatWidgetPermissions() { $this->drupalPost('node/' . $node->nid . '/edit', $edit, t('Save')); $this->assertUrl('node/' . $node->nid); foreach (filter_formats() as $format) { - if ($format->format != filter_fallback_format()) { + if (!$format->isFallbackFormat()) { $format->disable()->save(); } } diff --git a/core/modules/text/lib/Drupal/text/Tests/TextFieldTest.php b/core/modules/text/lib/Drupal/text/Tests/TextFieldTest.php index aa05563..abd7bf9 100644 --- a/core/modules/text/lib/Drupal/text/Tests/TextFieldTest.php +++ b/core/modules/text/lib/Drupal/text/Tests/TextFieldTest.php @@ -188,7 +188,7 @@ function _testTextfieldWidgetsFormatted($field_type, $widget_type) { // Disable all text formats besides the plain text fallback format. $this->drupalLogin($this->admin_user); foreach (filter_formats() as $format) { - if ($format->format != filter_fallback_format()) { + if (!$format->isFallbackFormat()) { $this->drupalPost('admin/config/content/formats/manage/' . $format->format . '/disable', array(), t('Disable')); } }