0

I have a integer field in the Webform Node content type, I want to modify the webform's "Limit total" setting base on the value of integer field.

So I created a custom module, use hook_form_alter to get the target_id from webform reference field and get value from integer field.

And then use hook_update_N to modify the configuration of referenced webform. The variable $ref_webform is for webform id, "webform.webform.$ref_webform" is equal to the configuration's name. But it's not work.

Please give me some suggestions, thank you so much!

Following is my code:

/**
 **** Implementation of hook_form_alter().
 ****/
use Drupal\Core\Form\FormStateInterface;
function limit_total_submission_field_form_node_form_alter(&$form, FormStateInterface $form_state, $form_id) {
        global $ref_webform;
        global $limit_total;
        $ref_webform = $form[webform][widget][0][target_id]['#default_value'];
        $limit_total = $form[field_limit_total][widget][0][value]['#default_value'];
        foreach (array_keys($form['actions']) as $action) {
                if ($action != 'preview' && isset($form['actions'][$action]['#type']) && $form['actions'][$action]['#type'] === 'submit') {
                        $form['actions'][$action]['#submit'][] = 'limit_total_submission_field_update_8001';
                }
        }
}
function limit_total_submission_field_update_8001() {
        global $ref_webform;
        global $limit_total;
        $config_factory = \Drupal::configFactory();
        $config = $config_factory->getEditable('webform.webform.{$ref_webform}');
        $config->set('limit_total', '$limit_total');
        $config->save(TRUE);
}

Your Answer

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

Browse other questions tagged or ask your own question.