You are here

function adsense_display in Google AdSense integration 8

Same name and namespace in other branches
  1. 5.3 adsense.module \adsense_display()
  2. 5 adsense.module \adsense_display()
  3. 5.2 adsense.module \adsense_display()
  4. 6 adsense.module \adsense_display()
  5. 7 adsense.module \adsense_display()

Generates the Google AdSense Ad.

Parameters

array $args: An array of arguments (format, group, channel or slot). A valid format must always be provided. If a slot is provided, the ad is generated by the new format modules, if not then the 'old' format modules are attempted.

Return value

array Render array with ad code.

Throws

\Drupal\Component\Plugin\Exception\PluginException Exception thrown in the event of problems with the plugin.

File

./adsense.module, line 229
Displays Google AdSense ads on Drupal pages.

Code

function adsense_display(array $args) {
  if (!is_array($args)) {

    // 'old' method of calling this function is not supported in version >= 8.x
    // adsense_display($format, $group, $channel, $slot, $referral, $cpa).
    $text = 'old adsense_display call syntax not supported';
  }
  else {
    $ad = AdsenseAdBase::createAd($args);
    if (isset($ad)) {
      return $ad
        ->display();
    }
    else {
      $text = 'no ad generated.';
    }
  }
  return [
    '#type' => 'inline_template',
    '#template' => '<!-- adsense: {{ comment }} -->',
    '#context' => [
      'comment' => $text,
    ],
  ];
}