protected function InstantArticleContentEntityNormalizer::adsFromSettings in Facebook Instant Articles 8.2
Same name and namespace in other branches
- 3.x src/Normalizer/InstantArticleContentEntityNormalizer.php \Drupal\fb_instant_articles\Normalizer\InstantArticleContentEntityNormalizer::adsFromSettings()
Add ads if configured in settings to the instant article normalize result.
Parameters
\Facebook\InstantArticles\Elements\InstantArticle $article: Instant article object we are normalizing to.
Return value
\Facebook\InstantArticles\Elements\InstantArticle Modified instant article.
1 call to InstantArticleContentEntityNormalizer::adsFromSettings()
File
- src/
Normalizer/ InstantArticleContentEntityNormalizer.php, line 301
Class
- InstantArticleContentEntityNormalizer
- Facebook Instant Articles content entity normalizer.
Namespace
Drupal\fb_instant_articles\NormalizerCode
protected function adsFromSettings(InstantArticle $article) {
$ads_type = $this->config
->get('ads.type');
if (!$ads_type || $ads_type === AdTypes::AD_TYPE_NONE) {
return $article;
}
$width = 300;
$height = 250;
$dimensions_match = [];
$dimensions_raw = $this->config
->get('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 = $article
->getHeader();
if (!$header) {
$header = Header::create();
$article
->withHeader($header);
}
switch ($ads_type) {
case AdTypes::AD_TYPE_FBAN:
$an_placement_id = $this->config
->get('ads.an_placement_id');
if ($an_placement_id) {
$ad
->withSource(Url::fromUri('https://www.facebook.com/adnw_request', [
'query' => [
'placement' => $an_placement_id,
'adtype' => 'banner' . $width . 'x' . $height,
],
])
->toString());
$header
->addAd($ad);
}
break;
case AdTypes::AD_TYPE_SOURCE_URL:
$iframe_url = $this->config
->get('ads.iframe_url');
if ($iframe_url) {
$ad
->withSource($iframe_url);
$header
->addAd($ad);
}
break;
case AdTypes::AD_TYPE_EMBED_CODE:
$embed_code = $this->config
->get('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;
}
$article
->enableAutomaticAdPlacement();
return $article;
}