You are here

public function AdsenseFilter::process in Google AdSense integration 8

Performs the filter processing.

Parameters

string $text: The text string to be filtered.

string $langcode: The language code of the text to be filtered.

Return value

\Drupal\filter\FilterProcessResult The filtered text, wrapped in a FilterProcessResult object, and possibly with associated assets, cacheability metadata and placeholders.

Overrides FilterInterface::process

See also

\Drupal\filter\FilterProcessResult

File

src/Plugin/Filter/AdsenseFilter.php, line 89

Class

AdsenseFilter
Provides a filter for AdSense input tags.

Namespace

Drupal\adsense\Plugin\Filter

Code

public function process($text, $langcode) {
  $patterns = [
    'block' => '/\\[adsense:block:([^\\]]+)\\]/x',
    'oldtag' => '/\\[adsense:([^:]+):(\\d*):(\\d*):?(\\w*)\\]/x',
    'tag' => '/\\[adsense:([^:]+):([^:\\]]+)\\]/x',
  ];
  $modified = FALSE;
  foreach ($patterns as $mode => $pattern) {
    if (preg_match_all($pattern, $text, $matches, PREG_SET_ORDER)) {
      foreach ($matches as $match) {
        $ad = NULL;
        switch ($mode) {
          case 'block':

            // adsense:block:name.
            // Get the block with the same machine name as the tag.
            try {
              $module_blocks = $this->blockStorage
                ->loadByProperties([
                'id' => $match[1],
              ]);
            } catch (\Exception $e) {
              $module_blocks = [];
            }

            /** @var \Drupal\block\Entity\Block $block */
            foreach ($module_blocks as $block) {
              if ($block
                ->getPlugin() instanceof AdBlockInterface) {
                $ad = $block
                  ->getPlugin()
                  ->createAd();
              }
            }
            break;
          case 'oldtag':

            // adsense:format:group:channel:slot.
            try {
              $ad = AdsenseAdBase::createAd([
                'format' => $match[1],
                'group' => $match[2],
                'channel' => $match[3],
                'slot' => $match[4],
              ]);
            } catch (PluginException $e) {

              // Do nothing.
            }
            break;
          case 'tag':

            // adsense:format:slot.
            try {
              $ad = AdsenseAdBase::createAd([
                'format' => $match[1],
                'slot' => $match[2],
              ]);
            } catch (PluginException $e) {

              // Do nothing.
            }
            break;
        }

        // Replace the first occurrence of the tag, in case we have the same
        // tag more than once.
        if (isset($ad)) {
          $modified = TRUE;
          $ad_array = $ad
            ->display();
          try {
            $ad_text = $this->renderer
              ->render($ad_array);
            $text = preg_replace('/\\' . $match[0] . '/', $ad_text, $text);
          } catch (\Exception $e) {

            // Do nothing.
          }
        }
      }
    }
  }
  $result = new FilterProcessResult($text);
  if ($modified) {
    $result
      ->addAttachments([
      'library' => [
        'adsense/adsense.css',
      ],
    ]);
    if ($this->config
      ->get('adsense_unblock_ads')) {
      $result
        ->addAttachments([
        'library' => [
          'adsense/adsense.unblock',
        ],
      ]);
    }
  }
  return $result;
}