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
|
||||
|
||||
use Lewp\Resolve;
|
||||
use Lewp\VarFolder;
|
||||
use Lewp\{Module, Resolve, VarFolder};
|
||||
|
||||
return new class extends Lewp\Module
|
||||
return new class extends Module
|
||||
{
|
||||
const OPTIONS_ELEMENTS = 'elements';
|
||||
const OPTIONS_ELEMENT = 'element';
|
||||
const OPTIONS_LABEL = 'label';
|
||||
|
||||
// element specific
|
||||
const OPTIONS_SHOW_PLACEHOLDER = 'show_placeholder';
|
||||
|
|
@ -21,6 +21,7 @@ return new class extends Lewp\Module
|
|||
self::OPTIONS_ELEMENTS => false,
|
||||
self::OPTIONS_ELEMENT => false,
|
||||
self::OPTIONS_CONTENT => false,
|
||||
self::OPTIONS_LABEL => false,
|
||||
];
|
||||
|
||||
private $element_id = 0;
|
||||
|
|
@ -44,15 +45,50 @@ return new class extends Lewp\Module
|
|||
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)
|
||||
{
|
||||
$element += [self::OPTIONS_CONTENT => ''];
|
||||
if ($this->canContainData($element)) {
|
||||
$element["name"] = implode('-', [
|
||||
$this->getModuleName(),
|
||||
$this->getExecutionCount(),
|
||||
$this->element_id++,
|
||||
]);
|
||||
// if no label given, the element id should be increased
|
||||
$element["name"] = $this->generateFormElementName(
|
||||
true//!array_key_exists($element[self::OPTIONS_LABEL])
|
||||
);
|
||||
$element["id"] = $element["name"];
|
||||
} else {
|
||||
unset($element["name"]);
|
||||
}
|
||||
|
|
@ -62,27 +98,42 @@ return new class extends Lewp\Module
|
|||
$element[self::OPTIONS_CONTENT],
|
||||
array_diff_key($element, $this->non_attribute_options)
|
||||
);
|
||||
|
||||
foreach ($element[self::OPTIONS_ELEMENTS] as $element) {
|
||||
$form_element->appendChild($this->createFormElement($element));
|
||||
$form_element->appendChild($this->createFormElementGroup($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)
|
||||
{
|
||||
foreach ($elements as $element) {
|
||||
$this->appendChild($this->createFormElement($element));
|
||||
$this->appendChild($this->createFormElementGroup($element));
|
||||
}
|
||||
}
|
||||
|
||||
private function collectFormKeys() {
|
||||
private function collectFormKeys()
|
||||
{
|
||||
return preg_grep(
|
||||
'/'.
|
||||
'/' .
|
||||
$this->getModuleName()
|
||||
.'-'
|
||||
.$this->getExecutionCount()
|
||||
.'-\d+'
|
||||
.'/',
|
||||
. '-'
|
||||
. $this->getExecutionCount()
|
||||
. '-\d+'
|
||||
. '/',
|
||||
array_keys($_POST)
|
||||
);
|
||||
}
|
||||
|
|
@ -98,13 +149,13 @@ return new class extends Lewp\Module
|
|||
public function getData()
|
||||
{
|
||||
if (!$this->isSubmitted()) {
|
||||
return null;
|
||||
return [];
|
||||
}
|
||||
$data = [];
|
||||
$keys = $this->collectFormKeys();
|
||||
sort($keys, SORT_NUMERIC);
|
||||
foreach ($keys as $key) {
|
||||
$data []= $_POST[$key];
|
||||
$data [] = $_POST[$key];
|
||||
}
|
||||
return $data;
|
||||
}
|
||||
|
|
@ -112,10 +163,10 @@ return new class extends Lewp\Module
|
|||
private function processAction($action)
|
||||
{
|
||||
if ($action === '/') {
|
||||
return '/'.$this->getLanguage();
|
||||
return '/' . $this->getLanguage();
|
||||
}
|
||||
if (strpos($action, '/') === 0) {
|
||||
return '/'.$this->getLanguage().$action;
|
||||
return '/' . $this->getLanguage() . $action;
|
||||
}
|
||||
return implode('/', [
|
||||
'',
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue