0

I'm preprocessing a webform using hook_webform_submission_form_alter. The webform is being rendered as a node field. How can I, from inside my form_alter hook access that node?

function MY_MODULE_webform_submission_form_alter(array &$form, FormStateInterface $form_state, $form_id) {
    $parameters = \Drupal::routeMatch()->getParameters();

    if ($parameters->has('node')) {

        // processing goes here
    }
}

I'm fetching it from the url parameter right now, but I would prefer to do this in a cleaner way.

0

After some digging, I've found out the answer.

This is how I get the webform submission, the webform object, and the associated node:

    function MY_MODULE_webform_submission_form_alter(array &$form, FormStateInterface $form_state, $form_id) {

        $entity  = $form_state->getFormObject()->getEntity(); // webform submission
        $webform = $form_state->getFormObject()->getWebform(); // webform object
        $node    = $entity->getSourceEntity(); // associated node

        // (...)
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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