Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
Problem/Motivation
When using focal point in a subform (e.g. an image field with focal point enabled in a paragraphs subform), validation fails with the following message, which makes the whole form unsaveable:
Notice: Undefined index: focal_point in Drupal\focal_point\Plugin\Field\FieldWidget\FocalPointImageWidget::validateFocalPoint() (line 120 of modules/contrib/focal_point/src/Plugin/Field/FieldWidget/FocalPointImageWidget.php).
This happens due to the way, the current focal point value is determined from the form state object:
$field_name = array_shift($element['#parents']);
$field_values = $form_state->getValue($field_name);
Proposed resolution
Provide a patch that uses $element['#value']
directly as other core widget plugins do it.
Remaining tasks
- Create patch
- Check why tests are failing now
Steps to reproduce
- Install paragraphs module
- Install latest focal_point dev-release module
- Create paragraphs type containing an image field
- Add a paragraphs-enabled field (entity referecence revision) field to a content type
- At "Manage for display" set the edit mode of the paragraphs field to "Preview"
- Try to create a content with the paragraphs type containing the image field
Comment | File | Size | Author |
---|---|---|---|
#13 | focal_point_validation-2764403-13.patch | 2.97 KB | bleen |
| |||
#13 | interdiff.txt | 1.55 KB | bleen |
Comments
Comment #2
hctomAttached is the proposed path. This patch also fixes an issue with the error message thrown on validation errors:
I'd appreciate your feedback ;)
Comment #4
hctomOops, I missed the case, where the focal point field may be completely empty. So here is a follow up patch covering that, too.
Comment #6
hctom@bleen18: There is some strange stuff going on in the tests... can you please have a look, why those fail now?
Thanx in advance
Comment #7
bleen CreditAttribution: bleen at NBCUniversal commented@hctom ... I have no problem with the change to the error message itself, but based on the test failures it seems that the using $element['#value'] cannot be used here because (like "alt" and "title") the focal point value is attached to the image itself and is not a value unto itself.
Can you give some more detailed steps to reproduce this issue?
Comment #8
hctomComment #9
hctomComment #10
bleen CreditAttribution: bleen at NBCUniversal commented...one way to fix this might be to add the validation to the image field itself instead of the form
Comment #11
hctomJust added some info how to reproduce this
Comment #12
hctomI think I found something in the tests ;)
The
FocalPointImageWidget::validateFocalPoint()
element validation handler is bound to thefocal_point
field in the widget:But in the tests, you call this method with the image test element as parameter but not with the actual focal_point element:
So the parameter passed in the test varies from the parameter Drupal would pass in, when running form validation, doesn't it?
Comment #13
bleen CreditAttribution: bleen at NBCUniversal commentedOk ... I had a few mins to take a look. The patch looks good but there was a slightly different issue with the unit tests than you suspected. The primary issue was that the test presumed that the test value had to be in an array that looked like the #parents array. Since thats no longer relevant I just moved it around in the test.
This should do it.
Comment #14
jastraat CreditAttribution: jastraat at Technivant commentedThe patch in #13 addressed the error that occurred when editing a node containing a field using the Inline Entity Form module for entity reference entry when the entity contained an image field configured with a focal point style.
Error addressed:
Notice: Undefined index: focal_point in Drupal\focal_point\Plugin\Field\FieldWidget\FocalPointImageWidget::validateFocalPoint()
Comment #15
bleen CreditAttribution: bleen at NBCUniversal commentedComment #17
bleen CreditAttribution: bleen at NBCUniversal commented