You are here

public function AmpGoogleAdsenseBlock::build in Accelerated Mobile Pages (AMP) 8.2

Same name and namespace in other branches
  1. 8 src/Plugin/Block/AmpGoogleAdsenseBlock.php \Drupal\amp\Plugin\Block\AmpGoogleAdsenseBlock::build()

Builds and returns the renderable array for this block plugin.

If a block should not be rendered because it has no content, then this method must also ensure to return no content: it must then only return an empty array, or an empty array with #cache set (with cacheability metadata indicating the circumstances for it being empty).

Return value

array A renderable array representing the content of the block.

Overrides BlockPluginInterface::build

See also

\Drupal\block\BlockViewBuilder

File

src/Plugin/Block/AmpGoogleAdsenseBlock.php, line 20

Class

AmpGoogleAdsenseBlock

Namespace

Drupal\amp\Plugin\Block

Code

public function build() {

  // Start by getting global Adsense configuration.
  $amp_config = \Drupal::config('amp.settings');
  $adsense_id = $amp_config
    ->get('google_adsense_id');
  if (empty($adsense_id)) {
    return array(
      '#markup' => $this
        ->t('This block requires a Google Adsense ID.'),
    );
  }

  // Retrieve existing configuration for this block.
  $config = $this
    ->getConfiguration();
  $data_ad_slot = $config['data_ad_slot'];
  $height = $config['height'];
  $width = $config['width'];
  return [
    'inside' => [
      '#theme' => 'amp_ad',
      '#type' => 'adsense',
      '#attributes' => [
        'height' => $height,
        'width' => $width,
        'data-ad-client' => $adsense_id,
        'data-ad-slot' => $data_ad_slot,
      ],
    ],
  ];
}