Skip to main content
3 of 4
deleted 187 characters in body
Prestosaurus
  • 2.1k
  • 1
  • 14
  • 29

Get Webform Category Name in Preprocess

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?


Success! (though not in preprocess)

Inside a custom handler using the ->get() method.

class CustomHandler extends WebformHandlerBase {
  public function alterForm(array &$form, FormStateInterface $form_state, WebformSubmissionInterface $webform_submission) {
    $webformCategory = $webform_submission->getWebform()->get('category');
    array_push($form['#attributes']['class'], $webformCategory);
  }
}
Prestosaurus
  • 2.1k
  • 1
  • 14
  • 29