You are here

public function PardotEventSubscriber::evaluateTrackingScope in Pardot Integration 8

Same name and namespace in other branches
  1. 2.x src/EventSubscriber/PardotEventSubscriber.php \Drupal\pardot\EventSubscriber\PardotEventSubscriber::evaluateTrackingScope()

Evaluates tracking scoping conditions and sets state setting.

Parameters

GetResponseEvent $event:

File

src/EventSubscriber/PardotEventSubscriber.php, line 77

Class

PardotEventSubscriber
Provides a response event subscriber for Pardot.

Namespace

Drupal\pardot\EventSubscriber

Code

public function evaluateTrackingScope(GetResponseEvent $event) {

  // Check if Pardot is configured with an account ID.
  if (null !== $this->config
    ->get('account_id')) {

    // Load use role condition configuration and current user.
    $user_role_condition = $this->config
      ->get('user_role_condition');
    $current_user = $this->user_storage
      ->load($this->account
      ->id());

    // Create user_role condition with Pardot user condition configuration.
    // Evaluate using current user context.
    $user_role = $this->condition_manager
      ->createInstance('user_role')
      ->setConfig('roles', $user_role_condition['roles'])
      ->setContextValue('user', $current_user);
    $user_role_result = $user_role
      ->evaluate();

    // Create request_path condition with Pardot path condition configuration.
    $request_path = $this->condition_manager
      ->createInstance('request_path');
    $request_path
      ->setConfiguration($this->config
      ->get('path_condition'));

    // Negate request_path_evaluate() if applicable.
    $is_negated = $request_path
      ->isNegated();
    $request_path_result = $request_path
      ->isNegated() ? !$request_path
      ->evaluate() : $request_path
      ->evaluate();
    if ($user_role_result && $request_path_result) {
      \Drupal::state()
        ->set('pardot.include_tracking', 1);
    }
    else {
      \Drupal::state()
        ->set('pardot.include_tracking', 0);
    }
  }
}