diff --git a/core/modules/image/config/schema/image.schema.yml b/core/modules/image/config/schema/image.schema.yml index 2c0cb389f7..97e107ca2f 100644 --- a/core/modules/image/config/schema/image.schema.yml +++ b/core/modules/image/config/schema/image.schema.yml @@ -167,3 +167,4 @@ filter_settings.filter_image_style: label: 'Allowed image styles' sequence: type: string + label: 'Image style ID' diff --git a/core/modules/image/image.module b/core/modules/image/image.module index 099f033527..0ddb5000f9 100644 --- a/core/modules/image/image.module +++ b/core/modules/image/image.module @@ -8,9 +8,10 @@ use Drupal\Core\Entity\EntityInterface; use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Routing\RouteMatchInterface; -use Drupal\file\Entity\File; -use Drupal\field\FieldStorageConfigInterface; +use Drupal\Core\Url; use Drupal\field\FieldConfigInterface; +use Drupal\field\FieldStorageConfigInterface; +use Drupal\file\Entity\File; use Drupal\image\Entity\ImageStyle; use Drupal\image\ImageStyleInterface; @@ -525,15 +526,46 @@ function image_form_editor_image_dialog_alter(&$form, FormStateInterface $form_s return $image_style->label(); }, $filter->getAllowedImageStyles()); - // Add a select element to choose an image style. - $form['image_style']['selection'] = [ - '#title' => t('Image style'), - '#type' => 'select', - '#default_value' => isset($image_element['data-image-style']) ? $image_element['data-image-style'] : NULL, - '#options' => $image_style_options, - '#required' => TRUE, - '#parents' => ['attributes', 'data-image-style'], - ]; + // If there is only a single image style the user doesn't have any choice. + // Just show a message in that case. + if (count($image_style_options) == 1) { + $label = reset($image_style_options); + $form['image_style']['selection'] = [ + '#type' => 'value', + '#value' => key($image_style_options), + '#parents' => ['attributes', 'data-image-style'], + ]; + $form['image_style']['help'] = [ + '#type' => 'html_tag', + '#tag' => 'p', + '#value' => t('The image will be displayed using the %image_style image style.', ['%image_style' => $label]), + ]; + } + else { + // Add a select element to choose an image style. + $form['image_style']['selection'] = [ + '#title' => t('Use image style'), + '#type' => 'select', + '#default_value' => isset($image_element['data-image-style']) ? $image_element['data-image-style'] : NULL, + '#options' => $image_style_options, + '#required' => TRUE, + '#parents' => ['attributes', 'data-image-style'], + ]; + } + + // Add a link to the configuration of the image styles if the user has + // access to it. + $access_manager = \Drupal::service('access_manager'); + $route_parameters = ['filter_format' => $editor->getFilterFormat()->id()]; + if ($access_manager->checkNamedRoute('entity.filter_format.edit_form', $route_parameters, \Drupal::currentUser())) { + $url = Url::fromRoute('entity.filter_format.edit_form', $route_parameters); + $url->setOptions(['fragment' => 'edit-filters-filter-image-style-settings']); + $form['image_style']['link'] = [ + '#title' => t('Configure allowed image styles'), + '#type' => 'link', + '#url' => $url, + ]; + } $form['actions']['save_modal']['#validate'][] = 'image_form_editor_image_dialog_validate'; } diff --git a/core/modules/image/image.post_update.php b/core/modules/image/image.post_update.php index 41a2564232..79ce7e52b8 100644 --- a/core/modules/image/image.post_update.php +++ b/core/modules/image/image.post_update.php @@ -46,9 +46,9 @@ function image_post_update_enable_filter_image_style() { $allowed_html = !empty($config['settings']['allowed_html']) ? $config['settings']['allowed_html'] : NULL; $matches = []; if ($allowed_html && preg_match('/]*)>/', $allowed_html, $matches)) { - $new_attributes = array_filter(explode(' ', $matches[1])); - $new_attributes[] = 'data-image-style'; - $config['settings']['allowed_html'] = preg_replace('/]*)>/', '', $allowed_html); + $attributes = array_filter(explode(' ', $matches[1])); + $attributes[] = 'data-image-style'; + $config['settings']['allowed_html'] = preg_replace('/]*)>/', '', $allowed_html); $format->setFilterConfig('filter_html', $config); $changed = TRUE; } diff --git a/core/modules/image/src/Tests/Update/ImageUpdateTextFormatsTest.php b/core/modules/image/src/Tests/Update/ImageUpdateTextFormatsTest.php index db819d821a..719e182d45 100644 --- a/core/modules/image/src/Tests/Update/ImageUpdateTextFormatsTest.php +++ b/core/modules/image/src/Tests/Update/ImageUpdateTextFormatsTest.php @@ -38,7 +38,6 @@ public function testPostUpdateFilterImageStyle() { $this->runUpdates(); // Check that the filter_format entities have been updated. - $config_factory = \Drupal::configFactory(); $basic_html_data = $config_factory->get('filter.format.basic_html')->get(); $this->assertTrue(isset($basic_html_data['filters']['filter_image_style']), 'After running the update the "filter_image_style" filter has been applied to the basic_html format.'); $full_html_data = $config_factory->get('filter.format.full_html')->get();