Verified Commit 28426e34 authored by Andrei Mateescu's avatar Andrei Mateescu
Browse files

task: #3567618 Create service for image_path_flush, image_style_options and...

task: #3567618 Create service for image_path_flush, image_style_options and constant for IMAGE_DERIVATIVE_TOKEN

By: nicxvan
By: dcam
By: claudiu.cristea
By: godotislate
By: amateescu
parent 46eed9dd
Loading
Loading
Loading
Loading
Loading
+2 −1
Original line number Diff line number Diff line
@@ -8,6 +8,7 @@
use Drupal\Core\Entity\EntityForm;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Language\LanguageInterface;
use Drupal\image\ImageDerivativeUtilities;

/**
 * Form controller for the test config edit forms.
@@ -52,7 +53,7 @@ public function form(array $form, FormStateInterface $form_state) {
    ];
    if ($this->moduleHandler->moduleExists('image')) {
      $form['style']['#access'] = TRUE;
      $form['style']['#options'] = image_style_options();
      $form['style']['#options'] = \Drupal::service(ImageDerivativeUtilities::class)->styleOptions();
    }

    // The main premise of entity forms is that we get to work with an entity
+20 −18
Original line number Diff line number Diff line
@@ -4,11 +4,16 @@
 * @file
 */

use Drupal\image\Entity\ImageStyle;
use Drupal\image\Hook\ImageThemeHooks;
use Drupal\image\ImageDerivativeUtilities;

/**
 * The name of the query parameter for image derivative tokens.
 *
 * @deprecated in drupal:11.4.0 and is removed from drupal:13.0.0. Use
 *   \Drupal\image\ImageStyleInterface::TOKEN instead.
 *
 * @see https://www.drupal.org/node/3567619
 */
define('IMAGE_DERIVATIVE_TOKEN', 'itok');

@@ -17,12 +22,15 @@
 *
 * @param string $path
 *   The Drupal file path to the original image.
 *
 * @deprecated in drupal:11.4.0 and is removed from drupal:13.0.0. Use
 *   ImageDerivativeUtilities::pathFlush() instead.
 *
 * @see https://www.drupal.org/node/3567619
 */
function image_path_flush($path): void {
  $styles = ImageStyle::loadMultiple();
  foreach ($styles as $style) {
    $style->flush($path);
  }
  @trigger_error(__FUNCTION__ . '() is deprecated in drupal:11.4.0 and is removed from drupal:13.0.0. Use \Drupal\image\ImageDerivativeUtilities::pathFlush() instead. See https://www.drupal.org/node/3567619', E_USER_DEPRECATED);
  \Drupal::service(ImageDerivativeUtilities::class)->pathFlush($path);
}

/**
@@ -33,21 +41,15 @@ function image_path_flush($path): void {
 *
 * @return string[]
 *   Array of image styles both key and value are set to style name.
 *
 * @deprecated in drupal:11.4.0 and is removed from drupal:13.0.0. Use
 *   ImageDerivativeUtilities::styleOptions() instead.
 *
 * @see https://www.drupal.org/node/3567619
 */
function image_style_options($include_empty = TRUE): array {
  $styles = ImageStyle::loadMultiple();
  $options = [];
  if ($include_empty && !empty($styles)) {
    $options[''] = t('- None -');
  }
  foreach ($styles as $name => $style) {
    $options[$name] = $style->label();
  }

  if (empty($options)) {
    $options[''] = t('No defined styles');
  }
  return $options;
  @trigger_error(__FUNCTION__ . '() is deprecated in drupal:11.4.0 and is removed from drupal:13.0.0. Use \Drupal\image\ImageDerivativeUtilities::styleOptions() instead. See https://www.drupal.org/node/3567619', E_USER_DEPRECATED);
  return \Drupal::service(ImageDerivativeUtilities::class)->styleOptions($include_empty);
}

/**
+1 −0
Original line number Diff line number Diff line
@@ -15,3 +15,4 @@ services:
  logger.channel.image:
    parent: logger.channel_base
    arguments: ['image']
  Drupal\image\ImageDerivativeUtilities: ~
+1 −1
Original line number Diff line number Diff line
@@ -133,7 +133,7 @@ public function deliver(Request $request, $scheme, ImageStyleInterface $image_st
    // The $target variable for a derivative of a style has
    // styles/<style_name>/... as structure, so we check if the $target variable
    // starts with styles/.
    $token = $request->query->get(IMAGE_DERIVATIVE_TOKEN, '');
    $token = $request->query->get(ImageStyleInterface::TOKEN, '');
    $token_is_valid = hash_equals($image_style->getPathToken($image_uri), $token)
      || hash_equals($image_style->getPathToken($scheme . '://' . $target), $token);
    if (!$this->config('image.settings')->get('allow_insecure_derivatives') || str_starts_with(ltrim($target, '\/'), 'styles/')) {
+1 −1
Original line number Diff line number Diff line
@@ -235,7 +235,7 @@ public function buildUrl($path, $clean_urls = NULL) {
        $path = \Drupal::config('system.file')->get('default_scheme') . '://' . $path;
      }
      $original_uri = $stream_wrapper_manager->normalizeUri($path);
      $token_query = [IMAGE_DERIVATIVE_TOKEN => $this->getPathToken($original_uri)];
      $token_query = [ImageStyleInterface::TOKEN => $this->getPathToken($original_uri)];
    }

    if ($clean_urls === NULL) {
Loading