Skip to content

Instantly share code, notes, and snippets.

@alexandreelise
Last active May 29, 2023 21:19
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save alexandreelise/0ee4b3eca712025a81fbb4626a64ac5f to your computer and use it in GitHub Desktop.
Save alexandreelise/0ee4b3eca712025a81fbb4626a64ac5f to your computer and use it in GitHub Desktop.
Render All Joomla! 4 Standard Form Fields

j4xall

Render All Joomla! 4 Standard Form Fields using a article layout override. To make it work, you can put this the j4xall.php file at this location:


JPATH_ROOT/templates/cassopeia/html/com_content/article/j4xall.php

JPATH_ROOT is the web root of your Joomla! 4 website.

You can also change rename the j4xall.php file to the name you prefer.

Take care Super Joomlers

preview

Preview of the result of the code snippet


INFOS

English: Click here to get in touch

Français: Cliquez ici pour me contacter


<?php
declare(strict_types=1);
/**
* Render All Joomla! 4 Standard Form Fields
*
* @version 1.0.0
* @package all
* @author Mr Alexandre J-S William ELISÉ <code@apiadept.com>
* @copyright (c) 2009-2021 . Mr Alexandre J-S William ELISÉ . Tous droits réservés.
* @license GPL-2.0-and-later GNU General Public License v2.0 or later
* @link https://apiadept.com
*/
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