file.api.php documentation is incorrect

Created on 14 December 2023, over 1 year ago

Problem/Motivation

The documentation in file.api.php documents the #upload_validators with an example using #type file. However, #upload_validators are not called for #type file, only #type managed_file.

Steps to reproduce

View the source on the API page for file.api.php: https://api.drupal.org/api/drupal/core%21modules%21file%21file.api.php/10

Proposed resolution

Change the example from '#type' => 'file' to '#type' => 'managed_file'

Remaining tasks

None.

User interface changes

None.

API changes

None.

Data model changes

None.

Release notes snippet

🐛 Bug report
Status

Active

Version

10.2

Component
File module 

Last updated 29 minutes ago

Created by

🇨🇦Canada jaypan Victoria, BC

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

Comments & Activities

  • Issue created by @jaypan
  • 🇨🇦Canada jaypan Victoria, BC
  • Open in Jenkins → Open on Drupal.org →
    Environment: PHP 8.1 & MariaDB 10.3.22
    last update over 1 year ago
    Patch Failed to Apply
  • 🇸🇰Slovakia poker10

    There are usages in core where upload_validators are used with the type file. For example in Drupal\system\Form\ThemeSettingsForm, see: https://git.drupalcode.org/project/drupal/-/blob/11.x/core/modules/syste...

          $form['logo']['settings']['logo_upload'] = [
            '#type' => 'file',
            '#title' => $this->t('Upload logo image'),
            '#description' => $this->t("If you don't have direct file access to the server, use this field to upload your logo."),
            '#upload_validators' => [
              'FileIsImage' => [],
            ],
          ];
    

    Based on this I think it is possible to use upload_validators on type file as well.

  • Status changed to Closed: works as designed 2 days ago
  • 🇮🇳India mohit_aghera Rajkot

    I believe we should close this issue.

    There are few more usages of upload validators with the file elmement.
    like
    Drupal\locale\Form\ImportForm.php

    $form['file'] = [
          '#type' => 'file',
          '#title' => $this->t('Translation file'),
          '#description' => [
            '#theme' => 'file_upload_help',
            '#description' => $this->t('A Gettext Portable Object file.'),
            '#upload_validators' => $validators,
          ],
          '#size' => 50,
          '#upload_validators' => $validators,
          '#upload_location' => 'translations://',
          '#attributes' => ['class' => ['file-import-input']],
        ];

    Feel free to re-open if you feel we still need to alter documentation to mention both the #types of fields.

Production build 0.71.5 2024