You are here

class ManagedAd in Google AdSense integration 8

Provides an AdSense managed ad unit.

Plugin annotation


@AdsenseAd(
  id = "managed",
  name = @Translation("Content ads"),
  isSearch = FALSE,
  needsSlot = TRUE
)

Hierarchy

Expanded class hierarchy of ManagedAd

2 files declare their use of ManagedAd
adsense.help.inc in help/adsense.help.inc
Help file for the settings tab of the adsense module.
ManagedAdBlock.php in src/Plugin/Block/ManagedAdBlock.php

File

src/Plugin/AdsenseAd/ManagedAd.php, line 18

Namespace

Drupal\adsense\Plugin\AdsenseAd
View source
class ManagedAd extends ContentAdBase {

  /**
   * Ad slot ID.
   *
   * @var string
   */
  private $slot;

  /**
   * Ad Shape (auto, horizontal, vertical, rectangle).
   *
   * @var string[]
   */
  private $shape;

  /**
   * Ad Layout key.
   *
   * @var string
   */
  private $layoutKey;

  /**
   * {@inheritdoc}
   */
  public function __construct(array $configuration, $plugin_id = '', $plugin_definition = NULL, $config_factory = NULL, $module_handler = NULL, $current_user = NULL) {
    parent::__construct($configuration, $plugin_id, $plugin_definition, $config_factory, $module_handler, $current_user);
    $fo = !empty($configuration['format']) ? $configuration['format'] : '';
    $sl = !empty($configuration['slot']) ? $configuration['slot'] : '';
    $sh = !empty($configuration['shape']) ? $configuration['shape'] : [
      'auto',
    ];
    $lk = !empty($configuration['layout_key']) ? $configuration['layout_key'] : '';
    if (substr($fo, 0, 10) != 'Search Box' && !empty($fo) && !empty($sl)) {
      $this->format = $fo;
      $this->slot = $sl;
      $this->shape = $sh;
      $this->layoutKey = $lk;
    }
  }

  /**
   * {@inheritdoc}
   */
  public function getAdPlaceholder() {
    if (!empty($this->format) && !empty($this->slot)) {
      $client = PublisherId::get();

      // Get width and height from the format.
      list($width, $height) = $this
        ->dimensions($this->format);
      $content = $this->configFactory
        ->get('adsense.settings')
        ->get('adsense_placeholder_text');
      $content .= "\nclient = ca-{$client}\nslot = {$this->slot}";
      if (ManagedAd::isResponsive($this->format) || ManagedAd::isFluid($this->format)) {
        $format = $this->format == 'responsive' ? implode(',', $this->shape) : $this->format;
        $content .= "\nformat = {$format}";
      }
      else {
        $content .= "\nwidth = {$width}\nheight = {$height}";
      }
      return [
        '#content' => [
          '#markup' => nl2br($content),
        ],
        '#format' => $this->format,
        '#width' => $width,
        '#height' => $height,
      ];
    }
    return [];
  }

  /**
   * {@inheritdoc}
   */
  public function getAdContent() {
    if (!empty($this->format) && !empty($this->slot)) {
      $config = $this->configFactory
        ->get('adsense.settings');
      $client = PublisherId::get();
      $this->moduleHandler
        ->alter('adsense', $client);
      $defer = $config
        ->get('adsense_managed_defer');
      if (ManagedAd::isResponsive($this->format)) {
        $shape = $this->format == 'responsive' ? implode(',', $this->shape) : $this->format;

        // Responsive smart sizing code.
        $content = [
          '#theme' => 'adsense_managed_responsive',
          '#format' => $this->format,
          '#client' => $client,
          '#slot' => $this->slot,
          '#shape' => $shape,
          '#defer' => $defer,
        ];
      }
      elseif (ManagedAd::isFluid($this->format)) {
        $style = 'display:block';
        if ($this->format == 'in-article') {
          $style .= '; text-align:center;';
        }

        // Responsive smart sizing code.
        $content = [
          '#theme' => 'adsense_managed_fluid',
          '#format' => $this->format,
          '#client' => $client,
          '#slot' => $this->slot,
          '#layout_key' => $this->layoutKey,
          '#style' => $style,
          '#defer' => $defer,
        ];
      }
      else {

        // Get width and height from the format.
        list($width, $height) = $this
          ->dimensions($this->format);
        if ($config
          ->get('adsense_managed_async')) {

          // Asynchronous code.
          $content = [
            '#theme' => 'adsense_managed_async',
            '#format' => $this->format,
            '#width' => $width,
            '#height' => $height,
            '#client' => $client,
            '#slot' => $this->slot,
            '#defer' => $defer,
          ];
        }
        else {
          $lang = $config
            ->get('adsense_secret_language');
          $secret = $lang ? "    google_language = '{$lang}';\n" : '';

          // Synchronous code.
          $content = [
            '#theme' => 'adsense_managed_sync',
            '#format' => $this->format,
            '#width' => $width,
            '#height' => $height,
            '#client' => $client,
            '#slot' => $this->slot,
            '#secret' => $secret,
            '#defer' => $defer,
          ];
        }
      }
      return $content;
    }
    return [];
  }

  /**
   * Helper function to determine if an ad format is responsive.
   *
   * @param string $format
   *   Ad format.
   *
   * @return bool
   *   TRUE if the ad is responsive.
   */
  private static function isResponsive($format) {
    return in_array($format, [
      'responsive',
      'link',
      'autorelaxed',
    ]);
  }

  /**
   * Helper function to determine if an ad format is fluid.
   *
   * @param string $format
   *   Ad format.
   *
   * @return bool
   *   TRUE if the ad is fluid.
   */
  private static function isFluid($format) {
    return in_array($format, [
      'in-article',
      'in-feed',
    ]);
  }

  /**
   * {@inheritdoc}
   */
  public static function adsenseAdFormats($key = NULL) {
    $ads = [
      'responsive' => [
        'desc' => t('Responsive ad unit'),
      ],
      'custom' => [
        'desc' => t('Custom size ad unit'),
      ],
      'autorelaxed' => [
        'desc' => t('Matched content'),
      ],
      'in-article' => [
        'desc' => t('In-article ad'),
      ],
      'in-feed' => [
        'desc' => t('In-feed ad'),
      ],
      // Top performing ad sizes.
      '300x250' => [
        'desc' => t('Medium Rectangle'),
      ],
      '336x280' => [
        'desc' => t('Large Rectangle'),
      ],
      '728x90' => [
        'desc' => t('Leaderboard'),
      ],
      '300x600' => [
        'desc' => t('Large Skyscraper'),
      ],
      '320x100' => [
        'desc' => t('Large Mobile Banner'),
      ],
      // Other supported ad sizes.
      '320x50' => [
        'desc' => t('Mobile Banner'),
      ],
      '468x60' => [
        'desc' => t('Banner'),
      ],
      '234x60' => [
        'desc' => t('Half Banner'),
      ],
      '120x600' => [
        'desc' => t('Skyscraper'),
      ],
      '120x240' => [
        'desc' => t('Vertical Banner'),
      ],
      '160x600' => [
        'desc' => t('Wide Skyscraper'),
      ],
      '300x1050' => [
        'desc' => t('Portrait'),
      ],
      '970x90' => [
        'desc' => t('Large Leaderboard'),
      ],
      '970x250' => [
        'desc' => t('Billboard'),
      ],
      '250x250' => [
        'desc' => t('Square'),
      ],
      '200x200' => [
        'desc' => t('Small Square'),
      ],
      '180x150' => [
        'desc' => t('Small Rectangle'),
      ],
      '125x125' => [
        'desc' => t('Button'),
      ],
      // 4-links.
      'link' => [
        'desc' => t('Responsive links'),
      ],
      '120x90' => [
        'desc' => t('4-links Vertical Small'),
      ],
      '160x90' => [
        'desc' => t('4-links Vertical Medium'),
      ],
      '180x90' => [
        'desc' => t('4-links Vertical Large'),
      ],
      '200x90' => [
        'desc' => t('4-links Vertical X-Large'),
      ],
      '468x15' => [
        'desc' => t('4-links Horizontal Medium'),
      ],
      '728x15' => [
        'desc' => t('4-links Horizontal Large'),
      ],
    ];
    if (!empty($key)) {
      return array_key_exists($key, $ads) ? $ads[$key] : NULL;
    }
    else {
      return $ads;
    }
  }

}

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.
ContentAdBase::$format protected property Ad Format.
ContentAdBase::dimensions public static function Provides the width and height of the ad.
ContentAdBase::getFormat public function Gets the ad format.
ManagedAd::$layoutKey private property Ad Layout key.
ManagedAd::$shape private property Ad Shape (auto, horizontal, vertical, rectangle).
ManagedAd::$slot private property Ad slot ID.
ManagedAd::adsenseAdFormats public static function This is the array that holds all ad formats. Overrides AdsenseAdInterface::adsenseAdFormats
ManagedAd::getAdContent public function Return the ad content. Overrides AdsenseAdInterface::getAdContent
ManagedAd::getAdPlaceholder public function Return the ad placeholder. Overrides AdsenseAdInterface::getAdPlaceholder
ManagedAd::isFluid private static function Helper function to determine if an ad format is fluid.
ManagedAd::isResponsive private static function Helper function to determine if an ad format is responsive.
ManagedAd::__construct public function Creates a new AdsenseAdBase instance. Overrides AdsenseAdBase::__construct
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.