Problem/Motivation

The file_field_replace replaces the existing file widget #value_callback with its own callback:

$element['#value_callback'] = 'file_field_replace_field_widget_value_calback';

This breaks compatibility with field widgets that extend Drupal core’s FileWidget / ImageWidget and implement their own value() method.

One concrete example is the Focal Point module’s image_focal_point widget. Focal Point uses its widget value callback to load the saved crop entity and add the saved focal point coordinates to the widget value:

  $return['focal_point'] = "{$anchor['x']},{$anchor['y']}";

When file_field_replace is enabled for the field, FocalPointImageWidget::value() is never called. As a result, the saved crop is still used correctly for image styles, but the edit form indicator falls back to the widget default, usually 50,50.

Steps to reproduce

1. Install and enable:
- file_field_replace
- focal_point
- crop

2. Create or use a Media image field using the Focal Point widget:
Widget: Image (Focal Point)

3. Enable File Field Replace behavior for that image field.
4. Edit a media item and move the focal point indicator away from center.
moved
5. Save the media item.
6. Confirm the rendered/cropped image uses the saved focal point correctly.
7. Reopen the media edit form.

Actual Result:
The focal point indicator is displayed at the center again.
unsaved
The saved crop entity still exists and has correct coordinates, but the edit widget receives a value without focal_point.

Cause
file_field_replace overwrites the widget’s original #value_callback:

 $element['#value_callback'] = 'file_field_replace_field_widget_value_calback';

Before that override, the callback for Focal Point’s widget would be:

  [
    'Drupal\focal_point\Plugin\Field\FieldWidget\FocalPointImageWidget',
    'value',
  ]
public static function value($element, $input, FormStateInterface $form_state)

Drupal Form API supports only one #value_callback, so the original callback is lost.

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

oleksandr.s created an issue. See original summary.

oleksandr.s’s picture

Status: Active » Needs review
oleksandr.s’s picture

Title: Overring the #value_callback breaks compatibility with widgets that extend FileWidget, e.g. Focal Point » Overriding the #value_callback breaks compatibility with widgets that extend FileWidget, e.g. Focal Point