Problem/Motivation
Drupal\Core\Render\Element\FormElementInterface::valueCallback() is missing two important pieces of information:
1. It can be overridden in specific form elements: see this line in FormBuilder:
$value_callable = $element['#value_callback'] ?? FormElementBase::class . '::valueCallback';
2. Its docs say:
> Determines how user input is mapped to an element's #value property
However, FormBuilder calls it to process the element default value: see #3643541: FormBuilder docs mention non-existent #type_value.
Steps to reproduce
Proposed resolution
Remaining tasks
User interface changes
Introduced terminology
API changes
Data model changes
Release notes snippet
Issue fork drupal-3643542
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
Comment #4
myoussef.linksystemz commentedOpened a merge request that documents that valueCallback() is also called when there is no input (with $input FALSE, to get the default value), and that elements can override it via #value_callback. Related: #3643541.
Comment #5
shruu_rao commentedI reviewed !18085 against `FormBuilder::handleInputElement()` and `ElementInfoManager`.
The MR covers both points from the issue summary:
- It now documents that `valueCallback()` is also called with `$input` set to `FALSE` to get the default value.
- It now documents that elements can override it with `#value_callback`.
One sentence needs fixing:
A specific element can use a different callback by setting its #value_callback property; otherwise this method is used.
"otherwise this method is used" isn't accurate. `ElementInfoManager` sets `#value_callback` to the element class's `valueCallback()` by default, so the element's own method is only reached through `#value_callback`. If `#value_callback` is missing or not callable, `FormBuilder::handleInputElement()` falls back to `FormElementBase::valueCallback()`, which returns NULL. It does not fall back to the element's own method.
Suggested wording:
- By default, the element info manager sets this method as the element's #value_callback. Individual form elements can override it by setting #value_callback.
Also add `@see \Drupal\Core\Render\ElementInfoManager`.
AI disclosure: Used Claude to review the MR against the Drupal core code and to help draft this comment. I checked the findings before posting.