Commit 9742f79f authored by Robert Kasza's avatar Robert Kasza Committed by Robert Kasza
Browse files

Issue #3287414 by Project Update Bot, kaszarobert: Automated Drupal 10 compatibility fixes

parent 8fec450d
Loading
Loading
Loading
Loading

composer.json

0 → 100644
+15 −0
Original line number Diff line number Diff line
{
    "name": "drupal/external_media_crop",
    "description": "Field widget to upload files and images from third-party services with crop support.",
    "type": "drupal-module",
    "license": "GPL-2.0+",
    "minimum-stability": "dev",
    "homepage": "https://www.drupal.org/project/external_media_crop",
    "authors": [],
    "support": {},
    "require": {
        "drupal/core": "^8.9 || ^9 || ^10",
        "drupal/external_media": "^1.0",
        "drupal/image_widget_crop": "^2.4"
    }
}
+1 −1
Original line number Diff line number Diff line
name: 'External Media Crop'
description: 'Field widget to upload files and images from third-party services with crop support.'
type: module
core_version_requirement: ^8.9 || ^9
core_version_requirement: ^8.9 || ^9 || ^10
dependencies:
  - external_media:external_media
  - image_widget_crop:image_widget_crop
+52 −13
Original line number Diff line number Diff line
@@ -61,6 +61,7 @@ class ExternalMediaImageWidgetCrop extends ExternalMediaFile {
   * @var \Drupal\Core\Config\Entity\ConfigEntityStorageInterface
   */
  protected $cropTypeStorage;

  /**
   * The config factory.
   *
@@ -280,6 +281,9 @@ class ExternalMediaImageWidgetCrop extends ExternalMediaFile {

  /**
   * {@inheritdoc}
   *
   * @return \Drupal\Core\StringTranslation\TranslatableMarkup[]
   *   A short summary of the widget settings.
   */
  public function settingsSummary() {
    $summary = parent::settingsSummary();
@@ -374,6 +378,9 @@ class ExternalMediaImageWidgetCrop extends ExternalMediaFile {

  /**
   * {@inheritdoc}
   *
   * @return array[]
   *   The form elements for a single widget for this field.
   */
  public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {
    $element = parent::formElement($items, $delta, $element, $form, $form_state);
@@ -430,11 +437,14 @@ class ExternalMediaImageWidgetCrop extends ExternalMediaFile {
  }

  /**
   * Form API callback: Processes a image_image field element.
   * Form API callback: Processes a crop_image field element.
   *
   * Expands the image_image type to include the alt and title fields.
   *
   * This method is assigned as a #process callback in formElement() method.
   *
   * @return array
   *   The elements with parents fields.
   */
  public static function process($element, FormStateInterface $form_state, $form) {
    $item = $element['#value'];
@@ -450,11 +460,15 @@ class ExternalMediaImageWidgetCrop extends ExternalMediaFile {
        'uri' => $file->getFileUri(),
      ];

      $dimension_key = $variables['uri'] . '.image_preview_dimensions';
      // Determine image dimensions.
      if (isset($element['#value']['width']) && isset($element['#value']['height'])) {
        $variables['width'] = $element['#value']['width'];
        $variables['height'] = $element['#value']['height'];
      }
      elseif ($form_state->has($dimension_key)) {
        $variables += $form_state->get($dimension_key);
      }
      else {
        $image = \Drupal::service('image.factory')->get($file->getFileUri());
        if ($image->isValid()) {
@@ -477,14 +491,7 @@ class ExternalMediaImageWidgetCrop extends ExternalMediaFile {

      // Store the dimensions in the form so the file doesn't have to be
      // accessed again. This is important for remote files.
      $element['width'] = [
        '#type' => 'hidden',
        '#value' => $variables['width'],
      ];
      $element['height'] = [
        '#type' => 'hidden',
        '#value' => $variables['height'],
      ];
      $form_state->set($dimension_key, ['width' => $variables['width'], 'height' => $variables['height']]);
    }
    elseif (!empty($element['#default_image'])) {
      $default_image = $element['#default_image'];
@@ -505,25 +512,25 @@ class ExternalMediaImageWidgetCrop extends ExternalMediaFile {
    $element['alt'] = [
      '#title' => t('Alternative text'),
      '#type' => 'textfield',
      '#default_value' => isset($item['alt']) ? $item['alt'] : '',
      '#default_value' => $item['alt'] ?? '',
      '#description' => t('Short description of the image used by screen readers and displayed when the image is not loaded. This is important for accessibility.'),
      // @see https://www.drupal.org/node/465106#alt-text
      '#maxlength' => 512,
      '#weight' => -12,
      '#access' => (bool) $item['fids'] && $element['#alt_field'],
      '#required' => $element['#alt_field_required'],
      '#element_validate' => $element['#alt_field_required'] == 1 ? [[get_called_class(), 'validateRequiredFields']] : [],
      '#element_validate' => $element['#alt_field_required'] == 1 ? [[static::class, 'validateRequiredFields']] : [],
    ];
    $element['title'] = [
      '#type' => 'textfield',
      '#title' => t('Title'),
      '#default_value' => isset($item['title']) ? $item['title'] : '',
      '#default_value' => $item['title'] ?? '',
      '#description' => t('The title is used as a tool tip when the user hovers the mouse over the image.'),
      '#maxlength' => 1024,
      '#weight' => -11,
      '#access' => (bool) $item['fids'] && $element['#title_field'],
      '#required' => $element['#title_field_required'],
      '#element_validate' => $element['#title_field_required'] == 1 ? [[get_called_class(), 'validateRequiredFields']] : [],
      '#element_validate' => $element['#title_field_required'] == 1 ? [[static::class, 'validateRequiredFields']] : [],
    ];

    if ($element['#files']) {
@@ -544,6 +551,38 @@ class ExternalMediaImageWidgetCrop extends ExternalMediaFile {
    return parent::process($element, $form_state, $form);
  }

  /**
   * Verify if the element have an image file.
   *
   * @param array $element
   *   A form element array containing basic properties for the widget.
   * @param array $variables
   *   An array with all existent variables for render.
   *
   * @return array[]
   *   The variables with width & height image information.
   */
  public static function getFileImageVariables(array $element, array &$variables) {
    // Determine image dimensions.
    if (isset($element['#value']['width']) && isset($element['#value']['height'])) {
      $variables['width'] = $element['#value']['width'];
      $variables['height'] = $element['#value']['height'];
    }
    else {
      /** @var \Drupal\Core\Image\Image $image */
      $image = \Drupal::service('image.factory')->get($variables['uri']);
      if ($image->isValid()) {
        $variables['width'] = $image->getWidth();
        $variables['height'] = $image->getHeight();
      }
      else {
        $variables['width'] = $variables['height'] = NULL;
      }
    }

    return $variables;
  }

  /**
   * Validate callback for alt and title field, if the user wants them required.
   *