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

CommentFileSizeAuthor
#2 2272577-2.patch2.16 KBthedavidmeister
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

thedavidmeister’s picture

Title: Ability for #type to extend other #types in renderable arrays. » Make #base_type a standard way to extend #type in renderable arrays.
Issue summary: View changes

I 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.

thedavidmeister’s picture

Status: Active » Needs review
FileSize
2.16 KB

Something like this.

thedavidmeister’s picture

Issue summary: View changes
thedavidmeister’s picture

Issue summary: View changes

Status: Needs review » Needs work

The last submitted patch, 2: 2272577-2.patch, failed testing.

sun’s picture

Status: Needs work » Postponed (maintainer needs more info)

Could 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.

thedavidmeister’s picture

Version: 8.x-dev » 9.x-dev
Status: Postponed (maintainer needs more info) » Active

D9 is fine.

tim.plunkett’s picture

catch’s picture

Status: Active » Closed (duplicate)

Marking duplicate.

Version: 9.x-dev » 9.0.x-dev

The 9.0.x branch will open for development soon, and the placeholder 9.x branch should no longer be used. Only issues that require a new major version should be filed against 9.0.x (for example, removing deprecated code or updating dependency major versions). New developments and disruptive changes that are allowed in a minor version should be filed against 8.9.x, and significant new features will be moved to 9.1.x at committer discretion. For more information see the Allowed changes during the Drupal 8 and 9 release cycles and the Drupal 9.0.0 release plan.