#ajax doesn't work on FAPI file_managed

Created on 8 February 2019, almost 6 years ago
Updated 22 January 2025, 7 days ago

Context:

Drupal 8.7.x
A node create form contianing a file_managed upload form element (Regular file upload for an .mp4 file)

Problem

I was trying to register an #ajax event on the file upload field to copy the file path after upload into a text field (video embed html). But the event is simply never triggered.

When I registered the #ajax on a different (select list) form element, everything works as expected.

So I guess the file_managed FAPI element isn't capable of #ajax, because it already uses #ajax itself on sub-elements?
https://api.drupal.org/api/drupal/core%21modules%21file%21src%21Element%...

Example code:

/**
 * Media Video Handling.
 */
function mymodule_form_media_video_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id){
  // Register #ajax change event on upload field:
  $form['field_video_file']['widget']['#ajax'] = [
    'event' => 'change',
    'callback' => 'mymodule_video_upload_callback',
  ];
}

function mymodule_video_upload_callback(array &$form, FormStateInterface $form_state){
  $response = new AjaxResponse();
  $response->addCommand(new AlertCommand('Uploaded file!'));
  $response->addCommand(new InvokeCommand('#edit-field-video-0-value', 'val', array('TEST')));
  $response->addCommand(new InvokeCommand('#edit-field-video-0-value', 'change'));

  return $response;
}

I couldn't find a working example and after several hours I aborted my journey through code. Any ideas or can someone confirm this problem?

💬 Support request
Status

Postponed: needs info

Version

11.0 🔥

Component

ajax system

Created by

🇩🇪Germany Anybody Porta Westfalica

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

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • 🇳🇿New Zealand quietone

    @anybody, the Drupal Core issue queue is not the ideal place for support requests. As is shown by 5 years without a response.

    Is this the same problem reported in 🐛 ManagedFile ajax request the wrong URL because it ignores #ajax url parameter Needs work ?

  • 🇩🇪Germany Anybody Porta Westfalica

    Thanks @quietone! You're right, and I think we could even change this into a bug report as it's unexpected behaviour?

    Still, I think we can make it minor, as nobody else seems to have run into this and we finally skipped this approach entirely. Still I think the issue exists and is worth to be kept open, if someone else runs into this niche case?

    No I don't think it's a duplicate of that issue, this is about the event not being called at all (while expected) and the other one is about the URL.

    Thanks for taking the time to read this! :)

Production build 0.71.5 2024