From 28b26661331907a7005f957d989b7d239e79aa9b Mon Sep 17 00:00:00 2001 From: Simon Engelhardt Date: Mon, 11 Apr 2016 13:18:03 -0400 Subject: [PATCH 1/8] Fix typo in variable name --- includes/admin.inc | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/includes/admin.inc b/includes/admin.inc index d4585b5..04c2696 100644 --- a/includes/admin.inc +++ b/includes/admin.inc @@ -113,13 +113,13 @@ function fb_instant_articles_module_config_ads($form) { ), ); - $form['ads']['fb_instant_articles_ads_dimension'] = array( + $form['ads']['fb_instant_articles_ads_dimensions'] = array( '#type' => 'select', '#title' => t('Ad Dimensions'), '#options' => array( 0 => t('Large (300 x 250)'), ), - '#default_value' => variable_get('fb_instant_articles_ads_dimension'), + '#default_value' => variable_get('fb_instant_articles_ads_dimensions'), '#states' => array( 'visible' => array( '[name=fb_instant_articles_ad_type]' => array('value' => FB_INSTANT_ARTICLES_AD_TYPE_EMBED_CODE), From b52ca3350f8076fe0dab4d49bad3f9ca09902803 Mon Sep 17 00:00:00 2001 From: Simon Engelhardt Date: Mon, 11 Apr 2016 13:46:53 -0400 Subject: [PATCH 2/8] Move ad type constants to shared enum-like class Note that this does not address the issue that we currently store the "human-readable" values and not the constant names in the database. This should be fixed to allow changing human-readable names without running a database upgrade script and also support translation. --- includes/admin.inc | 29 +++++++++++++---------------- src/AdTypes.php | 15 +++++++++++++++ 2 files changed, 28 insertions(+), 16 deletions(-) create mode 100644 src/AdTypes.php diff --git a/includes/admin.inc b/includes/admin.inc index 04c2696..36b9ba8 100644 --- a/includes/admin.inc +++ b/includes/admin.inc @@ -5,10 +5,7 @@ * Settings for Facebook Instant Articles Base module. */ -define('FB_INSTANT_ARTICLES_AD_TYPE_NONE', 'None'); -define('FB_INSTANT_ARTICLES_AD_TYPE_FBAN', 'Facebook Audience Network'); -define('FB_INSTANT_ARTICLES_AD_TYPE_SOURCE_URL', 'Source URL'); -define('FB_INSTANT_ARTICLES_AD_TYPE_EMBED_CODE', 'Embed Code'); + use Drupal\fb_instant_articles\AdTypes; /** * Form constructor for Facebook Instant Articles Base settings form. @@ -74,12 +71,12 @@ function fb_instant_articles_module_config_ads($form) { $form['ads']['fb_instant_articles_ad_type'] = array( '#type' => 'select', '#title' => t('Ad Type'), - '#default_value' => variable_get('fb_instant_articles_ad_type', FB_INSTANT_ARTICLES_AD_TYPE_NONE), + '#default_value' => variable_get('fb_instant_articles_ad_type', AdTypes::FB_INSTANT_ARTICLES_AD_TYPE_NONE), '#options' => array( - FB_INSTANT_ARTICLES_AD_TYPE_NONE => FB_INSTANT_ARTICLES_AD_TYPE_NONE, - FB_INSTANT_ARTICLES_AD_TYPE_FBAN => FB_INSTANT_ARTICLES_AD_TYPE_FBAN, - FB_INSTANT_ARTICLES_AD_TYPE_SOURCE_URL => FB_INSTANT_ARTICLES_AD_TYPE_SOURCE_URL, - FB_INSTANT_ARTICLES_AD_TYPE_EMBED_CODE => FB_INSTANT_ARTICLES_AD_TYPE_EMBED_CODE, + AdTypes::FB_INSTANT_ARTICLES_AD_TYPE_NONE => AdTypes::FB_INSTANT_ARTICLES_AD_TYPE_NONE, + AdTypes::FB_INSTANT_ARTICLES_AD_TYPE_FBAN => AdTypes::FB_INSTANT_ARTICLES_AD_TYPE_FBAN, + AdTypes::FB_INSTANT_ARTICLES_AD_TYPE_SOURCE_URL => AdTypes::FB_INSTANT_ARTICLES_AD_TYPE_SOURCE_URL, + AdTypes::FB_INSTANT_ARTICLES_AD_TYPE_EMBED_CODE => AdTypes::FB_INSTANT_ARTICLES_AD_TYPE_EMBED_CODE, ), '#description' => t('Note: this module will automatically place the ads within your articles.'), '#attributes' => array('class' => array('ad-type')), @@ -94,7 +91,7 @@ function fb_instant_articles_module_config_ads($form) { '#element_validate' => array('fb_instant_articles_validate_ad_source_url'), '#states' => array( 'visible' => array( - '[name=fb_instant_articles_ad_type]' => array('value' => FB_INSTANT_ARTICLES_AD_TYPE_SOURCE_URL), + '[name=fb_instant_articles_ad_type]' => array('value' => AdTypes::FB_INSTANT_ARTICLES_AD_TYPE_SOURCE_URL), ), ), ); @@ -108,7 +105,7 @@ function fb_instant_articles_module_config_ads($form) { '#element_validate' => array('fb_instant_articles_validate_an_placement_id'), '#states' => array( 'visible' => array( - '[name=fb_instant_articles_ad_type]' => array('value' => FB_INSTANT_ARTICLES_AD_TYPE_FBAN), + '[name=fb_instant_articles_ad_type]' => array('value' => AdTypes::FB_INSTANT_ARTICLES_AD_TYPE_FBAN), ), ), ); @@ -122,7 +119,7 @@ function fb_instant_articles_module_config_ads($form) { '#default_value' => variable_get('fb_instant_articles_ads_dimensions'), '#states' => array( 'visible' => array( - '[name=fb_instant_articles_ad_type]' => array('value' => FB_INSTANT_ARTICLES_AD_TYPE_EMBED_CODE), + '[name=fb_instant_articles_ad_type]' => array('value' => AdTypes::FB_INSTANT_ARTICLES_AD_TYPE_EMBED_CODE), ), ), ); @@ -136,7 +133,7 @@ function fb_instant_articles_module_config_ads($form) { '#element_validate' => array('fb_instant_articles_validate_ad_embed_code'), '#states' => array( 'visible' => array( - '[name=fb_instant_articles_ad_type]' => array('value' => FB_INSTANT_ARTICLES_AD_TYPE_EMBED_CODE), + '[name=fb_instant_articles_ad_type]' => array('value' => AdTypes::FB_INSTANT_ARTICLES_AD_TYPE_EMBED_CODE), ), ), ); @@ -180,7 +177,7 @@ function fb_instant_articles_validate_style($element, &$form_state, $form) { */ function fb_instant_articles_validate_an_placement_id($element, &$form_state, $form) { // Only validate if Audience Network is selected as ad type - if ($form_state['values']['fb_instant_articles_ad_type'] != FB_INSTANT_ARTICLES_AD_TYPE_FBAN) { + if ($form_state['values']['fb_instant_articles_ad_type'] != AdTypes::FB_INSTANT_ARTICLES_AD_TYPE_FBAN) { return; } @@ -198,7 +195,7 @@ function fb_instant_articles_validate_an_placement_id($element, &$form_state, $f */ function fb_instant_articles_validate_ad_source_url($element, &$form_state, $form) { // Only validate if Source URL is selected as ad type - if ($form_state['values']['fb_instant_articles_ad_type'] != FB_INSTANT_ARTICLES_AD_TYPE_SOURCE_URL) { + if ($form_state['values']['fb_instant_articles_ad_type'] != AdTypes::FB_INSTANT_ARTICLES_AD_TYPE_SOURCE_URL) { return; } @@ -216,7 +213,7 @@ function fb_instant_articles_validate_ad_source_url($element, &$form_state, $for */ function fb_instant_articles_validate_ad_embed_code($element, &$form_state, $form) { // Only validate if Embed Code is selected as ad type - if ($form_state['values']['fb_instant_articles_ad_type'] != FB_INSTANT_ARTICLES_AD_TYPE_EMBED_CODE) { + if ($form_state['values']['fb_instant_articles_ad_type'] != AdTypes::FB_INSTANT_ARTICLES_AD_TYPE_EMBED_CODE) { return; } diff --git a/src/AdTypes.php b/src/AdTypes.php new file mode 100644 index 0000000..d78b545 --- /dev/null +++ b/src/AdTypes.php @@ -0,0 +1,15 @@ + Date: Mon, 11 Apr 2016 13:50:01 -0400 Subject: [PATCH 3/8] Add support for ads Will include an automatically placed ad code of the specified type if configured in settings. --- .../src/DrupalInstantArticleDisplay.php | 72 ++++++++++++++++++- 1 file changed, 71 insertions(+), 1 deletion(-) diff --git a/modules/fb_instant_articles_display/src/DrupalInstantArticleDisplay.php b/modules/fb_instant_articles_display/src/DrupalInstantArticleDisplay.php index ff6fb57..90dc4ef 100644 --- a/modules/fb_instant_articles_display/src/DrupalInstantArticleDisplay.php +++ b/modules/fb_instant_articles_display/src/DrupalInstantArticleDisplay.php @@ -7,6 +7,7 @@ namespace Drupal\fb_instant_articles_display; +use Drupal\fb_instant_articles\AdTypes; use Facebook\InstantArticles\Elements\Ad; use Facebook\InstantArticles\Elements\Analytics; use Facebook\InstantArticles\Elements\Author; @@ -113,7 +114,9 @@ public static function create($node, $layoutSettings) { } $instantArticle->withHeader($header); - return new DrupalInstantArticleDisplay($node, $layoutSettings, $instantArticle); + $display = new DrupalInstantArticleDisplay($node, $layoutSettings, $instantArticle); + $display->addAdsFromSettings(); + return $display; } /** @@ -550,4 +553,71 @@ private function fieldFormatSocialElement($items, $body) { $body->addChild($social); } } + + /** + * Add ads if configured in settings + */ + public function addAdsFromSettings() { + $ad_type = variable_get('fb_instant_articles_ad_type', AdTypes::FB_INSTANT_ARTICLES_AD_TYPE_NONE); + if ($ad_type === AdTypes::FB_INSTANT_ARTICLES_AD_TYPE_NONE) { + return; + } + $dimensions = variable_get('fb_instant_articles_ads_dimension', AdTypes::FB_INSTANT_ARTICLES_AD_TYPE_NONE); + $width = 300; + $height = 250; + switch ($dimensions) { + case 0: + $width = 300; + $height = 250; + break; + default: + throw new Exception('Configured ad dimensions are not supported'); + } + $ad = Ad::create() + ->enableDefaultForReuse() + ->withWidth($width) + ->withHeight($height); + $header = $this->instantArticle->getHeader(); + switch ($ad_type) { + case AdTypes::FB_INSTANT_ARTICLES_AD_TYPE_FBAN: + $an_placement_id = variable_get('fb_instant_articles_ads_an_placement_id'); + if ($an_placement_id) { + $ad->withSource( + 'https://www.facebook.com/adnw_request?' . + drupal_http_build_query( + array( + 'placement' => $an_placement_id, + 'adtype' => 'banner' . $width . 'x' . $height, + ) + ) + ); + $header->addAd($ad); + } + break; + case AdTypes::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 AdTypes::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(); + } } From e059193a3614c6662324aec1b3ddd0bf49a64487 Mon Sep 17 00:00:00 2001 From: Simon Engelhardt Date: Mon, 11 Apr 2016 23:24:26 -0700 Subject: [PATCH 4/8] Rework ad type constants Addresses the issue of storing a descriptive name in the database, as mentioned in b52ca3350f8076fe0dab4d49bad3f9ca09902803 --- fb_instant_articles.module | 17 +++++++++++++ includes/admin.inc | 25 +++++++------------ .../src/DrupalInstantArticleDisplay.php | 13 +++++----- src/AdTypes.php | 15 ----------- 4 files changed, 32 insertions(+), 38 deletions(-) delete mode 100644 src/AdTypes.php diff --git a/fb_instant_articles.module b/fb_instant_articles.module index b9e2439..d51128b 100644 --- a/fb_instant_articles.module +++ b/fb_instant_articles.module @@ -5,6 +5,23 @@ * Hook implementations for Facebook Instant Articles Base module. */ +define('FB_INSTANT_ARTICLES_AD_TYPE_NONE', 'none'); +define('FB_INSTANT_ARTICLES_AD_TYPE_FBAN', 'fban'); +define('FB_INSTANT_ARTICLES_AD_TYPE_SOURCE_URL', 'source_url'); +define('FB_INSTANT_ARTICLES_AD_TYPE_EMBED_CODE', 'embed_code'); + +/** + * Get an array of possible ad types and their descriptive names + **/ +function fb_instant_articles_get_ad_types() { + return array( + FB_INSTANT_ARTICLES_AD_TYPE_NONE => t('None'), + FB_INSTANT_ARTICLES_AD_TYPE_FBAN => t('Facebook Audience Network'), + FB_INSTANT_ARTICLES_AD_TYPE_SOURCE_URL => t('Source URL'), + FB_INSTANT_ARTICLES_AD_TYPE_EMBED_CODE => t('Embed Code'), + ); +} + /** * Implements hook_menu(). */ diff --git a/includes/admin.inc b/includes/admin.inc index 36b9ba8..b79a0f1 100644 --- a/includes/admin.inc +++ b/includes/admin.inc @@ -5,8 +5,6 @@ * Settings for Facebook Instant Articles Base module. */ - use Drupal\fb_instant_articles\AdTypes; - /** * Form constructor for Facebook Instant Articles Base settings form. */ @@ -71,13 +69,8 @@ function fb_instant_articles_module_config_ads($form) { $form['ads']['fb_instant_articles_ad_type'] = array( '#type' => 'select', '#title' => t('Ad Type'), - '#default_value' => variable_get('fb_instant_articles_ad_type', AdTypes::FB_INSTANT_ARTICLES_AD_TYPE_NONE), - '#options' => array( - AdTypes::FB_INSTANT_ARTICLES_AD_TYPE_NONE => AdTypes::FB_INSTANT_ARTICLES_AD_TYPE_NONE, - AdTypes::FB_INSTANT_ARTICLES_AD_TYPE_FBAN => AdTypes::FB_INSTANT_ARTICLES_AD_TYPE_FBAN, - AdTypes::FB_INSTANT_ARTICLES_AD_TYPE_SOURCE_URL => AdTypes::FB_INSTANT_ARTICLES_AD_TYPE_SOURCE_URL, - AdTypes::FB_INSTANT_ARTICLES_AD_TYPE_EMBED_CODE => AdTypes::FB_INSTANT_ARTICLES_AD_TYPE_EMBED_CODE, - ), + '#default_value' => variable_get('fb_instant_articles_ad_type', FB_INSTANT_ARTICLES_AD_TYPE_NONE), + '#options' => fb_instant_articles_get_ad_types(), '#description' => t('Note: this module will automatically place the ads within your articles.'), '#attributes' => array('class' => array('ad-type')), ); @@ -91,7 +84,7 @@ function fb_instant_articles_module_config_ads($form) { '#element_validate' => array('fb_instant_articles_validate_ad_source_url'), '#states' => array( 'visible' => array( - '[name=fb_instant_articles_ad_type]' => array('value' => AdTypes::FB_INSTANT_ARTICLES_AD_TYPE_SOURCE_URL), + '[name=fb_instant_articles_ad_type]' => array('value' => FB_INSTANT_ARTICLES_AD_TYPE_SOURCE_URL), ), ), ); @@ -105,7 +98,7 @@ function fb_instant_articles_module_config_ads($form) { '#element_validate' => array('fb_instant_articles_validate_an_placement_id'), '#states' => array( 'visible' => array( - '[name=fb_instant_articles_ad_type]' => array('value' => AdTypes::FB_INSTANT_ARTICLES_AD_TYPE_FBAN), + '[name=fb_instant_articles_ad_type]' => array('value' => FB_INSTANT_ARTICLES_AD_TYPE_FBAN), ), ), ); @@ -119,7 +112,7 @@ function fb_instant_articles_module_config_ads($form) { '#default_value' => variable_get('fb_instant_articles_ads_dimensions'), '#states' => array( 'visible' => array( - '[name=fb_instant_articles_ad_type]' => array('value' => AdTypes::FB_INSTANT_ARTICLES_AD_TYPE_EMBED_CODE), + '[name=fb_instant_articles_ad_type]' => array('value' => FB_INSTANT_ARTICLES_AD_TYPE_EMBED_CODE), ), ), ); @@ -133,7 +126,7 @@ function fb_instant_articles_module_config_ads($form) { '#element_validate' => array('fb_instant_articles_validate_ad_embed_code'), '#states' => array( 'visible' => array( - '[name=fb_instant_articles_ad_type]' => array('value' => AdTypes::FB_INSTANT_ARTICLES_AD_TYPE_EMBED_CODE), + '[name=fb_instant_articles_ad_type]' => array('value' => FB_INSTANT_ARTICLES_AD_TYPE_EMBED_CODE), ), ), ); @@ -177,7 +170,7 @@ function fb_instant_articles_validate_style($element, &$form_state, $form) { */ function fb_instant_articles_validate_an_placement_id($element, &$form_state, $form) { // Only validate if Audience Network is selected as ad type - if ($form_state['values']['fb_instant_articles_ad_type'] != AdTypes::FB_INSTANT_ARTICLES_AD_TYPE_FBAN) { + if ($form_state['values']['fb_instant_articles_ad_type'] != FB_INSTANT_ARTICLES_AD_TYPE_FBAN) { return; } @@ -195,7 +188,7 @@ function fb_instant_articles_validate_an_placement_id($element, &$form_state, $f */ function fb_instant_articles_validate_ad_source_url($element, &$form_state, $form) { // Only validate if Source URL is selected as ad type - if ($form_state['values']['fb_instant_articles_ad_type'] != AdTypes::FB_INSTANT_ARTICLES_AD_TYPE_SOURCE_URL) { + if ($form_state['values']['fb_instant_articles_ad_type'] != FB_INSTANT_ARTICLES_AD_TYPE_SOURCE_URL) { return; } @@ -213,7 +206,7 @@ function fb_instant_articles_validate_ad_source_url($element, &$form_state, $for */ function fb_instant_articles_validate_ad_embed_code($element, &$form_state, $form) { // Only validate if Embed Code is selected as ad type - if ($form_state['values']['fb_instant_articles_ad_type'] != AdTypes::FB_INSTANT_ARTICLES_AD_TYPE_EMBED_CODE) { + if ($form_state['values']['fb_instant_articles_ad_type'] != FB_INSTANT_ARTICLES_AD_TYPE_EMBED_CODE) { return; } diff --git a/modules/fb_instant_articles_display/src/DrupalInstantArticleDisplay.php b/modules/fb_instant_articles_display/src/DrupalInstantArticleDisplay.php index 90dc4ef..e7e23fd 100644 --- a/modules/fb_instant_articles_display/src/DrupalInstantArticleDisplay.php +++ b/modules/fb_instant_articles_display/src/DrupalInstantArticleDisplay.php @@ -7,7 +7,6 @@ namespace Drupal\fb_instant_articles_display; -use Drupal\fb_instant_articles\AdTypes; use Facebook\InstantArticles\Elements\Ad; use Facebook\InstantArticles\Elements\Analytics; use Facebook\InstantArticles\Elements\Author; @@ -558,11 +557,11 @@ private function fieldFormatSocialElement($items, $body) { * Add ads if configured in settings */ public function addAdsFromSettings() { - $ad_type = variable_get('fb_instant_articles_ad_type', AdTypes::FB_INSTANT_ARTICLES_AD_TYPE_NONE); - if ($ad_type === AdTypes::FB_INSTANT_ARTICLES_AD_TYPE_NONE) { + $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; } - $dimensions = variable_get('fb_instant_articles_ads_dimension', AdTypes::FB_INSTANT_ARTICLES_AD_TYPE_NONE); + $dimensions = variable_get('fb_instant_articles_ads_dimension', FB_INSTANT_ARTICLES_AD_TYPE_NONE); $width = 300; $height = 250; switch ($dimensions) { @@ -579,7 +578,7 @@ public function addAdsFromSettings() { ->withHeight($height); $header = $this->instantArticle->getHeader(); switch ($ad_type) { - case AdTypes::FB_INSTANT_ARTICLES_AD_TYPE_FBAN: + 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( @@ -594,7 +593,7 @@ public function addAdsFromSettings() { $header->addAd($ad); } break; - case AdTypes::FB_INSTANT_ARTICLES_AD_TYPE_SOURCE_URL: + case FB_INSTANT_ARTICLES_AD_TYPE_SOURCE_URL: $iframe_url = variable_get('fb_instant_articles_ads_iframe_url'); if ($iframe_url) { $ad->withSource( @@ -603,7 +602,7 @@ public function addAdsFromSettings() { $header->addAd($ad); } break; - case AdTypes::FB_INSTANT_ARTICLES_AD_TYPE_EMBED_CODE: + case FB_INSTANT_ARTICLES_AD_TYPE_EMBED_CODE: $embed_code = variable_get('fb_instant_articles_ads_embed_code'); if ($embed_code) { $document = new \DOMDocument(); diff --git a/src/AdTypes.php b/src/AdTypes.php deleted file mode 100644 index d78b545..0000000 --- a/src/AdTypes.php +++ /dev/null @@ -1,15 +0,0 @@ - Date: Mon, 11 Apr 2016 23:45:44 -0700 Subject: [PATCH 5/8] Rework ad dimensions based on feedback in #36 - Ad dimensions are stored as string following a {width}x{height} convention - Variable is now named fb_instant_articles_ads_dimensions everywhere - Variable value is now deleted on module uninstall --- fb_instant_articles.install | 2 +- includes/admin.inc | 2 +- .../src/DrupalInstantArticleDisplay.php | 13 +++++-------- 3 files changed, 7 insertions(+), 10 deletions(-) diff --git a/fb_instant_articles.install b/fb_instant_articles.install index 966f859..b169cfa 100644 --- a/fb_instant_articles.install +++ b/fb_instant_articles.install @@ -14,7 +14,7 @@ function fb_instant_articles_uninstall() { variable_del('fb_instant_articles_ad_type'); variable_del('fb_instant_articles_ads_iframe_url'); variable_del('fb_instant_articles_ads_an_placement_id'); - variable_del('fb_instant_articles_ads_dimension'); + variable_del('fb_instant_articles_ads_dimensions'); variable_del('fb_instant_articles_ads_embed_code'); variable_del('fb_instant_articles_analytics_embed_code'); variable_del('fb_instant_articles_enable_logging'); diff --git a/includes/admin.inc b/includes/admin.inc index b79a0f1..8cf631b 100644 --- a/includes/admin.inc +++ b/includes/admin.inc @@ -107,7 +107,7 @@ function fb_instant_articles_module_config_ads($form) { '#type' => 'select', '#title' => t('Ad Dimensions'), '#options' => array( - 0 => t('Large (300 x 250)'), + '300x250' => t('Large (300 x 250)'), ), '#default_value' => variable_get('fb_instant_articles_ads_dimensions'), '#states' => array( diff --git a/modules/fb_instant_articles_display/src/DrupalInstantArticleDisplay.php b/modules/fb_instant_articles_display/src/DrupalInstantArticleDisplay.php index e7e23fd..903a045 100644 --- a/modules/fb_instant_articles_display/src/DrupalInstantArticleDisplay.php +++ b/modules/fb_instant_articles_display/src/DrupalInstantArticleDisplay.php @@ -561,16 +561,13 @@ public function addAdsFromSettings() { if ($ad_type === FB_INSTANT_ARTICLES_AD_TYPE_NONE) { return; } - $dimensions = variable_get('fb_instant_articles_ads_dimension', FB_INSTANT_ARTICLES_AD_TYPE_NONE); $width = 300; $height = 250; - switch ($dimensions) { - case 0: - $width = 300; - $height = 250; - break; - default: - throw new Exception('Configured ad dimensions are not supported'); + $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() From a14ade7441cb1261533ef253245960a63e4eef57 Mon Sep 17 00:00:00 2001 From: Simon Engelhardt Date: Mon, 11 Apr 2016 23:51:16 -0700 Subject: [PATCH 6/8] More Drupal-y URL building --- .../src/DrupalInstantArticleDisplay.php | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/modules/fb_instant_articles_display/src/DrupalInstantArticleDisplay.php b/modules/fb_instant_articles_display/src/DrupalInstantArticleDisplay.php index 903a045..3b13361 100644 --- a/modules/fb_instant_articles_display/src/DrupalInstantArticleDisplay.php +++ b/modules/fb_instant_articles_display/src/DrupalInstantArticleDisplay.php @@ -579,13 +579,12 @@ public function addAdsFromSettings() { $an_placement_id = variable_get('fb_instant_articles_ads_an_placement_id'); if ($an_placement_id) { $ad->withSource( - 'https://www.facebook.com/adnw_request?' . - drupal_http_build_query( - array( - 'placement' => $an_placement_id, + url('https://www.facebook.com/adnw_request', array( + 'query' => array( + 'placement' => $ad_placement_id, 'adtype' => 'banner' . $width . 'x' . $height, - ) - ) + ), + )) ); $header->addAd($ad); } From e12aabf49a1db0178e94bbe0f6ecd7e499ce8f61 Mon Sep 17 00:00:00 2001 From: Simon Engelhardt Date: Mon, 11 Apr 2016 23:54:52 -0700 Subject: [PATCH 7/8] Remove conditional visibility of ad dimensions Ad dimensions apply to all ad types --- includes/admin.inc | 5 ----- 1 file changed, 5 deletions(-) diff --git a/includes/admin.inc b/includes/admin.inc index 8cf631b..8fd25cb 100644 --- a/includes/admin.inc +++ b/includes/admin.inc @@ -110,11 +110,6 @@ function fb_instant_articles_module_config_ads($form) { '300x250' => t('Large (300 x 250)'), ), '#default_value' => variable_get('fb_instant_articles_ads_dimensions'), - '#states' => array( - 'visible' => array( - '[name=fb_instant_articles_ad_type]' => array('value' => FB_INSTANT_ARTICLES_AD_TYPE_EMBED_CODE), - ), - ), ); $form['ads']['fb_instant_articles_ads_embed_code'] = array( From 1e0938db9ac898650bcb1f20040e9a0503d57a67 Mon Sep 17 00:00:00 2001 From: Simon Engelhardt Date: Mon, 11 Apr 2016 23:56:02 -0700 Subject: [PATCH 8/8] Move ad dimensions to bottom --- includes/admin.inc | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/includes/admin.inc b/includes/admin.inc index 8fd25cb..6a30960 100644 --- a/includes/admin.inc +++ b/includes/admin.inc @@ -103,15 +103,6 @@ function fb_instant_articles_module_config_ads($form) { ), ); - $form['ads']['fb_instant_articles_ads_dimensions'] = array( - '#type' => 'select', - '#title' => t('Ad Dimensions'), - '#options' => array( - '300x250' => t('Large (300 x 250)'), - ), - '#default_value' => variable_get('fb_instant_articles_ads_dimensions'), - ); - $form['ads']['fb_instant_articles_ads_embed_code'] = array( '#type' => 'textarea', '#title' => t('Ad Embed Code'), @@ -126,6 +117,15 @@ function fb_instant_articles_module_config_ads($form) { ), ); + $form['ads']['fb_instant_articles_ads_dimensions'] = array( + '#type' => 'select', + '#title' => t('Ad Dimensions'), + '#options' => array( + '300x250' => t('Large (300 x 250)'), + ), + '#default_value' => variable_get('fb_instant_articles_ads_dimensions'), + ); + return $form; }