Skip to content

Instantly share code, notes, and snippets.

@esedic
Forked from alexandreelise/README.md
Created October 27, 2021 14:38
Show Gist options
  • Save esedic/cbd25c10fe3218f58ee9dbd5ff362c61 to your computer and use it in GitHub Desktop.
Save esedic/cbd25c10fe3218f58ee9dbd5ff362c61 to your computer and use it in GitHub Desktop.
Render All Joomla! 4 Standard Form Fields
<?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