Problem/Motivation
Currently there is no way to define a new #type by extending an existing #type using hook_element_info(). Every #type is totally "flat" and there's a lot of duplicated configuration between types.
Consider the following example (there are many in core like this):
// Input elements.
$types['submit'] = array(
'#input' => TRUE,
'#name' => 'op',
'#is_button' => TRUE,
'#executes_submit_callback' => TRUE,
'#limit_validation_errors' => FALSE,
'#process' => array('form_process_button', 'ajax_process_form'),
'#pre_render' => array('form_pre_render_button'),
'#theme_wrappers' => array('input__submit'),
);
$types['button'] = array(
'#input' => TRUE,
'#name' => 'op',
'#is_button' => TRUE,
'#executes_submit_callback' => FALSE,
'#limit_validation_errors' => FALSE,
'#process' => array('form_process_button', 'ajax_process_form'),
'#pre_render' => array('form_pre_render_button'),
'#theme_wrappers' => array('input__button'),
);
$types['image_button'] = array(
'#input' => TRUE,
'#is_button' => TRUE,
'#executes_submit_callback' => TRUE,
'#limit_validation_errors' => FALSE,
'#process' => array('form_process_button', 'ajax_process_form'),
'#return_value' => TRUE,
'#has_garbage_value' => TRUE,
'#src' => NULL,
'#pre_render' => array('form_pre_render_image_button'),
'#theme_wrappers' => array('input__image_button'),
);
Would it not be simpler and easier to understand and maintain to do the following:
// Input elements.
$types['submit'] = array(
'#base_type' => 'button',
'#executes_submit_callback' => TRUE,
'#theme_wrappers' => array('input__submit'),
);
$types['button'] = array(
'#input' => TRUE,
'#name' => 'op',
'#is_button' => TRUE,
'#executes_submit_callback' => FALSE,
'#limit_validation_errors' => FALSE,
'#process' => array('form_process_button', 'ajax_process_form'),
'#pre_render' => array('form_pre_render_button'),
'#theme_wrappers' => array('input__button'),
);
$types['image_button'] = array(
'#base_type' => 'button',
'#executes_submit_callback' => TRUE,
'#name' => NULL,
'#return_value' => TRUE,
'#has_garbage_value' => TRUE,
'#src' => NULL,
'#pre_render' => array('form_pre_render_image_button'),
'#theme_wrappers' => array('input__image_button'),
);
This is actually how the 'text_format' FAPI #type currently works with the "non standard" #base_type attribute - https://api.drupal.org/api/drupal/developer%21topics%21forms_api_referen...
Proposed resolution
Allow \Drupal\Core\Render\ElementInfo to build #type information recursively.
Remaining tasks
Write code.
User interface changes
None.
API changes
An element #type definition can extend from another #type using the #base_type attribute
Comment | File | Size | Author |
---|---|---|---|
#2 | 2272577-2.patch | 2.16 KB | thedavidmeister |
Comments
Comment #1
thedavidmeister CreditAttribution: thedavidmeister commentedI just noticed that #base_type seems to do something similar to this for 'text_format' #type elements in the FAPI only. So I suppose this issue should be to standardise that.
Comment #2
thedavidmeister CreditAttribution: thedavidmeister commentedSomething like this.
Comment #3
thedavidmeister CreditAttribution: thedavidmeister commentedComment #4
thedavidmeister CreditAttribution: thedavidmeister commentedComment #6
sunCould you rewrite the issue summary to properly split the problem and goal, please?
It's not clear why this is needed, so unless there is a solid problem statement, it should be bumped to D9.
Comment #7
thedavidmeister CreditAttribution: thedavidmeister commentedD9 is fine.
Comment #8
tim.plunkettThis will be possible after #2305839: Convert hook_element_info() to annotated classes
Comment #9
catchMarking duplicate.