From 32a87598d72dfc5e9af85b07f017349d6c9c8780 Mon Sep 17 00:00:00 2001 From: Nick Sagona Date: Sun, 4 Feb 2024 15:32:26 -0600 Subject: [PATCH] Add prepend/append functionality to form field elements --- src/Element/AbstractElement.php | 126 ++++++++++++++++++++++++++++++++ src/Fields.php | 10 +++ src/Fieldset.php | 69 ++++++++++++----- 3 files changed, 187 insertions(+), 18 deletions(-) diff --git a/src/Element/AbstractElement.php b/src/Element/AbstractElement.php index e299e27..8a510c3 100644 --- a/src/Element/AbstractElement.php +++ b/src/Element/AbstractElement.php @@ -59,6 +59,18 @@ abstract class AbstractElement extends Child implements ElementInterface */ protected array $hintAttributes = []; + /** + * Form element prepend contents + * @var ?string + */ + protected ?string $prepend = null; + + /** + * Form element append contents + * @var ?string + */ + protected ?string $append = null; + /** * Form element required property * @var bool @@ -160,6 +172,30 @@ public function setHint(string $hint): AbstractElement return $this; } + /** + * Set the prepend contents of the form element object + * + * @param string $prepend + * @return AbstractElement + */ + public function setPrepend(string $prepend): AbstractElement + { + $this->prepend = $prepend; + return $this; + } + + /** + * Set the append contents of the form element object + * + * @param string $append + * @return AbstractElement + */ + public function setAppend(string $append): AbstractElement + { + $this->append = $append; + return $this; + } + /** * Set an attribute of the label of the form element object * @@ -329,6 +365,16 @@ public function getLabel(): ?string return $this->label; } + /** + * Determine if form element has a label + * + * @return bool + */ + public function hasLabel(): bool + { + return !empty($this->label); + } + /** * Get form element object hint * @@ -339,6 +385,56 @@ public function getHint(): ?string return $this->hint; } + /** + * Determine if form element has a hint + * + * @return bool + */ + public function hasHint(): bool + { + return !empty($this->hint); + } + + /** + * Get form element object prepend contents + * + * @return ?string + */ + public function getPrepend(): ?string + { + return $this->prepend; + } + + /** + * Determine if form element has prepend content + * + * @return bool + */ + public function hasPrepend(): bool + { + return !empty($this->prepend); + } + + /** + * Get form element object append contents + * + * @return ?string + */ + public function getAppend(): ?string + { + return $this->append; + } + + /** + * Determine if form element has append content + * + * @return bool + */ + public function hasAppend(): bool + { + return !empty($this->append); + } + /** * Get the attributes of the form element object label * @@ -349,6 +445,16 @@ public function getLabelAttributes(): array return $this->labelAttributes; } + /** + * Determine if form element has label attributes + * + * @return bool + */ + public function hasLabelAttributes(): bool + { + return !empty($this->labelAttributes); + } + /** * Get the attributes of the form element object hint * @@ -359,6 +465,16 @@ public function getHintAttributes(): array return $this->hintAttributes; } + /** + * Determine if form element has hint attributes + * + * @return bool + */ + public function hasHintAttributes(): bool + { + return !empty($this->hintAttributes); + } + /** * Get validators * @@ -369,6 +485,16 @@ public function getValidators(): array return $this->validators; } + /** + * Determine if form element has validators + * + * @return bool + */ + public function hasValidators(): bool + { + return !empty($this->validators); + } + /** * Get whether the form element object is required * diff --git a/src/Fields.php b/src/Fields.php index 9c527af..0c1f97b 100644 --- a/src/Fields.php +++ b/src/Fields.php @@ -64,6 +64,8 @@ public static function create(string $name, array $field): AbstractElement $hint = $field['hint'] ?? null; $hintAttribs = $field['hint-attributes'] ?? null; $labelAttribs = $field['label-attributes'] ?? null; + $prepend = $field['prepend'] ?? null; + $append = $field['append'] ?? null; $errorPre = (isset($field['error']) && ($field['error'] == 'pre')); @@ -142,6 +144,14 @@ public static function create(string $name, array $field): AbstractElement if (($labelAttribs !== null) && is_array($labelAttribs)) { $element->setLabelAttributes($labelAttribs); } + // Set prepend content. + if ($prepend !== null) { + $element->setPrepend($prepend); + } + // Set append content. + if ($append !== null) { + $element->setAppend($append); + } // Set the hint. if ($hint !== null) { $element->setHint($hint); diff --git a/src/Fieldset.php b/src/Fieldset.php index eceaa45..dae2444 100644 --- a/src/Fieldset.php +++ b/src/Fieldset.php @@ -449,7 +449,7 @@ public function prepareForView(): array foreach ($this->fields as $groups) { foreach ($groups as $field) { - if ($field->getLabel() !== null) { + if ($field->hasLabel()) { $labelFor = $field->getName() . (($field->getNodeName() == 'fieldset') ? '1' : ''); $label = new Child('label', $field->getLabel()); $label->setAttribute('for', $labelFor); @@ -466,7 +466,7 @@ public function prepareForView(): array $fields[$field->getName() . '_label'] = $label->render(); } - if ($field->getHint() !== null) { + if ($field->hasHint()) { $hint = new Child('span', $field->getHint()); if ($field->getHintAttributes() !== null) { $hint->setAttributes($field->getHintAttributes()); @@ -504,13 +504,13 @@ protected function prepareTable(): void } $tr = new Child('tr'); - if ($field->getLabel() !== null) { + if ($field->hasLabel()) { $td = new Child('td'); $labelFor = $field->getName() . (($field->getNodeName() == 'fieldset') ? '1' : ''); $label = new Child('label', $field->getLabel()); $label->setAttribute('for', $labelFor); - if ($field->getLabelAttributes() !== null) { + if ($field->hasLabelAttributes()) { $label->setAttributes($field->getLabelAttributes()); } if ($field->isRequired()) { @@ -524,21 +524,32 @@ protected function prepareTable(): void $tr->addChild($td); } - $td = new Child('td'); + $nodeValue = null; + $options = []; + if ($field->hasAppend()) { + $nodeValue = $field->getAppend(); + $options = ['childrenFirst' => true]; + } else if ($field->hasPrepend()) { + $nodeValue = $field->getPrepend(); + $options = ['childrenFirst' => false]; + } + + $td = new Child('td', $nodeValue, $options); + if ($field->isErrorPre()) { $td->addChildren($errors); } $td->addChild($field); - if ($field->getHint() !== null) { + if ($field->hasHint()) { $hint = new Child('span', $field->getHint()); - if ($field->getHintAttributes() !== null) { + if ($field->hasHintAttributes()) { $hint->setAttributes($field->getHintAttributes()); } $td->addChild($hint); } - if ($field->getLabel() === null) { + if ($field->hasLabel()) { $td->setAttribute('colspan', 2); } if (!$field->isErrorPre()) { @@ -569,12 +580,23 @@ protected function prepareElement(string $element): void } } - $container = new Child($element); - if ($field->getLabel() !== null) { + $nodeValue = null; + $options = []; + if ($field->hasAppend()) { + $nodeValue = $field->getAppend(); + $options = ['childrenFirst' => true]; + } else if ($field->hasPrepend()) { + $nodeValue = $field->getPrepend(); + $options = ['childrenFirst' => false]; + } + + $container = new Child($element, $nodeValue, $options); + + if ($field->hasLabel()) { $labelFor = $field->getName() . (($field->getNodeName() == 'fieldset') ? '1' : ''); $label = new Child('label', $field->getLabel()); $label->setAttribute('for', $labelFor); - if ($field->getLabelAttributes() !== null) { + if ($field->hasLabelAttributes()) { $label->setAttributes($field->getLabelAttributes()); } if ($field->isRequired()) { @@ -592,9 +614,9 @@ protected function prepareElement(string $element): void } $container->addChild($field); - if ($field->getHint() !== null) { + if ($field->hasHint()) { $hint = new Child('span', $field->getHint()); - if ($field->getHintAttributes() !== null) { + if ($field->hasHintAttributes()) { $hint->setAttributes($field->getHintAttributes()); } $container->addChild($hint); @@ -625,13 +647,13 @@ protected function prepareDl(): void } } - if ($field->getLabel() !== null) { + if ($field->hasLabel()) { $dt = new Child('dt'); $labelFor = $field->getName() . (($field->getNodeName() == 'fieldset') ? '1' : ''); $label = new Child('label', $field->getLabel()); $label->setAttribute('for', $labelFor); - if ($field->getLabelAttributes() !== null) { + if ($field->hasLabelAttributes()) { $label->setAttributes($field->getLabelAttributes()); } if ($field->isRequired()) { @@ -645,15 +667,26 @@ protected function prepareDl(): void $dl->addChild($dt); } - $dd = new Child('dd'); + $nodeValue = null; + $options = []; + if ($field->hasAppend()) { + $nodeValue = $field->getAppend(); + $options = ['childrenFirst' => true]; + } else if ($field->hasPrepend()) { + $nodeValue = $field->getPrepend(); + $options = ['childrenFirst' => false]; + } + + $dd = new Child('dd', $nodeValue, $options); + if ($field->isErrorPre()) { $dd->addChildren($errors); } $dd->addChild($field); - if ($field->getHint() !== null) { + if ($field->hasHint()) { $hint = new Child('span', $field->getHint()); - if ($field->getHintAttributes() !== null) { + if ($field->hasHintAttributes()) { $hint->setAttributes($field->getHintAttributes()); } $dd->addChild($hint);