1

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?

1

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

<?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' => 'webform',
      '#webform' => 'contact',
      '#default_data' => [
        'name' => $name,
        'message' => $message,
      ],
    ];

    return $build;
  }

}

mymodule.routing.yml

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

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.