Call to undefined method in images of media field

Created on 26 November 2020, almost 4 years ago
Updated 23 May 2023, over 1 year ago

Problem/Motivation

Message Error: Call to undefined method Drupal\media_library\Form\FileUploadForm::getEntity() in _image_field_caption_widget_process() (line 50 of /Applications/XAMPP/xamppfiles/htdocs/eg-blog/web/modules/contrib/image_field_caption/image_field_caption.module)

This error appears in a field that uses the Media field type, when trying to upload an image to it.
Image is not loaded and the error appears in the log messages

Steps to reproduce

Add a new field of type media on a content type, and enable the image media type
Add content in the content type and try to upload an image to that field

Proposed resolution

This modification of the code works and the image is uploaded and the textarea for the caption appears

  // Get the entity.
  $field_definition = NULL;
  $form_object = $form_state->getFormObject();
  if ($form_object instanceof EntityFormInterface) {
    $entity = $form_object->getEntity();
    // Get the fields definitions.
    $field_definitions = $entity->getFieldDefinitions();
    // Get the current field definition.
    if (!empty($field_definitions[$element['#field_name']])) {
      $field_definition = $field_definitions[$element['#field_name']];
    }
    elseif (!empty($field_definitions[$element['#field_parents'][0]])) {
      $field_definition = $field_definitions[$element['#field_parents'][0]];
    }
  }

Remaining tasks

Create the patch with the proposed code.

User interface changes

API changes

Data model changes

πŸ› Bug report
Status

Needs review

Version

1.1

Component

Code

Created by

πŸ‡§πŸ‡ͺBelgium gorkagr

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.

  • πŸ‡ΊπŸ‡ΈUnited States jimmynash

    Patch in #6 helped with a problem using views bulk operations.

    Trying to use VBO and Views Bulk Edit to change the taxonomy terms across a content type that was using image_field_caption resulted in the following error.

    Error: Call to undefined method Drupal\views_bulk_operations\Form\ConfigureAction::getEntity() in _image_field_caption_widget_process() (line 49 of /code/web/modules/contrib/image_field_caption/image_field_caption.module)

Production build 0.71.5 2024