-
-
Save esedic/cbd25c10fe3218f58ee9dbd5ff362c61 to your computer and use it in GitHub Desktop.
Render All Joomla! 4 Standard Form Fields
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
declare(strict_types=1); | |
/** | |
* Render All Joomla! 4 Standard Form Fields | |
* | |
* @version 1.0.0 | |
* @package all | |
* @author Alexandre ELISÉ <[email protected]> | |
* @copyright (c) 2009-2021 . Alexandre ELISÉ . Tous droits réservés. | |
* @license GPL-2.0-and-later GNU General Public License v2.0 or later | |
* @link https://alexapi.cloud | |
*/ | |
use Joomla\CMS\Filesystem\File; | |
use Joomla\CMS\Filesystem\Folder; | |
use Joomla\CMS\Form\Form; | |
use Joomla\CMS\Form\FormHelper; | |
use Joomla\CMS\Version; | |
defined('_JEXEC') or die; | |
$fieldsPath = JPATH_LIBRARIES . '/src/Form/Field'; | |
FormHelper::addFieldPath($fieldsPath); | |
$exclude = ['PredefinedlistField.php', 'SubformField.php', 'PluginsField.php']; | |
$files = Folder::files($fieldsPath, '.php', false, false, $exclude); | |
$xml = <<<XML | |
<?xml version="1.0" encoding="UTF-8" ?> | |
<form name="all"> | |
<fieldset name="all"> | |
<fields name="all" /> | |
</fieldset> | |
</form> | |
XML; | |
$form = Form::getInstance('all', $xml); | |
if (empty($files)) { | |
echo 'No fields found<br>'; | |
return; | |
} | |
foreach ($files as $file) | |
{ | |
try | |
{ | |
$type = strtolower(str_replace('Field','', File::stripExt($file))); | |
$extension = $type === 'category' ? ' extension="com_content" ' : ''; | |
$field = new SimpleXMLElement('<field name="name-of-'.$type.'" type="'.$type.'" label="Label of '.$type.'" description="Description of '.$type.'" '.$extension.' />'); | |
$form->setField($field, 'all', true, 'all'); | |
} | |
catch (Exception $err) | |
{ | |
echo $err->getMessage() . ' ' . $err->getFile() . ' ' . $err->getLine(); | |
continue; | |
} | |
} | |
echo $form->renderFieldset('all'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment