You are here

public function GoogleAdWordsInlineJS::viewElements in Google AdWords Conversion Tracking 8

@returns [] Empty array We don't actually want any output, just tracking

Overrides FormatterInterface::viewElements

File

src/Plugin/Field/FieldFormatter/GoogleAdWordsInlineJS.php, line 62
Contains \Drupal\google_adwords\Plugin\Field\FieldFormatter\GoogleAdWordsInlineJS.

Class

GoogleAdWordsInlineJS
Plugin implementation of the 'google_adwords_inlinejs' formatter.

Namespace

Drupal\google_adwords\Plugin\Field\FieldFormatter

Code

public function viewElements(FieldItemListInterface $items, $langcode) {

  /**
   * @var \Drupal\google_adwords\GoogleAdwordsTracker $tracker
   */
  $tracker = \Drupal::getContainer()
    ->get('google_adwords.tracker');

  /**
   * @var \Drupal\Core\Config\ImmutableConfig $config
   *   saved settings for google_adwords
   */
  $config = \Drupal::config('google_adwords.settings');

  /**
   * @var \Drupal\Core\Session\AccountProxyInterface $user
   *   Current User
   */
  $user = \Drupal::currentUser();

  /**
   * @var array(string) $roles
   *  array of role ids for the current user
   */
  $roles = $user
    ->getRoles();
  if (is_array($roles)) {

    /**
     * @var string $role_id
     */
    foreach ($roles as $role_id) {

      // look for the first trackeable role

      /**
       * @TODO make this a single array in the $config
       *   There is no need for separate vars here (no efficieny, no memory)
       */
      if (TRUE || $config
        ->get('google_adwords_track_' . $role_id)) {

        // add each item as a themed conversion item
        foreach ($items as $delta => $item) {

          /**
           * @var \Drupal\Core\TypedData\Plugin\DataType\StringData $words
           */
          $words = $item
            ->get('words');
          $tracker
            ->addTracking($config
            ->get('conversion_id'), $config
            ->get('label'), (string) $words
            ->getCastedValue(), $langcode, $config
            ->get('color'), $config
            ->get('format'));
        }

        // break out of our role-checker (as we already passed at least 1 role)
        break;
      }
    }
  }

  /**
   * @return [] an empty array, we don't want output, we just want tracking
   */
  return [];
}