1

I'm having a heck of a time setting the start date and end date for a Webform.

We've got a node with a webform entity reference field that generates a new Webform. On that node form, we want to give people the option to set the new form's status. Here's a trimmed-down look at our code (I've omitted most of the superfluous stuff)...

// Get the status from the node creation form
$reg_form_status = $form_state->getValue('field_registration_form')[0]['settings']['status'];
// Get the scheduled dates
$reg_form_scheduled_open = $form_state->getValue('field_registration_form')[0]['settings']['scheduled']['open'];
$reg_form_scheduled_close = $form_state->getValue('field_registration_form')[0]['settings']['scheduled']['close'];
// create a new instance of our pre-generated webform
$webform = \Drupal::entityTypeManager()->getStorage('webform')->load('student_registration');
if (!empty($webform)) {
  $duplicate = $webform->createDuplicate();
  $duplicate->set('status',$reg_form_status);
  $duplicate->set('open',$reg_form_scheduled_open);
  $duplicate->set('close',$reg_form_scheduled_close);
  $duplicate->save();
}

Without setting the open/close dates, the form is created just fine. If it's set as "Open", then it sets correctly as open. If it's set as "Closed", then it is created as closed. However, if I try to set it as Scheduled, it defaults to open. If I try to set it as scheduled with the dates, then it throws Invalid data type for config element when saving the node.

I tried re-casting as a date object, but that doesn't seem to be working either.

$event_date_start = new DrupalDateTime($reg_form_scheduled_open, 'EST');
$event_date_close = new DrupalDateTime($reg_form_scheduled_close, 'EST');

with

$duplicate->set('open',$event_date_start);
$duplicate->set('close',$event_date_close);

Which hasn't resolved this issue. Here's the full stack trace...

An error has occurred: Drupal\Core\Config\UnsupportedDataTypeConfigException: Invalid data type for config element webform.webform.student_registration2006:open in /var/www/docroot/core/lib/Drupal/Core/Config/StorableConfigBase.php:207 Stack trace: #0 /var/www/docroot/core/lib/Drupal/Core/Config/Config.php(212): Drupal\Core\Config\StorableConfigBase->castValue('open', Object(Drupal\Core\Datetime\DrupalDateTime)) #1 /var/www/docroot/core/lib/Drupal/Core/Config/Entity/ConfigEntityStorage.php(284): Drupal\Core\Config\Config->save(false) #2 /var/www/docroot/core/lib/Drupal/Core/Entity/EntityStorageBase.php(457): Drupal\Core\Config\Entity\ConfigEntityStorage->doSave('student_registr...', Object(Drupal\webform\Entity\Webform)) #3 /var/www/docroot/core/lib/Drupal/Core/Config/Entity/ConfigEntityStorage.php(263): Drupal\Core\Entity\EntityStorageBase->save(Object(Drupal\webform\Entity\Webform)) #4 /var/www/docroot/modules/contrib/webform/src/WebformEntityStorage.php(133): Drupal\Core\Config\Entity\ConfigEntityStorage->save(Object(Drupal\webform\Entity\Webform)) #5 /var/www/docroot/core/lib/Drupal/Core/Entity/EntityBase.php(395): Drupal\webform\WebformEntityStorage->save(Object(Drupal\webform\Entity\Webform)) #6 /var/www/docroot/core/lib/Drupal/Core/Config/Entity/ConfigEntityBase.php(616): Drupal\Core\Entity\EntityBase->save() #7 /var/www/docroot/modules/custom/shgt_theme_helper/shgt_theme_helper.module(288): Drupal\Core\Config\Entity\ConfigEntityBase->save() #8 [internal function]: shgt_theme_helper_event_by_county(Array, Object(Drupal\Core\Form\FormState)) #9 /var/www/docroot/core/lib/Drupal/Core/Form/FormSubmitter.php(114): call_user_func_array('shgt_theme_help...', Array) #10 /var/www/docroot/core/lib/Drupal/Core/Form/FormSubmitter.php(52): Drupal\Core\Form\FormSubmitter->executeSubmitHandlers(Array, Object(Drupal\Core\Form\FormState)) #11 /var/www/docroot/core/lib/Drupal/Core/Form/FormBuilder.php(593): Drupal\Core\Form\FormSubmitter->doSubmitForm(Array, Object(Drupal\Core\Form\FormState)) #12 /var/www/docroot/modules/contrib/autosave_form/src/Form/AutosaveFormBuilder.php(144): Drupal\Core\Form\FormBuilder->processForm('node_event_by_c...', Array, Object(Drupal\Core\Form\FormState)) #13 /var/www/docroot/core/lib/Drupal/Core/Form/FormBuilder.php(321): Drupal\autosave_form\Form\AutosaveFormBuilder->processForm('node_event_by_c...', Array, Object(Drupal\Core\Form\FormState)) #14 /var/www/docroot/modules/contrib/autosave_form/src/Form/AutosaveFormBuilder.php(97): Drupal\Core\Form\FormBuilder->buildForm(Object(Drupal\node\NodeForm), Object(Drupal\Core\Form\FormState)) #15 /var/www/docroot/core/lib/Drupal/Core/Controller/FormController.php(91): Drupal\autosave_form\Form\AutosaveFormBuilder->buildForm(Object(Drupal\node\NodeForm), Object(Drupal\Core\Form\FormState)) #16 /var/www/docroot/core/modules/layout_builder/src/Controller/LayoutBuilderHtmlEntityFormController.php(39): Drupal\Core\Controller\FormController->getContentResult(Object(Symfony\Component\HttpFoundation\Request), Object(Drupal\Core\Routing\RouteMatch)) #17 [internal function]: Drupal\layout_builder\Controller\LayoutBuilderHtmlEntityFormController->getContentResult(Object(Symfony\Component\HttpFoundation\Request), Object(Drupal\Core\Routing\RouteMatch)) #18 /var/www/docroot/core/lib/Drupal/Core/EventSubscriber/EarlyRenderingControllerWrapperSubscriber.php(123): call_user_func_array(Array, Array) #19 /var/www/docroot/core/lib/Drupal/Core/Render/Renderer.php(573): Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}() #20 /var/www/docroot/core/lib/Drupal/Core/EventSubscriber/EarlyRenderingControllerWrapperSubscriber.php(124): Drupal\Core\Render\Renderer->executeInRenderContext(Object(Drupal\Core\Render\RenderContext), Object(Closure)) #21 /var/www/docroot/core/lib/Drupal/Core/EventSubscriber/EarlyRenderingControllerWrapperSubscriber.php(97): Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->wrapControllerExecutionInRenderContext(Array, Array) #22 /var/www/vendor/symfony/http-kernel/HttpKernel.php(151): Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}() #23 /var/www/vendor/symfony/http-kernel/HttpKernel.php(68): Symfony\Component\HttpKernel\HttpKernel->handleRaw(Object(Symfony\Component\HttpFoundation\Request), 1) #24 /var/www/docroot/modules/contrib/simple_oauth/src/HttpMiddleware/BasicAuthSwap.php(67): Symfony\Component\HttpKernel\HttpKernel->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true) #25 /var/www/docroot/core/lib/Drupal/Core/StackMiddleware/Session.php(57): Drupal\simple_oauth\HttpMiddleware\BasicAuthSwap->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true) #26 /var/www/docroot/core/lib/Drupal/Core/StackMiddleware/KernelPreHandle.php(47): Drupal\Core\StackMiddleware\Session->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true) #27 /var/www/docroot/core/lib/Drupal/Core/StackMiddleware/ReverseProxyMiddleware.php(47): Drupal\Core\StackMiddleware\KernelPreHandle->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true) #28 /var/www/docroot/core/lib/Drupal/Core/StackMiddleware/NegotiationMiddleware.php(52): Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true) #29 /var/www/vendor/stack/builder/src/Stack/StackedHttpKernel.php(23): Drupal\Core\StackMiddleware\NegotiationMiddleware->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true) #30 /var/www/docroot/core/lib/Drupal/Core/DrupalKernel.php(708): Stack\StackedHttpKernel->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true) #31 /var/www/docroot/index.php(19): Drupal\Core\DrupalKernel->handle(Object(Symfony\Component\HttpFoundation\Request)) #32 {main}

Does anyone have a working example or link to documentation on setting the open/close date on a webform programmatically?

0

1 Answer 1

1

I believe I may have found the answer in WebformEntityTest.php in the Webform module. There, a test creates a webform with the following settings for the scheduled dates...

$webform->setStatus(WebformInterface::STATUS_SCHEDULED);
// Check set open date to yesterday.
$webform->set('open', date('Y-m-d\TH:i:s', strtotime('today -1 days')));
$webform->set('close', NULL);
$this->assertTrue($webform->isOpen());</code>

So I've done some testing, and for my use case, the following seems to be working so far.

$duplicate->set('open',date('Y-m-d\TH:i:s', strtotime($reg_form_scheduled_open)));
$duplicate->set('close',date('Y-m-d\TH:i:s', strtotime($reg_form_scheduled_close)));

If anyone has any remaining input on this, I'd appreciate that. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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