Add label to text form

[MAILPOET-3331]
This commit is contained in:
Brezo Cordero
2022-05-31 15:18:04 +01:00
committed by Veljko V
parent 12d641505f
commit 6ad442a5dc
3 changed files with 30 additions and 9 deletions

View File

@@ -104,28 +104,39 @@ class BlockRendererHelper {
public function renderLabel(array $block, array $formSettings): string { public function renderLabel(array $block, array $formSettings): string {
$html = ''; $html = '';
$forId = '';
if ( if (
isset($block['params']['hide_label']) isset($block['params']['hide_label'])
&& $block['params']['hide_label'] && $block['params']['hide_label']
) { ) {
return $html; return $html;
} }
if (
isset($block['params']['label_within'])
&& $block['params']['label_within']
) {
return $html;
}
$automationId = null; $automationId = null;
if (in_array($block['id'], ['email', 'last_name', 'first_name'], true)) { if (in_array($block['id'], ['email', 'last_name', 'first_name'], true)) {
$automationId = 'data-automation-id="form_' . $block['id'] . '_label" '; $automationId = 'data-automation-id="form_' . $block['id'] . '_label" ';
if (isset($formSettings['id'])) {
$forId = 'for="form_' . $block['id'] . '_' . $formSettings['id'] . '" ';
} }
}
if ( if (
isset($block['params']['label']) isset($block['params']['label'])
&& strlen(trim($block['params']['label'])) > 0 && strlen(trim($block['params']['label'])) > 0
) { ) {
$labelClass = 'class="mailpoet_' . $block['type'] . '_label" ';
if (
isset($block['params']['label_within'])
&& $block['params']['label_within']
) {
$labelClass = 'class="screen-reader-text" ';
}
$html .= '<label ' $html .= '<label '
. 'class="mailpoet_' . $block['type'] . '_label" ' . $forId
. $labelClass
. $this->renderFontStyle($formSettings, $block['styles'] ?? []) . $this->renderFontStyle($formSettings, $block['styles'] ?? [])
. ($automationId ?? '') . ($automationId ?? '')
. '>'; . '>';

View File

@@ -34,6 +34,7 @@ class Text {
public function render(array $block, array $formSettings): string { public function render(array $block, array $formSettings): string {
$type = 'text'; $type = 'text';
$automationId = ' '; $automationId = ' ';
$id = '';
$autocomplete = 'on'; $autocomplete = 'on';
if ($block['id'] === 'email') { if ($block['id'] === 'email') {
$type = 'email'; $type = 'email';
@@ -41,20 +42,26 @@ class Text {
} }
if (in_array($block['id'], ['email', 'last_name', 'first_name'], true)) { if (in_array($block['id'], ['email', 'last_name', 'first_name'], true)) {
$automationId = 'data-automation-id="form_' . $this->wp->escAttr($block['id']) . '" '; $automationId = 'data-automation-id="form_' . $this->wp->escAttr($block['id']) . '" ';
if (isset($formSettings['id'])) {
$id = 'id="form_' . $this->wp->escAttr($block['id']) . '_' . $this->wp->escAttr($formSettings['id']) . '" ';
}
} }
$styles = $this->inputStylesRenderer->renderForTextInput($block['styles'] ?? [], $formSettings); $styles = $this->inputStylesRenderer->renderForTextInput($block['styles'] ?? [], $formSettings);
$name = $this->rendererHelper->getFieldName($block); $name = $this->rendererHelper->getFieldName($block);
$html = ''; $html = $this->inputStylesRenderer->renderPlaceholderStyles($block, 'input[name="data[' . $name . ']"]');
$html .= $this->inputStylesRenderer->renderPlaceholderStyles($block, 'input[name="data[' . $name . ']"]');
$html .= $this->rendererHelper->renderLabel($block, $formSettings); $html .= $this->rendererHelper->renderLabel($block, $formSettings);
$html .= '<input type="' . $type . '" autocomplete="' . $autocomplete . '" class="mailpoet_text" '; $html .= '<input type="' . $type . '" autocomplete="' . $autocomplete . '" class="mailpoet_text" ';
$html .= $id;
$html .= 'name="data[' . $name . ']" '; $html .= 'name="data[' . $name . ']" ';
$html .= 'title="' . $this->rendererHelper->getFieldLabel($block) . '" '; $html .= 'title="' . $this->rendererHelper->getFieldLabel($block) . '" ';

View File

@@ -101,6 +101,9 @@ class BlocksRenderer {
public function renderBlock(array $block, array $formSettings, ?int $formId): string { public function renderBlock(array $block, array $formSettings, ?int $formId): string {
$html = ''; $html = '';
if ($formId) {
$formSettings['id'] = $formId;
}
switch ($block['type']) { switch ($block['type']) {
case FormEntity::HTML_BLOCK_TYPE: case FormEntity::HTML_BLOCK_TYPE:
$html .= $this->html->render($block, $formSettings); $html .= $this->html->render($block, $formSettings);