You are here

function pardot_form_contact_message_form_alter in Pardot Integration 8

Same name and namespace in other branches
  1. 2.x pardot.module \pardot_form_contact_message_form_alter()

Implements hook_form_FORM_ID_alter().

Add submit handler if the contact form has a Pardot mapping.

File

./pardot.module, line 66
Pardot integration module.

Code

function pardot_form_contact_message_form_alter(array &$form, FormStateInterface $form_state, $form_id) {

  // Get conditional state variable.
  // @see \Drupal\pardot\EventSubscriber\PardotEventSubscriber
  $include_tracking = (bool) \Drupal::state()
    ->get('pardot.include_tracking') ?: 0;
  $config = \Drupal::config('pardot.settings');
  if (null !== $config
    ->get('account_id') && $include_tracking) {
    $bundle = FALSE;
    $form_object = $form_state
      ->getFormObject();
    if ($form_object instanceof MessageForm) {
      $bundle = $form_object
        ->getEntity()
        ->bundle();
      $entity_manager = \Drupal::service('entity_type.manager');
      $config_entity = $entity_manager
        ->getStorage('pardot_contact_form_map')
        ->loadByProperties([
        'contact_form_id' => $bundle,
        'status' => TRUE,
      ]);

      // Add submit handler if we have an enabled contact form mapping.
      if ($config_entity) {
        $storage = $form_state
          ->getStorage();
        $storage['pardot_mapping'] = reset($config_entity);
        $form_state
          ->setStorage($storage);
        $form['actions']['submit']['#submit'][] = 'pardot_contact_form_submit';
      }
    }
  }
}