1

Webform elements can be altered using the function MYMODULE_form_alter. However the changes are not permanent. For example, if you're changing the title of an element from '#title' => t('Old Title') to '#title' => t('New Title') ,the change will not affect source - the (View) Source page will still show '#title' => t('Old Title')
So my question is: Is it possible to alter the source (YAML) from code because that will make changes permanent.I have tried to locate weformX.yml for webformX inorder to alter it in code but I can not find.

1

You can alter the yaml source of a webform by loading the config entity and modify elements:

use Drupal\webform\Entity\Webform;
use Drupal\Core\Serialization\Yaml;

  $webform = Webform::load('contact');
  $elements = Yaml::decode($webform->get('elements'));
  $elements['name']['#title'] = 'Please fill in your name';
  $webform->set('elements', Yaml::encode($elements));
  $webform->save();
  • This is the right answer it is working.But somebody down-voted my question without a comment. I use this to populate cascaded dropdowns instead of hitting the db again and again in form_alter. This have improved the response time of the application – Mundile Oct 30 '17 at 8:33
  • I think this is a very good question, because this is a special case. The yaml source of the form elements is not like normal configuration, it's embedded in the webform config entity. So you have to load the configuration and extract the value or if you export the configuration you have yaml embedded in yaml. – 4k4 Oct 30 '17 at 8:55

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.