1

I have a custom component I am creating. I am trying to implement custom fields for several of my views. I have custom fields added currently and I can create a custom field and custom field groups for each of the views that I am wanting, but I can't seem to get them to show up on my edit layout.

Here is the code for my edit layout.

<?php
//NO DIRECT ACCESS
defined('_JEXEC') or die('Restricted access');
?>
<form action="<?php echo JRoute::_('index.php?option=com_example&layout=edit&id='.(int) $this->item->id);?>" method="post" name="adminForm" id="adminForm" class="form-validate">
<fieldset class="adminform">
    <legend><?php echo JText::_('Client Details'); ?></legend>
    <?php foreach($this->form->getFieldset() as $field): ?>
        <?php if (!$field->hidden): ?>
            <?php echo $field->label; ?>
        <?php endif; ?>
        <?php echo $field->input; ?>
    <?php endforeach; ?>
</fieldset>
<fieldset class="adminform">
    <legend><?php echo JText::_('Custom Fields'); ?></legend>
    <?php
    JLoader::register('FieldsHelper', JPATH_ADMINISTRATOR . '/components/com_fields/helpers/fields.php');
    $customFields = FieldsHelper::getFields('com_example.clients', $this->item, true);
    echo '<pre>';
    print_r($customFields);
    echo '</pre>';
    ?>
    <?php foreach($customFields as $field): ?>
        <?php if (!$field->hidden): ?>
            <?php echo $field->label; ?>
        <?php endif; ?>
        <?php echo $field->input; ?>
    <?php endforeach; ?>
</fieldset>
<div>
    <input type="hidden" name="task" value="client.edit" />
    <?php echo JHtml::_('form.token'); ?>
</div>
</form>

I when I print_r() the $customFields variable I can see the custom field that I created, but for the life of me I can't figure out how to convert that stdObject over to the actual form field. Anyone have any ideas on this? I am sure it is something simple but I don't see the answer in the Joomla Component Dev Tutorials online.

2 Answers 2

1

Custom fields are added to the form definition by a plugin using onContentPrepareForm event. For this reason you don't need and should not hardcode any code related to custom fields. Form fields can be rendered in many different ways. It depends on your form structure. If fields are inside fieldsets, you can use Joomla\CMS\Form\Form::getFieldsets() to get the fieldsets, iterate over them and render them using Joomla\CMS\Form\Form::renderFieldset():

<?php foreach ($this->form->getFieldsets() as $fieldset) : ?>
    <fieldset>
        <legend><?= JText::_($fieldset->label); ?></legend>
        <?= $this->form->renderFieldset($fieldset->name); ?>
    </fieldset>
<?php endforeach; ?>

There are also some layouts available in layouts/joomla/edit directory that you can use but they're rather quirky.

7
  • Ok so here is what is bothering me. I have my fieldset created under the custom fields fieldsets. I have a simple text field created and attached to that fieldset. When I print_r($this->form->getFieldsets()) it returns an empty array for me. I am guessing that onContentPrepare is not "attaching" my custom fieldset to my form? It seems like I am missing a step here somewhere.
    – Terry Carter
    Commented Sep 20, 2022 at 22:07
  • Looking in the database I can see that my custom fields fieldset and the text field are indeed created and have the correct context associated with them for my extension if that helps.
    – Terry Carter
    Commented Sep 20, 2022 at 22:08
  • Are there any changes to the model that I missed when adding custom fields by chance?
    – Terry Carter
    Commented Sep 20, 2022 at 22:27
  • do you have any other thoughts on this or should I start a bounty?
    – Terry Carter
    Commented Sep 23, 2022 at 14:17
  • Is onContentPrepareForm event even being triggered by the model?
    – Sharky
    Commented Sep 24, 2022 at 21:33
0

The issue I had with custom fields not showing up was I had my content name incorrect in my helper file. The context has to match the view that you are applying custom fields to or they will never load.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.