backdrop / backdrop-issues

Issue tracker for Backdrop core.
144 stars 38 forks source link

[DX] Form API: Add a new property to tableselect, to allow specifying which column should be teated as labels for the checkboxes #6543

Open klonos opened 2 weeks ago

klonos commented 2 weeks ago

This would be really useful and could reduce effort and code required for issues like #6355 and #5489.

Before:

  $header = array(
    'title' => t('Title'),
    'description' => t('Description'),
  );

  ...

  // Custom code to add the "for" attribute to the elements that are
  // to serve as labels for the checkboxes.
  $checkbox_id = backdrop_html_class('edit-name-fields-' . $key);
  $fields[$key]['title'] = array(
    'data' => '<label for="' . $checkbox_id . '">' . $field['title'] . '</label>',
    'class' => array('title'),
  );

  ...

  $form['nodes'] = array(
    '#type' => 'tableselect',
    '#header' => $header,
    '#options' => $options,
    '#empty' => t('No content available.'),
  );

After - no custom code required for the for attribute - the Form API makes things work automagically via a new #labels_column attribute (final name TBD):

  $form['nodes'] = array(
    '#type' => 'tableselect',
    '#header' => $header,
    // Add (optional) support for something like this:
    '#labels_column' => 'title',
    // 'title' should be a key that exists in the #header array.
    '#options' => $options,
    '#empty' => t('No content available.'),
  );
klonos commented 2 weeks ago

@laryn this is the issue 😉

laryn commented 2 weeks ago

I like this idea -- make it more consistent and less reinvent-the-wheely.