Thanks very much for your work on adding #2853164: Hierarchical entity select. However, the UX wasn't exactly what the client had in mind when faced with allowing the user to select multiple categories, even with the jquery Select.

They requested the ability to have checkboxes and as you check a term, its child terms become visible.

The attached patch adds this functionality, though is currently without tests.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

stella created an issue. See original summary.

jrockowitz’s picture

@stella Adding this element makes complete sense to me.

Besides tests, we should consider....

  • Improve code reuse by creating a WebformTermBase form element and plugin, which would be extended by WebformTermCheckboxes and WebformTermSelect.
  • Creating a WebformTermRadios element.
  • Change 'test_element_term_select' test form to 'test_element_term' and add instance of 'webform_term_checkboxes' and 'webform_term_radios' elements.
  • Since the list of displayed terms could get pretty tall and nice enhancement would be to make the "term tree" scrollable by settings customizable max height to the div container.

Notes.

  • Once these new elements are finalized, the webform_test.module needs to be updated to generate examples of these new elements.
stella’s picture

I think Radios would be harder to achieve as you can only select 1 radio button normally. So clicking through an expanding set of child terms which are all part of one radios set, wouldn't be good ux, no?

Though you probably want to be able to force them to click through one path and only select one child term at the very end (and not the entire tree). Maybe the cshs module approach to that is better? it's a drop-down which spawns a secondary drop-down for child terms which in turn spawns another for grandchildren, etc. And if you want to allow more than one term to be selected you click 'add more' to get another set.

As for the scrollable thing - as long as it's an optional feature, i'm ok with that.

jrockowitz’s picture

I think creating a WebformTermBase class is the key task and creating a WebformTermRadios element can be handled in a new ticket.

jrockowitz’s picture

Assigned: Unassigned » jrockowitz

@stella I am going to do a little work on the patch.

  • jrockowitz committed b02b301 on 2864400-term-checkboxes
    Issue #2864400 by stella: Hierarchical taxonomy checkboxes
    

  • jrockowitz committed 5257fe8 on 2864400-term-checkboxes
    Issue #2864400 by stella: Hierarchical taxonomy checkboxes
    

  • jrockowitz committed 3a957bf on 2864400-term-checkboxes
    Issue #2864400 by stella: Hierarchical taxonomy checkboxes
    
jrockowitz’s picture

@stella I added support for the #scroll property and set up a WebformTermReference trait. I ran into some "out of sync" exported configuration so the attached patch is updating all exported config.

Below is a screenshot of the #scroll adding scrolling to term checkboxes.

Right now, I can't see supporting the custom #states hide/show logic but below is your code moved into a custom hook, that you can use to maintain this functionality for your client.

/**
 * Implements hook_webform_element_ELEMENT_TYPE_alter().
 */
function CUSTOM_MODULE_webform_element_webform_term_checkboxes_alter(array &$element, \Drupal\Core\Form\FormStateInterface $form_state, array $context) {
  $taxonomy_storage = \Drupal::entityTypeManager()->getStorage('taxonomy_term');
  $tree = $taxonomy_storage->loadTree($element['#vocabulary']);
  $name = $element['#webform_key'];
  foreach ($tree as $item) {
    if (!empty($item->parents)) {
      // We only care about the first parent for adding classes.
      $parent = $item->parents[0];
      $element[$item->tid]['#attributes']['class'][] = 'parent-' . $parent;
      $element[$item->tid]['#states'] = [
        'visible' => [
          ":input[name=\"{$name}[{$parent}]\"]" => ['checked' => TRUE],
        ],
      ];
    }
  }
}

I am going to write some tests and commit this improvement.

In the long term, it would be really cool if we could support something like https://www.jstree.com/ for selecting hierarchical taxonomy terms.

jrockowitz’s picture

@stella I also think it should be possible to add support to the webform module for the cshs module which includes a dedicated form element.

Status: Needs review » Needs work

The last submitted patch, 9: hierarchical_taxonomy-2864400-7.patch, failed testing.

  • jrockowitz committed ed711fe on 2864400-term-checkboxes
    Issue #2864400 by stella: Hierarchical taxonomy checkboxes
    
jrockowitz’s picture

Status: Needs work » Needs review
FileSize
123.51 KB

Status: Needs review » Needs work

The last submitted patch, 13: hierarchical_taxonomy-2864400-12.patch, failed testing.

jrockowitz’s picture

Status: Needs work » Needs review
FileSize
121.75 KB

  • jrockowitz committed 7bb7979 on 2864400-term-checkboxes
    Issue #2864400 by stella: Hierarchical taxonomy checkboxes
    
stella’s picture

The idea behind the states thing is to reduce the length of checkboxes visible at any one point, so the user only sees the ones relevant to them. They have to click on a term to see its children, and then can drill down to the child term they want. My requirement was they had to select each parent along the way to get to the final term they wanted to select.

The taxonomy I have is going to grow quite large, 100 or so terms, so the client only wanted top level ones visible, and then clicked down through to refine the categorisation of the event that was being submitted on the form. They have about 8 top level terms, and the rest are more specialised as you drill down.

jrockowitz’s picture

@stella That requirement makes sense. I remember a similar feature being included in the hierarchical select module. I think the UX would need to more refined before being included in the Webform core module. For example, any term with sub-terms should have an open/close indicator, like an arrow. Hopefully, the webform module can just integrate with one of the available hierarchical selection module.

For now, the scrolling behavior will allow for 100+ terms to be displayed.

BTW, this custom hide/show code could be moved to a recipe.

  • jrockowitz committed a592549 on 8.x-5.x
    Issue #2864400 by jrockowitz, stella: Hierarchical taxonomy checkboxes
    
jrockowitz’s picture

Status: Needs review » Fixed

Status: Fixed » Closed (fixed)

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