You are here

public static function AdsenseAdBase::createAd in Google AdSense integration 8

Creates the ad object, as specified by the definitions in the parameter.

Parameters

array $args: Definitions of the ad object as per the AdsenseAd annotation.

Return value

AdsenseAdBase Created ad object.

Throws

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

Overrides AdsenseAdInterface::createAd

2 calls to AdsenseAdBase::createAd()
AdsenseFilter::process in src/Plugin/Filter/AdsenseFilter.php
Performs the filter processing.
adsense_display in ./adsense.module
Generates the Google AdSense Ad.

File

src/AdsenseAdBase.php, line 80

Class

AdsenseAdBase
Base class for the AdsenseAd plugins.

Namespace

Drupal\adsense

Code

public static function createAd(array $args) {
  $version = 1;
  $is_search = FALSE;
  if (!empty($args['format']) && substr($args['format'], 0, 10) == 'Search Box') {
    $is_search = TRUE;
    switch ($args['format']) {
      case 'Search Box':
        $version = 1;
        break;
      case 'Search Box v2':
        $version = 2;
        break;
    }
  }
  $needs_slot = !empty($args['slot']);

  // Search for the AdsenseAd plugins.

  /** @var \Drupal\adsense\AdsenseAdManager $manager */
  $manager = \Drupal::service('plugin.manager.adsensead');
  $plugins = $manager
    ->getDefinitions();
  foreach ($plugins as $plugin) {
    if ($plugin['isSearch'] == $is_search && $plugin['needsSlot'] == $needs_slot && $plugin['version'] == $version) {

      // Return an ad created by the compatible plugin.
      return $manager
        ->createInstance($plugin['id'], $args);
    }
  }
  return NULL;
}