Skip to content

Commit 019fc33

Browse files
authored
[6.0] Allow adding additional layout base path to form fields (#44590)
* Allow adding addition layout paths to FormField fields * Allow adding layout include paths via form XML * Move layout include path to __set method * Use auto add for layout include path * Rename parameter and add as string * Add since tag * Remove mistake * Add template override possibility * CS * Add layoutPaths to getter method * Remove unnecessary method * Cleanup set method * Improve layout set/generation * Code style
1 parent a99398f commit 019fc33

File tree

1 file changed

+29
-3
lines changed

1 file changed

+29
-3
lines changed

libraries/src/Form/FormField.php

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
use Joomla\Database\DatabaseAwareTrait;
2222
use Joomla\Database\DatabaseInterface;
2323
use Joomla\Database\Exception\DatabaseNotFoundException;
24+
use Joomla\Filesystem\Path;
2425
use Joomla\Registry\Registry;
2526
use Joomla\String\Normalise;
2627
use Joomla\String\StringHelper;
@@ -398,6 +399,14 @@ abstract class FormField implements DatabaseAwareInterface, CurrentUserInterface
398399
*/
399400
protected $renderLabelLayout = 'joomla.form.renderlabel';
400401

402+
/**
403+
* Additional layout paths to look for layout files
404+
*
405+
* @var array
406+
* @since __DEPLOY_VERSION__
407+
*/
408+
protected $layoutPaths = [];
409+
401410
/**
402411
* The data-attribute name and values of the form field.
403412
* For example, data-action-type="click" data-action-type="change"
@@ -474,6 +483,7 @@ public function __get($name)
474483
case 'validationtext':
475484
case 'showon':
476485
case 'parentclass':
486+
case 'layoutPaths':
477487
return $this->$name;
478488

479489
case 'input':
@@ -591,6 +601,10 @@ public function __set($name, $value)
591601
$this->$name = (int) $value;
592602
break;
593603

604+
case 'layoutIncludePath':
605+
$this->layoutPaths = \is_array($value) ? $value : explode(',', (string) $value);
606+
break;
607+
594608
default:
595609
// Detect data attribute(s)
596610
if (str_starts_with($name, 'data-')) {
@@ -658,7 +672,7 @@ public function setup(\SimpleXMLElement $element, $value, $group = null)
658672
$attributes = [
659673
'multiple', 'name', 'id', 'hint', 'class', 'description', 'labelclass', 'onchange', 'onclick', 'validate', 'pattern', 'validationtext',
660674
'default', 'required', 'disabled', 'readonly', 'autofocus', 'hidden', 'autocomplete', 'spellcheck', 'translateHint', 'translateLabel',
661-
'translate_label', 'translateDescription', 'translate_description', 'size', 'showon', ];
675+
'translate_label', 'translateDescription', 'translate_description', 'size', 'showon', 'layoutIncludePath'];
662676

663677
$this->default = isset($element['value']) ? (string) $element['value'] : $this->default;
664678

@@ -1060,7 +1074,13 @@ public function renderField($options = [])
10601074

10611075
$data = array_merge($this->collectLayoutData(), $data);
10621076

1063-
return $this->getRenderer($this->renderLayout)->render($data);
1077+
$renderer = $this->getRenderer($this->renderLayout);
1078+
1079+
if (isset($options['layoutIncludePath']) && is_dir(Path::check($options['layoutIncludePath']))) {
1080+
$renderer->addIncludePaths($options['layoutIncludePath']);
1081+
}
1082+
1083+
return $renderer->render($data);
10641084
}
10651085

10661086
/**
@@ -1372,7 +1392,13 @@ protected function getLayoutPaths()
13721392
{
13731393
$renderer = new FileLayout('default');
13741394

1375-
return $renderer->getDefaultIncludePaths();
1395+
$paths = $renderer->getDefaultIncludePaths();
1396+
1397+
foreach ($this->layoutPaths as $path) {
1398+
array_unshift($paths, JPATH_ROOT . '/' . ltrim((string) $path, '/'));
1399+
}
1400+
1401+
return $paths;
13761402
}
13771403

13781404
/**

0 commit comments

Comments
 (0)