Errors are not showing up

Created on 3 October 2023, over 1 year ago
Updated 22 April 2024, 9 months ago

Problem/Motivation

I'm trying to use "Reference existing" option in the widget and when I choose the image, it set's an error message "Oops, something went wrong. Check your browser's developer console for more details." and nothing else happens.

Steps to reproduce

Maybe it's Drupal 10.1 issue because I could not reproduce it at https://simplytest.me/ but you could try locally.

Start the alpha 5 version at https://simplytest.me/
Add article with an image /node/add/article
Set max image resolution to something small eg 10x10 at /admin/structure/types/manage/article/fields/node.article.field_image
Enable "Autocomplete reference textfield" at /admin/structure/types/manage/article/form-display for Article
Add article /node/add/article , choose the "Reference existing" option, choose the image that was previously uploaded

Proposed resolution

Review the filefield_sources_element_validate() and fix issues.

πŸ› Bug report
Status

Closed: duplicate

Version

1.0

Component

General

Created by

πŸ‡ͺπŸ‡ͺEstonia hkirsman

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

  • Issue created by @hkirsman
  • πŸ‡ͺπŸ‡ͺEstonia hkirsman

    1. What is this talk about PHP 5.3? I did find it was added 13 years ago https://git.drupalcode.org/project/filefield_sources/-/commit/57a0d0a643...
    I think this might be replaced by what is in file.module

    2. All t() functions return object so doing .= will fail.

    3. $file->filename is not string but but object. I think something like $file->getFileName() should be used.

    /**
     * Validate a file based on the $element['#upload_validators'] property.
     */
    function filefield_sources_element_validate($element, $file, FormStateInterface $form_state) {
      $validators = $element['#upload_validators'];
      $errors = [];
    
      // Since this frequently is used to reference existing files, check that
      // they exist first in addition to the normal validations.
      if (!file_exists($file->getFileUri())) {
        $errors[] = t('The file does not exist.');
      }
      // Call the validation functions.
      else {
        foreach ($validators as $function => $args) {
          // Add the $file variable to the list of arguments and pass it by
          // reference (required for PHP 5.3 and higher).
          array_unshift($args, NULL);
          $args[0] = &$file;
          $errors = array_merge($errors, call_user_func_array($function, $args));
        }
      }
    
      // Check for validation errors.
      if (!empty($errors)) {
        $message = t('The selected file %name could not be referenced.', ['%name' => $file->filename]);
        if (count($errors) > 1) {
          $message .= '<ul><li>' . implode('</li><li>', $errors) . '</li></ul>';
        }
        else {
          $message .= ' ' . array_pop($errors);
        }
        $form_state->setError($element, $message);
        return 0;
      }
    
      return 1;
    }
  • πŸ‡ΊπŸ‡ΈUnited States rschwab
  • Status changed to Closed: duplicate 9 months ago
  • πŸ‡ΊπŸ‡ΈUnited States rschwab
Production build 0.71.5 2024