Skip to main content

Drupal 8, Webform: How to update webform status programmaticaly

This is the code i am using for updating the webform status programmatically.

    //Check if webform exists already this might be true for event translated content
    $webform_exists = \Drupal::entityTypeManager()->getStorage('webform')->load('event_registration_gr_1118');

    if ($webform_exists instanceof \Drupal\webform\WebformInterface) {
      /*
      // Possible values ->setStatus(true,false,'scheduled') είναι οκ
      // If $webform_exists->setStatus(true) then it is always open even if you have scheduled dates
      // If $webform->setStatus(false) then the webform is closed (scheduled date fields are also ignored)
      // if $webform->setStatus(scheduled) then you must add a starting and closing date (open,close)
      */
      $webform_status = 'scheduled';
      $webform_open = new DrupalDateTime(date('d-m-Y H:i',time() + (86400 * 10)));
      $webform_close = new DrupalDateTime(date('d-m-Y H:i',time() + 86400));

      $webform_exists->setStatus($webform_status);
      $webform_exists->set('open', $webform_open->format('Y-m-d\TH:i:00'));
      $webform_exists->set('close', $webform_close->format('Y-m-d\TH:i:00'));
      $webform_exists->save();
    }

 

webform