You are here

abstract class AdsenseAdBase in Google AdSense integration 8

Base class for the AdsenseAd plugins.

Hierarchy

Expanded class hierarchy of AdsenseAdBase

3 files declare their use of AdsenseAdBase
adsense.module in ./adsense.module
Displays Google AdSense ads on Drupal pages.
AdsenseFilter.php in src/Plugin/Filter/AdsenseFilter.php
AdsenseMainSettings.php in src/Form/AdsenseMainSettings.php

File

src/AdsenseAdBase.php, line 16

Namespace

Drupal\adsense
View source
abstract class AdsenseAdBase extends PluginBase implements AdsenseAdInterface, ContainerFactoryPluginInterface {
  use StringTranslationTrait;

  /**
   * Config factory.
   *
   * @var \Drupal\Core\Config\ConfigFactory
   */
  protected $configFactory;

  /**
   * Module handler.
   *
   * @var \Drupal\Core\Extension\ModuleHandlerInterface
   */
  protected $moduleHandler;

  /**
   * Current user.
   *
   * @var \Drupal\Core\Session\AccountProxyInterface
   */
  protected $currentUser;

  /**
   * Creates a new AdsenseAdBase instance.
   *
   * @param array $configuration
   *   A configuration array containing information about the plugin instance.
   * @param string $plugin_id
   *   The plugin_id for the plugin instance.
   * @param mixed|null $plugin_definition
   *   The plugin implementation definition.
   * @param \Drupal\Core\Config\ConfigFactoryInterface|null $config_factory
   *   The config factory.
   * @param \Drupal\Core\Extension\ModuleHandlerInterface|null $module_handler
   *   The module handler.
   * @param \Drupal\Core\Session\AccountProxyInterface|null $current_user
   *   The current user.
   */
  public function __construct(array $configuration, $plugin_id = '', $plugin_definition = NULL, ConfigFactoryInterface $config_factory = NULL, ModuleHandlerInterface $module_handler = NULL, AccountProxyInterface $current_user = NULL) {
    parent::__construct($configuration, $plugin_id, $plugin_definition);
    $this->configFactory = $config_factory ?: \Drupal::configFactory();
    $this->moduleHandler = $module_handler ?: \Drupal::moduleHandler();
    $this->currentUser = $current_user ?: \Drupal::currentUser();
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
    return new static($configuration, $plugin_id, $plugin_definition, $container
      ->get('config.factory'), $container
      ->get('module_handler'), $container
      ->get('current_user'));
  }

  /**
   * {@inheritdoc}
   */
  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;
  }

  /**
   * Display ad HTML.
   *
   * @param array $classes
   *   Set of classes to add to the ad HTML.
   *
   * @return array
   *   render array with ad or placeholder depending on current configuration.
   */
  public function display(array $classes = []) {
    $config = $this->configFactory
      ->get('adsense.settings');
    $libraries = [
      'adsense/adsense.css',
    ];
    $text = '';
    if ($this
      ->isDisabled($text)) {
      return [
        '#type' => 'inline_template',
        '#template' => '<!-- adsense: {{ comment }} -->',
        '#context' => [
          'comment' => $text,
        ],
      ];
    }
    elseif ($config
      ->get('adsense_test_mode') || $this->currentUser
      ->hasPermission('show adsense placeholders')) {

      // Show ad placeholder.
      $content = $this
        ->getAdPlaceholder();
      return [
        '#theme' => 'adsense_ad',
        '#content' => $content['#content'],
        '#width' => isset($content['#width']) ? $content['#width'] : NULL,
        '#height' => isset($content['#height']) ? $content['#height'] : NULL,
        '#format' => $content['#format'],
        '#classes' => array_merge([
          'adsense-placeholder',
        ], $classes),
        '#attached' => [
          'library' => $libraries,
        ],
      ];
    }
    else {

      // Display ad-block disabling request.
      if ($config
        ->get('adsense_unblock_ads')) {
        $libraries[] = 'adsense/adsense.unblock';
      }
      $content = $this
        ->getAdContent();

      // Show ad.
      return [
        '#theme' => 'adsense_ad',
        '#content' => $content,
        '#width' => isset($content['#width']) ? $content['#width'] : NULL,
        '#height' => isset($content['#height']) ? $content['#height'] : NULL,
        '#format' => isset($content['#format']) ? $content['#format'] : NULL,
        '#classes' => $classes,
        '#attached' => [
          'library' => $libraries,
        ],
      ];
    }
  }

  /**
   * Check if ads display is disabled.
   *
   * @param string $text
   *   Reason for the ad display being disabled.
   *
   * @return bool
   *   TRUE if ads are disabled.
   */
  public static function isDisabled(&$text = '') {
    $account = \Drupal::currentUser();
    $config = \Drupal::config('adsense.settings');
    if (!$config
      ->get('adsense_basic_id')) {
      $text = 'no publisher id configured.';
    }
    elseif ($config
      ->get('adsense_disable')) {
      $text = 'adsense disabled.';
    }
    elseif ($account
      ->id() != 1 && $account
      ->hasPermission('hide adsense')) {
      $text = 'disabled for current user.';
    }
    else {
      return FALSE;
    }
    return TRUE;
  }

  /**
   * List of available languages.
   *
   * @return array
   *   array of language options with the key used by Google and description.
   */
  public static function adsenseLanguages() {
    return [
      'ar' => t('Arabic'),
      'bg' => t('Bulgarian'),
      'ca' => t('Catalan'),
      'zh-Hans' => t('Chinese Simplified'),
      'zh-TW' => t('Chinese Traditional'),
      'hr' => t('Croatian'),
      'cs' => t('Czech'),
      'da' => t('Danish'),
      'nl' => t('Dutch'),
      'en' => t('English'),
      'et' => t('Estonian'),
      'fi' => t('Finnish'),
      'fr' => t('French'),
      'de' => t('German'),
      'el' => t('Greek'),
      'iw' => t('Hebrew'),
      'hi' => t('Hindi'),
      'hu' => t('Hungarian'),
      'is' => t('Icelandic'),
      'in' => t('Indonesian'),
      'it' => t('Italian'),
      'ja' => t('Japanese'),
      'ko' => t('Korean'),
      'lv' => t('Latvian'),
      'lt' => t('Lithuanian'),
      'no' => t('Norwegian'),
      'pl' => t('Polish'),
      'pt' => t('Portuguese'),
      'ro' => t('Romanian'),
      'ru' => t('Russian'),
      'sr' => t('Serbian'),
      'sk' => t('Slovak'),
      'sl' => t('Slovenian'),
      'es' => t('Spanish'),
      'sv' => t('Swedish'),
      'th' => t('Thai'),
      'tr' => t('Turkish'),
      'uk' => t('Ukrainian'),
      'vi' => t('Vietnamese'),
    ];
  }

}

Members

Namesort descending Modifiers Type Description Overrides
AdsenseAdBase::$configFactory protected property Config factory.
AdsenseAdBase::$currentUser protected property Current user.
AdsenseAdBase::$moduleHandler protected property Module handler.
AdsenseAdBase::adsenseLanguages public static function List of available languages. Overrides AdsenseAdInterface::adsenseLanguages
AdsenseAdBase::create public static function Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface::create
AdsenseAdBase::createAd public static function Creates the ad object, as specified by the definitions in the parameter. Overrides AdsenseAdInterface::createAd
AdsenseAdBase::display public function Display ad HTML.
AdsenseAdBase::isDisabled public static function Check if ads display is disabled.
AdsenseAdBase::__construct public function Creates a new AdsenseAdBase instance. Overrides PluginBase::__construct 5
AdsenseAdInterface::adsenseAdFormats public static function This is the array that holds all ad formats. 3
AdsenseAdInterface::getAdContent public function Return the ad content. 5
AdsenseAdInterface::getAdPlaceholder public function Return the ad placeholder. 5
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 3
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.