1

I am trying to get the Category name (from the General form settings) for webforms in a preprocess function, ultimately to add as a class to the webform wrapper.

MYMODULE.module:

function MYMODULE_preprocess_webform(&$variables) {
  kint($variables);
}

Right now I don't see a "category" value in the array structure.

How do you get this value in preprocess? Maybe I need a different hook?

3 Answers 3

0

Not quite clear on the question actually. So you're saying you want to get the settings value from webform element "Category" list before it's submitted and set it as your form wrapper?

Here's some tip I recently learned, hope it helps.


// get all webform submissions id from my_webform
$query = \Drupal::entityQuery('webform_submission')->condition('webform_id', 'my_webform');
$result = $query->execute();

// load all submission data from webform submission storage
$webformStorage = \Drupal::entityTypeManager()->getStorage('webform_submission');
$submissions = $webformStorage->loadMultiple($result);

If you want to get the submission values form webform, then you can use WebformSubmission class.

If your want to get the element settings value that user sees from your category list, then you'll need Webform class to load the options array from the webform element.

use Drupal\webform\Entity\WebformSubmission;
use Drupal\webform\Entity\Webform;

// get sid from your preprocess or elsewhere
$webform_submission = WebformSubmission::load($sid);

// This will return the data array of submitted values from sid given.
$data = $webform_submission->getData();

// Load webform with bundle type id, normally it's your webform machine name
$webform = Webform::load($sid->bundle());

// Get element definition in webform element
$select_list_element = $webform->getElement('key_name');

// for options in Select list. this may vary depending on what element type you're using
$select_list = $select_list_element['#options'];

1
  • Hi @Michael Chen, thanks for the response. Not sure if that is the approach I need? The value I am trying to retrieve is from the webform 'General' settings form. I've added a bit to my question as I've tried some other approaches. And hopefully, any code would be webform ID agnostic. Scoped inside a custom Handler would be fine! Commented Apr 7, 2020 at 19:09
0

A couple approaches I found to accomplish this:

MYMODULE.module:

/**
 * Implements hook_form_alter().
 */
function MYMODULE_form_alter(&$form, FormStateInterface $form_state, $form_id) {

  if ($form['#webform_id']) {
    $webformCategory = strtolower($form_state->getFormObject()->getWebform()->get('category'));
    array_push($form['#attributes']['class'], 'webform-category-' . $webformCategory);
  }
}

^ There may be better ways to verify we have a webform other than if ($form['#webform_id']) to prevent errors from ->getWebform and ->getEntity methods on non-webform forms.

MYCUSTOMHANDLER.php:

class CustomHandler extends WebformHandlerBase {
  public function alterForm(array &$form, FormStateInterface $form_state, WebformSubmissionInterface $webform_submission) {
    $webformCategory = strtolower($webform_submission->getWebform()->get('category'));
    array_push($form['#attributes']['class'], 'webform-category-' . $webformCategory);
  }
}

This works well but would need to be included in each handler to apply this class to all webforms in question.

0

The webform isn't there yet in the preprocess function you need to load it first.

use Drupal\Component\Utility\Html;

/**
 * Implements hook_preprocess_HOOK().
 */
function MYMODULE_preprocess_webform(&$variables) {
  /** @var \Drupal\webform\WebformInterface $webform */
  $webform = \Drupal::service('entity_type.manager')->getStorage('webform')->load($variables['element']['#webform_id']);
  if (!empty($webform->toArray()['category'])) {
    $category = $webform->toArray()['category'];
    $variables['attributes']['class'][] = Html::cleanCssIdentifier('category-' . strtolower($category));
  }
  else {
    $variables['attributes']['class'][] = 'category-none';
  }
}

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.