Registration override with custom fields Topic is solved

For Joomla! 3.x Coding related discussions, you could also use: http://groups.google.com/group/joomla-dev-general

Moderators: ooffick, General Support Moderators

Forum rules
Post Reply
mkoontz
Joomla! Enthusiast
Joomla! Enthusiast
Posts: 132
Joined: Thu May 13, 2010 4:43 am
Location: Athens Greece

Registration override with custom fields

Post by mkoontz » Wed Feb 21, 2024 5:44 pm

I need some help with Custom Fields for users. FYI, the client's site is still using J3 (customized extensions require him to stay on it until he pays to have them J4/5 compatible).
Nevertheless, we need to add some custom fields to the "registration" page. However, he has a custom User Profile plugin that allows to collect First Name & Last Name instead of just one "Name" field.
As a result, an override was created to set the order the fields would display in for the Registration page so it would display the custom User Profile plugin above the default and the 'terms' at the end. The problem is, the override isn't working with Custom Fields so they don't show up on the frontend.
The code snippet for the override is this:

Code: Select all

        <?php $fieldsetsDefault = $this->form->getFieldsets(); ?>
        <?php $fieldsetsOrdering = ['customprofile', 'default', 'terms']; ?>
        <?php foreach ($fieldsetsOrdering as $fieldOrderingValue) : ?>
            <?php $fieldset = $fieldsetsDefault[$fieldOrderingValue] ?? []; ?>
            <?php if (empty($fieldset)) : ?>
                <?php continue; ?>
            <?php endif; ?>
            <?php $fields = $this->form->getFieldset($fieldset->name); ?>
            <?php if (count($fields)) : ?>
                <fieldset>
                    <?php echo $this->form->renderFieldset($fieldset->name); ?>
                </fieldset>
            <?php endif; ?>
        <?php endforeach; ?>
You can see with the "fieldsetsOrdering" above that it sets the order of the fields. What I can't figure out is how to get it to display the custom fields after 'default' and before 'terms'.
I checked the default.php file, and the code snippet from that same section is this:

Code: Select all

 <?php // Iterate through the form fieldsets and display each one. ?>
 <?php foreach ($this->form->getFieldsets() as $fieldset) : ?>
 <?php $fields = $this->form->getFieldset($fieldset->name); ?>
 <?php if (count($fields)) : ?>
 <fieldset>
 <?php // If the fieldset has a label set, display it as the legend. ?>
 <?php if (isset($fieldset->label)) : ?>
 <legend><?php echo JText::_($fieldset->label); ?></legend>
 <?php endif; ?>
 <?php echo $this->form->renderFieldset($fieldset->name); ?>
 </fieldset>
 <?php endif; ?>
 <?php endforeach; ?>
However, I can't find anything that specifically calls the custom fields and I haven't been able to find anything on how I can call the custom fields to have them display here in the override.
Do any of you have a suggestion on how I can get the custom fields to display with the override?
Thanks in advance for any help!

User avatar
toivo
Joomla! Master
Joomla! Master
Posts: 17445
Joined: Thu Feb 15, 2007 5:48 am
Location: Sydney, Australia

Re: Registration override with custom fields

Post by toivo » Thu Feb 22, 2024 7:11 am

Your override code is correct in principle. The custom fields are missing because the fieldset 'customprofile' does not exist. In fact, Joomla 3.10.12 assigns names like 'fields-1' automatically to custom field groups, depending on the id of field group in the database table _fields_groups.

These debugging lines display the structure of the array with the names of the fieldsets.

Code: Select all

        <?php $fieldsetsDefault = $this->form->getFieldsets(); ?>
		<?php $debug = print_r($fieldsetsDefault, true); 
			  echo '<p>' . $debug . '</p>';
		?>
This screenshot shows an example of a fieldsets array, containing the custom field group 'My Custom Field Group', known in my test by Joomla 3 as 'fields-2'. The field group has one field, 'Custom Text Field'. The first two standard user fields of the registration form are also shown.
User-Registration-Custom-Fields.png
You do not have the required permissions to view the files attached to this post.
Toivo Talikka, Global Moderator

mkoontz
Joomla! Enthusiast
Joomla! Enthusiast
Posts: 132
Joined: Thu May 13, 2010 4:43 am
Location: Athens Greece

Re: Registration override with custom fields

Post by mkoontz » Sun Feb 25, 2024 11:56 pm

Thank you very much for the reply Toivo!

I received some help from a member in the Joomla facebook group which was a similar solution to what you provided.
He provided the following line of PHP code:

Code: Select all

<?php echo '<pre>'; print_r($fieldsetsDefault); echo '</pre>'; ?>
that when placed after the following line of code in the template override:

Code: Select all

<?php $fieldsetsDefault = $this->form->getFieldsets(); ?>
displays the fields, so with that information I was able to find the name of the field groups.

Your code though is much more succinct. I will definitely be saving this so I can use it or something like it in the future if I need it.

FYI, I know that "customprofile" isn't a field, it is the name of a custom plugin that was coded by another developer to be used on the registration page and placed at the top of the page, hence the template override.

Thank you again for your help!

P.S. - I'll be posting another question here shortly that I hope you can help with.


Post Reply

Return to “Joomla! 3.x Coding”