You are here

function webform_update_8170 in Webform 6.x

Same name and namespace in other branches
  1. 8.5 includes/webform.install.update.inc \webform_update_8170()

Issue #3064070: Split STATE_DRAFT into STATE_DRAFT_CREATED and STATE_DRAFT_UPDATE.

File

includes/webform.install.update.inc, line 3156
Archived Webform update hooks.

Code

function webform_update_8170() {

  // List of handler is that use webform submission state.
  $states_handler_ids = [
    'action',
    'email',
    'scheduled_email',
    'remote_post',
  ];
  $config_factory = \Drupal::configFactory();
  foreach ($config_factory
    ->listAll('webform.webform.') as $webform_config_name) {
    $webform_config = $config_factory
      ->getEditable($webform_config_name);
    $data = $webform_config
      ->getRawData();
    $handler_updated = FALSE;
    foreach ($data['handlers'] as &$handler) {
      $settings = $handler['settings'];
      if (!in_array($handler['id'], $states_handler_ids)) {
        continue;
      }
      if (isset($settings['states']) && is_array($settings['states']) && in_array('draft', $settings['states'])) {

        // Remove old 'draft' state.
        WebformArrayHelper::removeValue($settings['states'], 'draft');

        // Prepend new 'draft_created' and 'draft_updated' states.
        $settings['states'] = array_merge([
          'draft_created',
          'draft_updated',
        ], $settings['states']);
      }
      if ($handler['id'] === 'remote_post' && isset($settings['draft_url'])) {
        $settings['draft_created_url'] = $settings['draft_url'];
        $settings['draft_created_custom_data'] = $settings['draft_custom_data'];
        $settings['draft_updated_url'] = $settings['draft_url'];
        $settings['draft_updated_custom_data'] = $settings['draft_custom_data'];
        unset($settings['draft_url']);
        unset($settings['draft_custom_data']);
      }
      if ($handler['settings'] !== $settings) {
        $handler_updated = TRUE;
        $handler['settings'] = $settings;
      }
    }
    if ($handler_updated) {
      $webform_config
        ->setData($data)
        ->save();
    }
  }
}