1

I am hoping to use the selected option of one Webform element ("Organization") as the argument in an Entity Select element ("Site") using Views handler.

organization:
  '#type': entity_select
  '#title': 'What is the name of your organization?'
  '#empty_option': 'Please select'
  '#target_type': node
  '#selection_handler': 'default:node'
  '#selection_settings':
    target_bundles:
      organizations: organizations
site:
  '#type': entity_select
  '#title': Site
  '#target_type': node
  '#selection_handler': views
  '#selection_settings':
    view:
      view_name: sites
      display_name: entity_reference_1
      arguments:
        - 'WHAT DO I PUT HERE TO REPRESENT ORGANIZATION?'

I wouldn't expect this to happen with tokens, which become available after submission, but I was thinking this could potentially be done using "data", which is available to Webform Computed Twig elements. I tried adding '#ajax': true to the Entity Select element and then putting {{ data.organization }} as the argument, but that didn't work.

Thank in advance for any tips!

1 Answer 1

0

So my colleague, @nikathone, provided the following code, to be put in a .module file. Replace the MYMODULE and MY_WEBFORM_ID according to your needs:

<?php

use Drupal\Core\Form\FormStateInterface;
use Drupal\webform\Element\WebformEntitySelect;

/**
 * Implements hook_webform_submission_form_alter().
 */
function MYMODULE_webform_submission_form_alter(array &$form, FormStateInterface $form_state, $form_id) {
  $webform_id = $form['#webform_id'] ?? '';
  if ($webform_id !== 'MY_WEBFORM_ID') {
    return;
  }

  $organization_element = $form['elements']['organization'] ?? [];
  $site_element = $form['elements']['site'] ?? [];
  if (!$organization_element || !$site_element || empty($site_element['#selection_settings']['view'])) {
    return;
  }

  // Settings ajax related properties.
  $site_element_wrapper_id = $site_element['#webform_id'] . '--wrapper';
  $organization_element['#ajax'] = [
    'callback' => 'MYMODULE_webform_submission_form_organization_element_ajax_callback',
    'wrapper' => $site_element_wrapper_id,
  ];
  $site_element['#prefix'] = '<div id="' . $site_element_wrapper_id . '">';
  $site_element['#suffix'] = '</div>';

  // Attempting to retrieve possible selected organization.
  if (!($selected_organization = $form_state->getValue('organization'))) {
    $selected_organization = $organization_element['#default_value'] ?? 0;
  }
  // Apply the selected organization if a selected one was found.
  if ($selected_organization) {
    $site_element['#selection_settings']['view']['arguments'] = [$selected_organization];
    // Reset the options so that we can repopulate it again. Without this the
    // ::setOptions method above won't process the new arguments.
    $site_element['#options'] = [];
    $form_object = $form_state->getFormObject();
    $settings = ['webform_submission' => $form_object->getEntity()];
    WebformEntitySelect::setOptions($site_element, $settings);
  }

  $form['elements']['organization'] = $organization_element;
  $form['elements']['site'] = $site_element;
}

/**
 * Ajax callback for the site element.
 *
 * @param array $form
 *   The full form element.
 * @param \Drupal\Core\Form\FormStateInterface $form_state
 *   The form state.
 *
 * @return array
 *   The site element array.
 */
function MYMODULE_webform_submission_form_organization_element_ajax_callback(array $form, FormStateInterface $form_state) {
  return $form['elements']['site'];
}

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.