The $form[$field_name] structure prepared by WidgetBase::formSingleElement() contains lots of #custom_properties
that can now be obtained through other ways :
- The $items parameter in Widget::formElement()
- The $context parameter for hook_field_widget_form_alter()
- The main entity for form_alters on the whole form.

$form[$field_name]['#entity']
$form[$field_name]['#entity_type']
$form[$field_name]['#bundle']
$form[$field_name]['#language']
$form[$field_name]['#field_name']

Interestingly, '#entity' was added very late in D7 (only a little more than 2 years ago, post 7.0) to support #1541792: Enable dynamic allowed list values function with additional context.
It was only needed so that the widgets for "list" field types could pass the $entity to the "give me the list of allowed values" function.

Comments

yched’s picture

Status: Active » Needs review
StatusFileSize
new1.58 KB

Some of those properties might be used by widget specific #process or #submit FAPI callbacks - FileWidget does that.
But those are very specific use cases, those widgets can totally take care of placing the #custom_properties their helper callbacks need. FileWidget does it already for a couple properties.

Other than that, brute force patch, let's see what breaks...

Status: Needs review » Needs work

The last submitted patch, 1: 2293723-simplify_field_form-1.patch, failed testing.

yched’s picture

Status: Needs work » Needs review
StatusFileSize
new6.66 KB
new5.38 KB

So, LinkWidget uses $element['#entity'] to figure whether we are on the "default value" widget in the instance edit form.
Not sure that even works, and need to mull a bit more on the right alternative.

Meanwhile, fixed the other fails.

Status: Needs review » Needs work

The last submitted patch, 3: 2293723-simplify_field_form-3.patch, failed testing.

andypost’s picture

nice clean-up! much less data to serialize for entity forms!

PS: the default values for comment field still have hacks, so I re-opened #1919834-13: Field instance got no default value when created in field UI

yched’s picture

Title: Remove '#entity' from $form['field'] » Generate lighter $form[$field] structures
Status: Needs work » Needs review
StatusFileSize
new6.83 KB
new958 bytes

Better way to check for "are we on the default value widget ?" in LinkWidget.
Should hopefully be green.

+ more accurate title.

yched’s picture

StatusFileSize
new7.81 KB
new1004 bytes

Removing those properties from the docs too.

andypost’s picture

Status: Needs review » Reviewed & tested by the community

awesome clean-up

yched’s picture

Issue summary: View changes

Created a draft change notice : https://www.drupal.org/node/2294629

catch’s picture

Status: Reviewed & tested by the community » Fixed

Committed/pushed to 8.x, thanks!

  • catch committed e8ab0b2 on 8.x
    Issue #2293723 by yched: Generate lighter $form[$field] structures.
    
yched’s picture

Thanks !
Published the CR at https://www.drupal.org/node/2294629.

yched’s picture

Status: Fixed » Needs review
StatusFileSize
new2.77 KB

Quick doc followup : EntityFormDisplayInterface::buildForm() documented this structure too - and was in fact a bit stale already.
Attached patch makes it to date.

andypost’s picture

Status: Needs review » Reviewed & tested by the community
Issue tags: +Documentation

Hm, missed this too

  • catch committed 6a3574a on 8.x
    Issue #2293723 by yched: Generate lighter $form[$field] structures.
    
catch’s picture

Status: Reviewed & tested by the community » Fixed

Committed/pushed the follow-up, thanks!

yched’s picture

Thanks @catch. You might also be interested in #2200355: Move functions in field.form.inc into Core/Field :-)

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.