1

I have set up terms for a taxonomy vocabulary ("User Group"). Each taxonomy term has a field "Hide from selection", which defaults to False.

When rendering the widget (as a dropdown list in a form), I would like to hide the groups whose "Hide from selection" field is set to True. This field allows multiple input, as a user may belong to multiple groups.

How should I implement it using hooks? Thank you. I am new to Drupal.

5
  • 1
    For that to answer we need to know where exactly this list is getting rendered. Is it a form?
    – leymannx
    Commented Sep 4, 2023 at 6:24
  • Thank you for your comment @leymannx. Yes, it's supposed to be rendered in a form. I am using a module "Client-side Hierarchical Select" as nested units are expected.
    – John Doe
    Commented Sep 4, 2023 at 6:46
  • You should be able to do that in many ways. The Client-side Hierarchical Select has integration with views which means you can filter the referenced entites
    – Ziftman
    Commented Sep 9, 2023 at 21:39
  • view rer not working, I checked the module; a way to do achieve that is to create a custom element extending the CshsElement element then override the preRender function, and modify it to limit the options. at that point you would be able to see $element['#options'] and change the options, Just load the term and check if its true or not, or make a query and replace the whole options, also you can do it in processElement function as well.
    – Ziftman
    Commented Sep 9, 2023 at 22:07
  • I think best place is prerender before this line $element['#options'] = CshsOption::formatOptions($element), so after modify you send it to formatoption function
    – Ziftman
    Commented Sep 9, 2023 at 22:12

2 Answers 2

0

If your widget is a selectbox, you could use a custom EntityReferenceSelection described here or use the Hook hook_query_TAG_alter (your_module_query_entity_reference_alter) described here.

The module "Client-side Hierarchical Select" does currently not provide a Hook to alter the options. Only the status field is considered.
One way to accomplish your goal would be to create your own FieldWidget based on the Cshs widget or alter the options with a Hook like hook_field_widget_complete_WIDGET_TYPE_form_alter (your_module_field_widget_complete_cshs_form_alter).

2
  • Thank you. Unfortunately, my widget is Client Side Hierarchical Select. I will take a look at using Select Box instead. Thanks again.
    – John Doe
    Commented Sep 8, 2023 at 1:00
  • With the selectbox you can also use a Views Entity Reference Display to filter the terms. You can find an example here.
    – Chris4783
    Commented Sep 8, 2023 at 6:33
0

I found a way to do this using Drupal hook. It works with any widget type and also works with dynamically adding new widgets through Ajax.

use Drupal\Core\Entity\EntityInterface;


/**
 * Implements hook_form_alter().
 * 
 * For my_form_id, hide user_group term from available options
 * based on the value of a field of the term, "field_should_hide" 
 */
function my_module_form_alter(array &$form, FormStateInterface $form_state, string $form_id) {

  if($form_id === "my_form_id") {

    $term_ids_to_hide = [];

    // Get all the IDs of user group terms
    $term_ids = Drupal::entityQuery('taxonomy_term')->condition('vid', 'user_group')->execute();
    
    if (!empty($term_ids)) {
      // Load the terms, including their fields
      $terms = Term::loadMultiple($term_ids);

      foreach ($terms as $term) {
        // Store the Term IDs we want to hide
        $term_id = $term->id();
        $shouldHide = $term->get('field_should_hide')->value;

        if ($shouldHide == 1) {
          $term_ids_to_hide[] = $term_id;
        }
      }

      foreach ($form["field_user_group"]["widget"] as $w_key => $widget) {
        if (isset($widget["target_id"])) {
          $options = $widget["target_id"]["#options"];

          foreach ($options as $o_key => $value) {
            if (in_array($o_key, $term_ids_to_hide)) {
              // Hide the terms from widget option
              unset($form["field_user_group"]["widget"][$w_key]["target_id"]["#options"][$o_key]);
            }
          }
        }
      }
    }
  }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.