Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

2757177 Support empty breakpoint ad sizes #9

Open
wants to merge 5 commits into
base: 8.x-1.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 15 additions & 4 deletions src/Form/BreakpointFormTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' => '<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'] != '<none>' && 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' => '<none>')
)
);
}
}
}
Expand Down Expand Up @@ -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' => '<none>')
);
}
}

Expand Down
9 changes: 8 additions & 1 deletion src/Tests/DisplayTagTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 "&lt;none&gt;").'), 'An error was correctly thrown when invalid characters.');

// Test tags with ad_size set to <none>.
$edit['breakpoints[0][browser_size]'] = '0x0';
$edit['breakpoints[0][ad_sizes]'] = '<none>';
$this->dfpEditTag($tag->id(), $edit);
$this->drupalGet('');
$this->assertRaw('addSize([0, 0], [])');
}

/**
Expand Down
11 changes: 9 additions & 2 deletions src/View/TagView.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 == '<none>') {
// If the ad sizes string contains the special keyword "<none>," 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) . ']';
Expand Down
1 change: 1 addition & 0 deletions tests/src/Unit/View/TagViewTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public function testFormatSize($size, $expected) {
*/
public function formatSizeProvider() {
return [
['<none>', '[]'],
['300x250 ', '[300, 250]'],
['300x250, 728x90 ', '[[300, 250], [728, 90]]'],
];
Expand Down