Skip to content

Commit

Permalink
Let component manipulate custom field form
Browse files Browse the repository at this point in the history
  • Loading branch information
bembelimen committed Dec 13, 2023
1 parent 47e0fae commit 82f03d9
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
32 changes: 32 additions & 0 deletions administrator/components/com_fields/src/Model/FieldModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Joomla\CMS\Component\ComponentHelper;
use Joomla\CMS\Event\CustomFields\PrepareDomEvent;
use Joomla\CMS\Factory;
use Joomla\CMS\Fields\FieldsServiceInterface;
use Joomla\CMS\Form\Form;
use Joomla\CMS\Form\FormHelper;
use Joomla\CMS\Language\Text;
Expand Down Expand Up @@ -1108,6 +1109,37 @@ function () use ($component, $section) {
}
}

$componentInterface = Factory::getApplication()->bootComponent($component);

if ($componentInterface instanceof FieldsServiceInterface) {
$componentInterface->prepareForm($form, $data);
} else {
// Try to find the component helper.
$eName = str_replace('com_', '', $component);
$path = Path::clean(JPATH_ADMINISTRATOR . "/components/$component/helpers/fields.php");

if (file_exists($path)) {
$cName = ucfirst($eName) . ucfirst($section) . 'HelperFields';

\JLoader::register($cName, $path);

if (class_exists($cName) && \is_callable([$cName, 'onPrepareForm'])) {
$lang->load($component, JPATH_BASE, null, false, false)
|| $lang->load($component, JPATH_BASE . '/components/' . $component, null, false, false)
|| $lang->load($component, JPATH_BASE, $lang->getDefault(), false, false)
|| $lang->load($component, JPATH_BASE . '/components/' . $component, $lang->getDefault(), false, false);
\call_user_func_array([$cName, 'onPrepareForm'], [&$form]);

// Check for an error.
if ($form instanceof \Exception) {
$this->setError($form->getMessage());

return false;
}
}
}
}

// Trigger the default form events.
parent::preprocessForm($form, $data, $group);
}
Expand Down
14 changes: 14 additions & 0 deletions libraries/src/Fields/FieldsServiceInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

namespace Joomla\CMS\Fields;

use Joomla\CMS\Form\Form;

// phpcs:disable PSR1.Files.SideEffects
\defined('_JEXEC') or die;
// phpcs:enable PSR1.Files.SideEffects
Expand Down Expand Up @@ -41,4 +43,16 @@ public function validateSection($section, $item = null);
* @since 4.0.0
*/
public function getContexts(): array;

/**
* Prepares the category form
*
* @param Form $form The form to change
* @param array|object $data The form data
*
* @return void
*
* @since __DEPLOY_VERSION__
*/
public function prepareForm(Form $form, $data);
}

0 comments on commit 82f03d9

Please sign in to comment.