public function DrupalInstantArticleDisplay::addAdsFromSettings in Facebook Instant Articles 7
Add ads if configured in settings
File
- modules/
fb_instant_articles_display/ src/ DrupalInstantArticleDisplay.php, line 611 - Contains \Drupal\fb_instant_articles_display\DrupalInstantArticleDisplay.
Class
- DrupalInstantArticleDisplay
- Facebook Instant Article node wrapper class. Builds up an InstantArticle object using field formatters.
Namespace
Drupal\fb_instant_articles_displayCode
public function addAdsFromSettings() {
$ad_type = variable_get('fb_instant_articles_ad_type', FB_INSTANT_ARTICLES_AD_TYPE_NONE);
if ($ad_type === FB_INSTANT_ARTICLES_AD_TYPE_NONE) {
return;
}
$width = 300;
$height = 250;
$dimensions_match = array();
$dimensions_raw = variable_get('fb_instant_articles_ads_dimensions');
if (preg_match('/^(?:\\s)*(\\d+)x(\\d+)(?:\\s)*$/', $dimensions_raw, $dimensions_match)) {
$width = intval($dimensions_match[1]);
$height = intval($dimensions_match[2]);
}
$ad = Ad::create()
->enableDefaultForReuse()
->withWidth($width)
->withHeight($height);
$header = $this->instantArticle
->getHeader();
switch ($ad_type) {
case FB_INSTANT_ARTICLES_AD_TYPE_FBAN:
$an_placement_id = variable_get('fb_instant_articles_ads_an_placement_id');
if ($an_placement_id) {
$ad
->withSource(url('https://www.facebook.com/adnw_request', array(
'query' => array(
'placement' => $an_placement_id,
'adtype' => 'banner' . $width . 'x' . $height,
),
)));
$header
->addAd($ad);
}
break;
case FB_INSTANT_ARTICLES_AD_TYPE_SOURCE_URL:
$iframe_url = variable_get('fb_instant_articles_ads_iframe_url');
if ($iframe_url) {
$ad
->withSource($iframe_url);
$header
->addAd($ad);
}
break;
case FB_INSTANT_ARTICLES_AD_TYPE_EMBED_CODE:
$embed_code = variable_get('fb_instant_articles_ads_embed_code');
if ($embed_code) {
$document = new \DOMDocument();
$fragment = $document
->createDocumentFragment();
$valid_html = @$fragment
->appendXML($embed_code);
if ($valid_html) {
$ad
->withHTML($fragment);
$header
->addAd($ad);
}
}
break;
}
$this->instantArticle
->enableAutomaticAdPlacement();
}