Render form block custom classes on frontend

[MAILPOET-2746]
This commit is contained in:
Rostislav Wolny
2020-03-12 16:27:59 +01:00
committed by Veljko V
parent 72a0afa75b
commit 060a6839c7
7 changed files with 43 additions and 11 deletions

View File

@ -3,7 +3,8 @@
namespace MailPoet\Form\Block; namespace MailPoet\Form\Block;
class Divider { class Divider {
public function render(): string { public function render($block): string {
return '<hr class="mailpoet_divider" />'; $classes = isset($block['params']['class_name']) ? " " . $block['params']['class_name'] : '';
return '<hr class="mailpoet_divider' . $classes . '" />';
} }
} }

View File

@ -22,7 +22,8 @@ class Html {
$text = nl2br($text); $text = nl2br($text);
} }
$html .= '<div class="mailpoet_paragraph" ' . $this->rendererHelper->renderFontStyle($formSettings) . '>'; $classes = isset($block['params']['class_name']) ? " " . $block['params']['class_name'] : '';
$html .= '<div class="mailpoet_paragraph' . $classes . '" ' . $this->rendererHelper->renderFontStyle($formSettings) . '>';
$html .= $text; $html .= $text;
$html .= '</div>'; $html .= '</div>';

View File

@ -2,10 +2,9 @@
namespace MailPoet\Form; namespace MailPoet\Form;
use MailPoet\Models\Form;
class BlockWrapperRenderer { class BlockWrapperRenderer {
public function render(array $block, string $blockContent): string { public function render(array $block, string $blockContent): string {
return '<div class="mailpoet_paragraph">' . $blockContent . '</div>'; $classes = isset($block['params']['class_name']) ? " " . $block['params']['class_name'] : '';
return '<div class="mailpoet_paragraph' . $classes . '">' . $blockContent . '</div>';
} }
} }

View File

@ -98,7 +98,7 @@ class BlocksRenderer {
break; break;
case 'divider': case 'divider':
$html .= $this->divider->render(); $html .= $this->divider->render($block);
break; break;
case 'checkbox': case 'checkbox':

View File

@ -8,13 +8,29 @@ class DividerTest extends \MailPoetUnitTest {
/** @var Divider */ /** @var Divider */
private $divider; private $divider;
private $block = [
'type' => 'divider',
'name' => 'Divider',
'id' => 'divider',
'unique' => '1',
'static' => '0',
'params' => [],
'position' => '1',
];
public function _before() { public function _before() {
parent::_before(); parent::_before();
$this->divider = new Divider(); $this->divider = new Divider();
} }
public function testItShouldRenderDivider() { public function testItShouldRenderDivider() {
$html = $this->divider->render(); $html = $this->divider->render($this->block);
expect($html)->equals('<hr class="mailpoet_divider" />'); expect($html)->equals('<hr class="mailpoet_divider" />');
} }
public function testItShouldRenderCustomClass() {
$this->block['params']['class_name'] = 'my_class';
$html = $this->divider->render($this->block);
expect($html)->equals('<hr class="mailpoet_divider my_class" />');
}
} }

View File

@ -10,9 +10,9 @@ class HtmlTest extends \MailPoetUnitTest {
private $html; private $html;
private $block = [ private $block = [
'type' => 'divider', 'type' => 'html',
'name' => 'Divider', 'name' => 'Html',
'id' => 'divider', 'id' => 'html',
'unique' => '1', 'unique' => '1',
'static' => '0', 'static' => '0',
'params' => [ 'params' => [
@ -32,6 +32,13 @@ class HtmlTest extends \MailPoetUnitTest {
expect($html)->equals("<div class=\"mailpoet_paragraph\" >line1<br />\nline2</div>"); expect($html)->equals("<div class=\"mailpoet_paragraph\" >line1<br />\nline2</div>");
} }
public function testItShouldRenderCustomClass() {
$block = $this->block;
$block['params']['class_name'] = 'my_class';
$html = $this->html->render($block, []);
expect($html)->equals("<div class=\"mailpoet_paragraph my_class\" >line1<br />\nline2</div>");
}
public function testItShouldRenderCustomHtmlWithoutAutomaticBrs() { public function testItShouldRenderCustomHtmlWithoutAutomaticBrs() {
$block = $this->block; $block = $this->block;
$block['params']['nl2br'] = ''; $block['params']['nl2br'] = '';

View File

@ -12,4 +12,12 @@ class BlockWrapperRendererTest extends \MailPoetUnitTest {
$result = $renderer->render($block, 'content'); $result = $renderer->render($block, 'content');
expect($result)->equals('<div class="mailpoet_paragraph">content</div>'); expect($result)->equals('<div class="mailpoet_paragraph">content</div>');
} }
public function testItShouldWrapRenderCustomClasses() {
$renderer = new BlockWrapperRenderer();
$block = Fixtures::get('simple_form_body')[0];
$block['params']['class_name'] = 'class1 class2';
$result = $renderer->render($block, 'content');
expect($result)->equals('<div class="mailpoet_paragraph class1 class2">content</div>');
}
} }