diff --git a/src/Form/BreakpointFormTrait.php b/src/Form/BreakpointFormTrait.php index 4b0bbfc..631da76 100644 --- a/src/Form/BreakpointFormTrait.php +++ b/src/Form/BreakpointFormTrait.php @@ -22,14 +22,22 @@ public static function breakpointFormValidate(array $element, FormStateInterface $form_state->setError($element['browser_size'], t('The browser size cannot be empty if ad size(s) exists.')); } elseif (!empty($element['browser_size']['#value']) && empty($element['ad_sizes']['#value'])) { - $form_state->setError($element['ad_sizes'], t('The ad size(s) cannot be empty if a browser size exists.')); + $form_state->setError($element['ad_sizes'], t( + 'The ad size(s) cannot be empty if a browser size exists. If you wish to suppress an ad slot for a given browser size, you can enter "@none" in the ad size(s) field.', + array('@none' => '') + ) + ); } if (!empty($element['browser_size']['#value']) && !empty($element['ad_sizes']['#value'])) { if (preg_match('/[^x|0-9]/', $element['browser_size']['#value'])) { $form_state->setError($element['browser_size'], t('The browser size can only contain numbers and the character x.')); } - elseif (preg_match('/[^x|,|0-9]/', $element['ad_sizes']['#value'])) { - $form_state->setError($element['ad_sizes'], t('The ad size(s) can only contain numbers, the character x and commas.')); + elseif ($element['ad_sizes']['#value'] != '' && preg_match('/[^x|,|0-9]/', $element['ad_sizes']['#value'])) { + $form_state->setError($element['ad_sizes'], t( + 'The ad size(s) string can only contain numbers, the character x and commas (unless it is the special keyword "@none").', + array('@none' => '') + ) + ); } } } @@ -158,7 +166,10 @@ protected function addBreakpointForm(array &$form, $key, array $data = []) { ]; if (empty($data)) { $form['breakpoints']['table'][$key]['browser_size']['#description'] = $this->t('Example: 1024x768'); - $form['breakpoints']['table'][$key]['ad_sizes']['#description'] = $this->t('Example: 300x600,300x250'); + $form['breakpoints']['table'][$key]['ad_sizes']['#description'] = $this->t( + 'Example: 300x600, 300x250. Enter "@none" to suppress this slot for a given browser size.', + array('@none' => '') + ); } } diff --git a/src/Tests/DisplayTagTest.php b/src/Tests/DisplayTagTest.php index 261359f..9d4ff33 100644 --- a/src/Tests/DisplayTagTest.php +++ b/src/Tests/DisplayTagTest.php @@ -69,7 +69,14 @@ public function testDisplayTagWithMapping() { $edit['breakpoints[0][browser_size]'] = $this->dfpGenerateSize(); $edit['breakpoints[0][ad_sizes]'] = '100y100,200x200'; $this->dfpEditTag($tag->id(), $edit); - $this->assertText(t('The ad size(s) can only contain numbers, the character x and commas.'), 'An error was correctly thrown when invalid characters.'); + $this->assertText(t('The ad size(s) string can only contain numbers, the character x and commas (unless it is the special keyword "<none>").'), 'An error was correctly thrown when invalid characters.'); + + // Test tags with ad_size set to . + $edit['breakpoints[0][browser_size]'] = '0x0'; + $edit['breakpoints[0][ad_sizes]'] = ''; + $this->dfpEditTag($tag->id(), $edit); + $this->drupalGet(''); + $this->assertRaw('addSize([0, 0], [])'); } /** diff --git a/src/View/TagView.php b/src/View/TagView.php index 9b4cc10..b8ccd42 100644 --- a/src/View/TagView.php +++ b/src/View/TagView.php @@ -329,8 +329,15 @@ public static function formatSize($size) { $sizes = explode(',', $size); foreach ($sizes as $size) { - $formatted_size = explode('x', trim($size)); - $formatted_sizes[] = '[' . implode(', ', $formatted_size) . ']'; + if ($size == '') { + // If the ad sizes string contains the special keyword "," use an + // empty size list in order to suppress slot display. + $formatted_sizes[] = '[]'; + } + else { + $formatted_size = explode('x', trim($size)); + $formatted_sizes[] = '[' . implode(', ', $formatted_size) . ']'; + } } return count($formatted_sizes) == 1 ? $formatted_sizes[0] : '[' . implode(', ', $formatted_sizes) . ']'; diff --git a/tests/src/Unit/View/TagViewTest.php b/tests/src/Unit/View/TagViewTest.php index 95f644f..9d402d5 100644 --- a/tests/src/Unit/View/TagViewTest.php +++ b/tests/src/Unit/View/TagViewTest.php @@ -32,6 +32,7 @@ public function testFormatSize($size, $expected) { */ public function formatSizeProvider() { return [ + ['', '[]'], ['300x250 ', '[300, 250]'], ['300x250, 728x90 ', '[[300, 250], [728, 90]]'], ];