0

I have a custom form in drupal 9 where I am creating a managed_file type field to upload an image:

$form['footer']['logo'] = array(
  '#title' => 'Logo',
  '#type' => 'managed_file',
  '#upload_location' => 'public://',
  '#upload_validators' => [
    'file_validate_extensions' => ['png jpg jpeg'],
  ],
  '#description' => t('The valid formats are png, jpg, jpeg'),
  '#default_value' => $data['footer']['logo'] ?? '',
); 

And I need to add the "alt" attribute to the field and the user can add this information when uploading the image, however when I make the attempt to add it normally it doesn't work:

$form['footer']['logo'] = array(
  '#title' => 'Logo',
  '#type' => 'managed_file',
  '#alt' => 'Example'
  '#upload_location' => 'public://',
  '#upload_validators' => [
    'file_validate_extensions' => ['png jpg jpeg'],
  ],
  '#description' => t('The valid formats are png, jpg, jpeg'),
  '#default_value' => $data['footer']['logo'] ?? '',
); 

can anyone give me an idea how can I fix it?

3
  • 2
    Managed file elements don’t have an alt (that’s an image field, different thing altogether). Add a separate text field for alt, and save/load its data alongside the file ID when you need to
    – Clive
    Jun 22, 2023 at 16:59
  • Okay and maybe it would be easier to use a field of type "image" or something like that instead of using "managed_file"? Jun 22, 2023 at 17:03
  • Field widgets only work with entities, for manual forms you need to build it yourself
    – Clive
    Jun 22, 2023 at 17:18

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.