Dynamic default values in WebForm Drupal 8.x We are working on our web projects with Drupal 8 and WebForm. Now we have a small problem ...

We have a webform that allows people to sign up for a workout. We would need to provide dynamic dates, so that the exercisers can register at that time.

Starting position: There are different groups that have a workout at different times. There is a fixed number per workout group. That can not be exceeded. People can sign out or sign in.

When a workout group is full, no one can register. Therefore, we must provide a selection of possible dates. This must be reprocessed every day.

Our problem: How can we create a dynamic ad on WebForm before viewing? Using element types, templates, preprocess or any else? Any hints or tips?
You can place a webform anywhere in code, examples are a controller, a block or a preprocess hook in a theme. Put the dynamic default values in #default_data:

/src/Controller/WebformController.php

&lt;?php

namespace Drupal\mymodule\Controller;

use Drupal\Core\Controller\ControllerBase;

/**
 * A webform controller.
 */
class WebformController extends ControllerBase {

  /**
   * {@inheritdoc}
   */
  public function webform() {

    // generate dynamic default values
    $name = 'John Doe';
    $message = 'This content is generated dynamically.';

    // add this to a webform
    $build['form']  = [
      '#type' =&gt; 'webform',
      '#webform' =&gt; 'contact',
      '#default_data' =&gt; [
        'name' =&gt; $name,
        'message' =&gt; $message,
      ],
    ];

    return $build;
  }

}


mymodule.routing.yml

mymodule.webform:
  path: '/mywebform'
  defaults:
    _controller: '\Drupal\mymodule\Controller\WebformController:webform'
    _title: 'My Webform'
  requirements:
    _permission: 'access content'