diff --git a/core/modules/filter/src/Element/ProcessedText.php b/core/modules/filter/src/Element/ProcessedText.php index 06b6a44..41d1af8 100644 --- a/core/modules/filter/src/Element/ProcessedText.php +++ b/core/modules/filter/src/Element/ProcessedText.php @@ -41,26 +41,25 @@ public function getInfo() { * * Runs all the enabled filters on a piece of text. * - * Note: Because filters can inject JavaScript or execute PHP code, security is - * vital here. When a user supplies a text format, you should validate it using - * $format->access() before accepting/using it. This is normally done in the - * validation stage of the Form API. You should for example never make a + * Note: Because filters can inject JavaScript or execute PHP code, security + * is vital here. When a user supplies a text format, you should validate it + * using $format->access() before accepting/using it. This is normally done in + * the validation stage of the Form API. You should for example never make a * preview of content in a disallowed format. * * @param array $element * A structured array with the following key-value pairs: * - #text: containing the text to be filtered * - #format: containing the machine name of the filter format to be used to - * filter the text. Defaults to the fallback format. See - * filter_fallback_format(). + * filter the text. Defaults to the fallback format. * - #langcode: the language code of the text to be filtered, e.g. 'en' for * English. This allows filters to be language-aware so language-specific * text replacement can be implemented. Defaults to an empty string. * - #filter_types_to_skip: an array of filter types to skip, or an empty - * array (default) to skip no filter types. All of the format's filters will - * be applied, except for filters of the types that are marked to be skipped. - * FilterInterface::TYPE_HTML_RESTRICTOR is the only type that cannot be - * skipped. + * array (default) to skip no filter types. All of the format's filters + * will be applied, except for filters of the types that are marked to be + * skipped. FilterInterface::TYPE_HTML_RESTRICTOR is the only type that + * cannot be skipped. * * @return array * The passed-in element with the filtered text in '#markup'. @@ -74,12 +73,12 @@ public static function preRenderText($element) { $langcode = $element['#langcode']; if (!isset($format_id)) { - $format_id = filter_fallback_format(); + $format_id = static::configFactory()->get('filter.settings')->get('fallback_format'); } // If the requested text format does not exist, the text cannot be filtered. /** @var \Drupal\filter\Entity\FilterFormat $format **/ if (!$format = FilterFormat::load($format_id)) { - \Drupal::logger('filter')->alert('Missing text format: %format.', array('%format' => $format_id)); + static::logger('filter')->alert('Missing text format: %format.', array('%format' => $format_id)); $element['#markup'] = ''; return $element; } @@ -97,11 +96,10 @@ public static function preRenderText($element) { $text = str_replace(array("\r\n", "\r"), "\n", $text); // Get a complete list of filters, ordered properly. - /** @var \Drupal\filter\FilterBag $filters **/ + /** @var \Drupal\filter\Plugin\FilterInterface[] $filters **/ $filters = $format->filters(); - // Give filters the chance to escape HTML-like data such as code or formulas. - /** @var \Drupal\filter\Plugin\FilterInterface $filter **/ + // Give filters a chance to escape HTML-like data such as code or formulas. foreach ($filters as $filter) { if ($filter_must_be_applied($filter)) { $text = $filter->prepare($text, $langcode); @@ -151,4 +149,22 @@ public static function preRenderText($element) { return $element; } + /** + * Wraps a logger channel. + * + * @return \Psr\Log\LoggerInterface + */ + protected static function logger($channel) { + return \Drupal::logger($channel); + } + + /** + * Wraps the config factory. + * + * @return \Drupal\Core\Config\ConfigFactoryInterface + */ + protected static function configFactory() { + return \Drupal::configFactory(); + } + } diff --git a/core/modules/filter/src/Element/TextFormat.php b/core/modules/filter/src/Element/TextFormat.php index 9b99832..35d2bdf 100644 --- a/core/modules/filter/src/Element/TextFormat.php +++ b/core/modules/filter/src/Element/TextFormat.php @@ -42,8 +42,8 @@ public function getInfo() { * - value: Holds the original element, having its #type changed to the value * of #base_type or 'textarea' by default. * - format: Holds the text format details and the text format selection, - * using the text format ID specified in #format or the user's default format - * by default, if NULL. + * using the text format ID specified in #format or the user's default + * format by default, if NULL. * * The resulting value for the element will be an array holding the value and * the format. For example, the value for the body element will be: @@ -60,8 +60,8 @@ public function getInfo() { * - #format: (optional) The text format ID to preselect. If omitted, the * default format for the current user will be used. * - #allowed_formats: (optional) An array of text format IDs that are - * available for this element. If omitted, all text formats that the current - * user has access to will be allowed. + * available for this element. If omitted, all text formats that the + * current user has access to will be allowed. * @param \Drupal\Core\Form\FormStateInterface $form_state * The current state of the form. * @param array $complete_form @@ -71,7 +71,7 @@ public function getInfo() { * The form element. */ public static function processFormat(&$element, FormStateInterface $form_state, &$complete_form) { - $user = \Drupal::currentUser(); + $user = static::currentUser(); // Ensure that children appear as subkeys of this element. $element['#tree'] = TRUE; @@ -103,7 +103,7 @@ public static function processFormat(&$element, FormStateInterface $form_state, } $element['value']['#type'] = $element['#base_type']; - $element['value'] += \Drupal::service('element_info')->getInfo($element['#base_type']); + $element['value'] += static::elementInfo()->getInfo($element['#base_type']); // Make sure the #default_value key is set, so we can use it below. $element['value'] += array('#default_value' => ''); @@ -134,16 +134,17 @@ public static function processFormat(&$element, FormStateInterface $form_state, $element['#format'] = reset($formats)->format; } - // If #allowed_formats is set, the list of formats must not be modified in any - // way. Otherwise, however, if all of the following conditions are true, + // If #allowed_formats is set, the list of formats must not be modified in + // any way. Otherwise, however, if all of the following conditions are true, // remove the fallback format from the list of formats: // 1. The 'always_show_fallback_choice' filter setting has not been activated. // 2. Multiple text formats are available. // 3. The fallback format is not the default format. // The 'always_show_fallback_choice' filter setting is a hidden setting that // has no UI. It defaults to FALSE. - if (!isset($element['#allowed_formats']) && !\Drupal::config('filter.settings')->get('always_show_fallback_choice')) { - $fallback_format = filter_fallback_format(); + $config = static::configFactory()->get('filter.settings'); + if (!isset($element['#allowed_formats']) && !$config->get('always_show_fallback_choice')) { + $fallback_format = $config->get('fallback_format'); if ($element['#format'] !== $fallback_format && count($formats) > 1) { unset($formats[$fallback_format]); } @@ -186,7 +187,7 @@ public static function processFormat(&$element, FormStateInterface $form_state, $format_exists = isset($all_formats[$element['#format']]); $format_allowed = !isset($element['#allowed_formats']) || in_array($element['#format'], $element['#allowed_formats']); $user_has_access = isset($formats[$element['#format']]); - $user_is_admin = \Drupal::currentUser()->hasPermission('administer filters'); + $user_is_admin = $user->hasPermission('administer filters'); // If the stored format does not exist or if it is not among the allowed // formats for this textarea, administrators have to assign a new format. @@ -198,8 +199,9 @@ public static function processFormat(&$element, FormStateInterface $form_state, $element['format']['format']['#access'] = TRUE; } // Disable this widget, if the user is not allowed to use the stored format, - // or if the stored format does not exist. The 'administer filters' permission - // only grants access to the filter administration, not to all formats. + // or if the stored format does not exist. The 'administer filters' + // permission only grants access to the filter administration, not to all + // formats. elseif (!$user_has_access || !$format_exists) { // Overload default values into #value to make them unalterable. $element['value']['#value'] = $element['value']['#default_value']; @@ -229,4 +231,31 @@ public static function processFormat(&$element, FormStateInterface $form_state, return $element; } + /** + * Wraps the current user. + * + * \Drupal\Core\Session\AccountInterface + */ + protected static function currentUser() { + return \Drupal::currentUser(); + } + + /** + * Wraps the config factory. + * + * @return \Drupal\Core\Config\ConfigFactoryInterface + */ + protected static function configFactory() { + return \Drupal::configFactory(); + } + + /** + * Wraps the element info service. + * + * @return \Drupal\Core\Render\ElementInfoManagerInterface + */ + protected static function elementInfo() { + return \Drupal::service('element_info'); + } + }