Added label support.
If a label is available, the label and its input field are being wrapped by a div for better structuring/styling.
This commit is contained in:
parent
0e2d1cd54a
commit
c3844d6722
1 changed files with 71 additions and 20 deletions
91
module.php
91
module.php
|
|
@ -1,12 +1,12 @@
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
use Lewp\Resolve;
|
use Lewp\{Module, Resolve, VarFolder};
|
||||||
use Lewp\VarFolder;
|
|
||||||
|
|
||||||
return new class extends Lewp\Module
|
return new class extends Module
|
||||||
{
|
{
|
||||||
const OPTIONS_ELEMENTS = 'elements';
|
const OPTIONS_ELEMENTS = 'elements';
|
||||||
const OPTIONS_ELEMENT = 'element';
|
const OPTIONS_ELEMENT = 'element';
|
||||||
|
const OPTIONS_LABEL = 'label';
|
||||||
|
|
||||||
// element specific
|
// element specific
|
||||||
const OPTIONS_SHOW_PLACEHOLDER = 'show_placeholder';
|
const OPTIONS_SHOW_PLACEHOLDER = 'show_placeholder';
|
||||||
|
|
@ -21,6 +21,7 @@ return new class extends Lewp\Module
|
||||||
self::OPTIONS_ELEMENTS => false,
|
self::OPTIONS_ELEMENTS => false,
|
||||||
self::OPTIONS_ELEMENT => false,
|
self::OPTIONS_ELEMENT => false,
|
||||||
self::OPTIONS_CONTENT => false,
|
self::OPTIONS_CONTENT => false,
|
||||||
|
self::OPTIONS_LABEL => false,
|
||||||
];
|
];
|
||||||
|
|
||||||
private $element_id = 0;
|
private $element_id = 0;
|
||||||
|
|
@ -44,15 +45,50 @@ return new class extends Lewp\Module
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private function generateFormElementName(bool $increase_element_id): string
|
||||||
|
{
|
||||||
|
return implode(
|
||||||
|
'-',
|
||||||
|
[
|
||||||
|
$this->getModuleName(),
|
||||||
|
$this->getExecutionCount(),
|
||||||
|
($increase_element_id) ? $this->element_id++ : $this->element_id,
|
||||||
|
]
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
private function createFormElementGroup(array $element)
|
||||||
|
{
|
||||||
|
if (!isset($element[self::OPTIONS_LABEL])) {
|
||||||
|
return $this->createFormElement($element);
|
||||||
|
} else {
|
||||||
|
$label = $this->createLabel($element);
|
||||||
|
}
|
||||||
|
|
||||||
|
$wrapper = $this->createAndSetupElement(
|
||||||
|
'div',
|
||||||
|
'',
|
||||||
|
[
|
||||||
|
'class' => 'element-container'
|
||||||
|
]
|
||||||
|
);
|
||||||
|
$wrapper->appendChild($label);
|
||||||
|
|
||||||
|
$form_element = $this->createFormElement($element);
|
||||||
|
|
||||||
|
$wrapper->appendChild($form_element);
|
||||||
|
return $wrapper;
|
||||||
|
}
|
||||||
|
|
||||||
private function createFormElement(array $element)
|
private function createFormElement(array $element)
|
||||||
{
|
{
|
||||||
$element += [self::OPTIONS_CONTENT => ''];
|
$element += [self::OPTIONS_CONTENT => ''];
|
||||||
if ($this->canContainData($element)) {
|
if ($this->canContainData($element)) {
|
||||||
$element["name"] = implode('-', [
|
// if no label given, the element id should be increased
|
||||||
$this->getModuleName(),
|
$element["name"] = $this->generateFormElementName(
|
||||||
$this->getExecutionCount(),
|
true//!array_key_exists($element[self::OPTIONS_LABEL])
|
||||||
$this->element_id++,
|
);
|
||||||
]);
|
$element["id"] = $element["name"];
|
||||||
} else {
|
} else {
|
||||||
unset($element["name"]);
|
unset($element["name"]);
|
||||||
}
|
}
|
||||||
|
|
@ -62,27 +98,42 @@ return new class extends Lewp\Module
|
||||||
$element[self::OPTIONS_CONTENT],
|
$element[self::OPTIONS_CONTENT],
|
||||||
array_diff_key($element, $this->non_attribute_options)
|
array_diff_key($element, $this->non_attribute_options)
|
||||||
);
|
);
|
||||||
|
|
||||||
foreach ($element[self::OPTIONS_ELEMENTS] as $element) {
|
foreach ($element[self::OPTIONS_ELEMENTS] as $element) {
|
||||||
$form_element->appendChild($this->createFormElement($element));
|
$form_element->appendChild($this->createFormElementGroup($element));
|
||||||
}
|
}
|
||||||
return $form_element;
|
return $form_element;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private function createLabel(array $element)
|
||||||
|
{
|
||||||
|
|
||||||
|
$label = $this->createAndSetupElement(
|
||||||
|
'label',
|
||||||
|
$element[self::OPTIONS_LABEL],
|
||||||
|
[
|
||||||
|
'for' => $this->generateFormElementName(false),
|
||||||
|
]
|
||||||
|
);
|
||||||
|
return $label;
|
||||||
|
}
|
||||||
|
|
||||||
private function setupForm(array $elements)
|
private function setupForm(array $elements)
|
||||||
{
|
{
|
||||||
foreach ($elements as $element) {
|
foreach ($elements as $element) {
|
||||||
$this->appendChild($this->createFormElement($element));
|
$this->appendChild($this->createFormElementGroup($element));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private function collectFormKeys() {
|
private function collectFormKeys()
|
||||||
|
{
|
||||||
return preg_grep(
|
return preg_grep(
|
||||||
'/'.
|
'/' .
|
||||||
$this->getModuleName()
|
$this->getModuleName()
|
||||||
.'-'
|
. '-'
|
||||||
.$this->getExecutionCount()
|
. $this->getExecutionCount()
|
||||||
.'-\d+'
|
. '-\d+'
|
||||||
.'/',
|
. '/',
|
||||||
array_keys($_POST)
|
array_keys($_POST)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
@ -98,13 +149,13 @@ return new class extends Lewp\Module
|
||||||
public function getData()
|
public function getData()
|
||||||
{
|
{
|
||||||
if (!$this->isSubmitted()) {
|
if (!$this->isSubmitted()) {
|
||||||
return null;
|
return [];
|
||||||
}
|
}
|
||||||
$data = [];
|
$data = [];
|
||||||
$keys = $this->collectFormKeys();
|
$keys = $this->collectFormKeys();
|
||||||
sort($keys, SORT_NUMERIC);
|
sort($keys, SORT_NUMERIC);
|
||||||
foreach ($keys as $key) {
|
foreach ($keys as $key) {
|
||||||
$data []= $_POST[$key];
|
$data [] = $_POST[$key];
|
||||||
}
|
}
|
||||||
return $data;
|
return $data;
|
||||||
}
|
}
|
||||||
|
|
@ -112,10 +163,10 @@ return new class extends Lewp\Module
|
||||||
private function processAction($action)
|
private function processAction($action)
|
||||||
{
|
{
|
||||||
if ($action === '/') {
|
if ($action === '/') {
|
||||||
return '/'.$this->getLanguage();
|
return '/' . $this->getLanguage();
|
||||||
}
|
}
|
||||||
if (strpos($action, '/') === 0) {
|
if (strpos($action, '/') === 0) {
|
||||||
return '/'.$this->getLanguage().$action;
|
return '/' . $this->getLanguage() . $action;
|
||||||
}
|
}
|
||||||
return implode('/', [
|
return implode('/', [
|
||||||
'',
|
'',
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue