You are here

function hubspot_post_update_flip_field_mapping in HubSpot 3.x

Flip field mapping keys and values.

File

./hubspot.post_update.php, line 54
Post Update hook file for hubspot.

Code

function hubspot_post_update_flip_field_mapping(&$sandbox) {
  $webform_storage = \Drupal::entityTypeManager()
    ->getStorage('webform');

  /** @var \Drupal\webform\WebformInterface[] $webforms */
  $webforms = $webform_storage
    ->loadMultiple();
  foreach ($webforms as $webform) {
    $updated = FALSE;
    foreach ($webform
      ->getHandlers() as $handler) {
      if ($handler instanceof HubspotWebformHandler) {
        $hubspot_mapping = $handler
          ->getSettings();
        $hubspot_mapping['field_mapping'] = array_flip($hubspot_mapping['field_mapping']);
        $handler
          ->setSettings($hubspot_mapping);
        $updated = TRUE;
      }
    }
    if ($updated) {
      $webform
        ->save();
    }
  }
}