Contact form: how to add telephone number for the mailing?

113 views
Skip to first unread message

Nico van de Kamp

unread,
Jan 19, 2025, 8:54:11 AMJan 19
to Joomla! General Development
I don't want it do with custom fields because I want to add telephone number after the e-mail. Default you have the mailing fields order:
- Name
- e-mail
- subject 
- message
- copy you're self.

I've made an override com_contact - contact - default_form.php, and I thought this will be easy, but I get stuck in the xml. Otherwise I hope it is an good learning moment.
I thought just before rendering I will extend the array $fields, which does contain the fieldset from contact.xml
I've tried different things, but at this moment have this:
[code]
...
<?php $fields = $this->form->getFieldset($fieldset->name); ?> // $fields is an array

<php? '**************** here starts my modification ****************'; ?>
<?php if ($fieldset->name === 'contact') {

$getFormName = $this->form->getName();
echo 'getFormName: ' . $getFormName . '<br />'; // com_contact.contact
echo is_array($fields) ? '$fields is_array - length: ' .count($fields) . '<br/>': '$fields is geen array<br />' ; // fields is_array - length: 6
// Define the new field XML
$telephoneFieldXml = '
<field
name="contact_telephone"
type="text"
label="Telephone"
description="Please provide your phone number."
required="false"
filter="string"
id="contact-telephone-field"
/>';
$telephoneField = new SimpleXMLElement($telephoneFieldXml);            

// get node contact_email, which we will append to
//   $xpath = new DOMXPath($fieldSetXml);
//        $eva = $xpath->query('/contact/field[.="contact_email"]')->item(0); // This doesn't work...?

// Get the fieldset XML form object (not the array of fields)
$fieldSetXml = $this->form->getXml();
// Convert the fieldset XML to a SimpleXMLElement
try {
$fieldSetXmlElement = new SimpleXMLElement($fieldSetXml->asXML());
} catch (Exception $e) {
print $e->getMessage();
}

        echo print_r($fieldSetXmlElement, true) . '<br /><br />';
echo is_array($fieldSetXmlElement->fieldset) ? '$fieldSetXmlElement is an array<br/>' : '$fieldSetXmlElement is not an array<br/>'; // $fieldSetXmlElement is not an array

// Find the "email" field and insert the new field after it
foreach ($fieldSetXmlElement->fieldset->field as $field) {

echo '$field[name]: ' . (string)$field['name'] . '<br />';
if ((string)$field['name'] === 'contact_email') {
$domFieldSet = dom_import_simplexml($fieldSetXmlElement->fieldset);
$domField = dom_import_simplexml($field);
$domTelephoneField = dom_import_simplexml($telephoneField);

// Insert the new field after the "email" field
$domField->parentNode->insertBefore($domTelephoneField, $domField->nextSibling);        // Adding the telephone field: Wrong Document Error:
break;
}
}
// Update the form with the modified XML structure
//        $this->form->setFields(new SimpleXMLElement($fieldSetXmlElement->asXML()), null, true, $fieldset->name);

<php? '**************** here einds my modification' ****************?>;
...
[/code]

Does somebody know how insert a the telephone field in the contact fieldset?

If I do this:
[code]
$result = $this->form->setField($telephoneField, null, false, $fieldset->name);
$fields[] = $telephoneField;   
echo '<pre>setField() result: ' . ($result ? 'true' : 'false') . '</pre>';    // true
[/code]

Than the field is add at the end after the message, but that is not what I want!


Steven Berkson

unread,
Jan 19, 2025, 11:53:29 AMJan 19
to joomla-de...@googlegroups.com
Have you considered using a custom contact mail field for phone number and then writing a plugin that moves the field to where you want it on the form with Javascript?

Steven Berkson


From: joomla-de...@googlegroups.com <joomla-de...@googlegroups.com> on behalf of Nico van de Kamp <ni...@nicovandekamp.nl>
Sent: Sunday, January 19, 2025 7:54:10 AM
To: Joomla! General Development <joomla-de...@googlegroups.com>
Subject: [jgen] Contact form: how to add telephone number for the mailing?
 
--
You received this message because you are subscribed to the Google Groups "Joomla! General Development" group.
To unsubscribe from this group and stop receiving emails from it, send an email to joomla-dev-gene...@googlegroups.com.
To view this discussion, visit https://groups.google.com/d/msgid/joomla-dev-general/8c07e12c-aa8b-4184-9d1b-7490b18d2484n%40googlegroups.com.

Steven Berkson

unread,
Jan 19, 2025, 11:54:39 AMJan 19
to joomla-de...@googlegroups.com
Or just adding a JS file on your override to move the field programmatically...

Steven Berkson


From: joomla-de...@googlegroups.com <joomla-de...@googlegroups.com> on behalf of Steven Berkson <sber...@blazingimages.com>
Sent: Sunday, January 19, 2025 10:53:17 AM
To: joomla-de...@googlegroups.com <joomla-de...@googlegroups.com>
Subject: Re: [jgen] Contact form: how to add telephone number for the mailing?
 

Nico van de Kamp

unread,
Jan 19, 2025, 5:06:39 PMJan 19
to Joomla! General Development
I have also tried with a plugin like:
[code]
class PlgSystemAddTelephone extends CMSPlugin
{
public function onContentPrepareForm($form, $data)
{
// Ensure we're targeting the contact form
if ($form->getName() !== 'com_contact.contact')
{
return;
}

// Add the telephone field
$form->load('<field name="contact_telephone" type="text" label="COM_CONTACT_TELEPHONE" description="COM_CONTACT_TELEPHONE_DESC" required="false" filter="string" />', false);
}
}
[/code]

But how does this work? How can I add this in the default_form.php, so that come after the e-mail? This plugin (I've also defined the plugin xml etc, and installed as an extension) and is called through Joomla.

But now I think if add the field to the fieldset, and when the fieldset is rendered than also this field is rendered.

Steven Berkson

unread,
Jan 20, 2025, 2:17:17 PMJan 20
to joomla-de...@googlegroups.com

I will probably make this a plugin for myself, which is why I placed the JS file in the directory ‘media/plg_contactMoveFields’

 

I added to a template override of default_form in com_contact

 

->registerAndUseScript('plg_contactMoveFields.contactMoveFields', 'media/plg_contactMoveFields/js/contactMoveFields.js', [], ['defer' => 'true'], ["core", "jquery"]);

 

 

 

Then the contactMoveFields.js reads

 

jQuery(document).ready(function($) {
    const formField = 'phone';
    const placeAfter = 'email';
    form = $('#contact-form');
    moveField = form.find($('#jform_com_fields_' + formField)).parents('div.control-group');
    removeFieldset = form.find('input[id="jform_com_fields_' + formField + '"]').parents('fieldset');
    moveField.insertAfter(form.find('input[id="jform_contact_' + placeAfter + '"]').parents('div.control-group'));
    removeFieldset.remove();
})

 

 

It works.

Steven Berkson

unread,
Jan 20, 2025, 2:36:19 PMJan 20
to joomla-de...@googlegroups.com

Slightly better and more consistent JS

 

jQuery(document).ready(function($) {
    const formField = 'phone';
    const placeAfter = 'email';
    form = $('#contact-form');

    moveField = form.find('#jform_com_fields_' + formField).parents('div.control-group');
    removeFieldset = form.find('#jform_com_fields_' + formField).parents('fieldset');
    moveField.insertAfter(form.find('#jform_contact_' + placeAfter).parents('div.control-group'));
    removeFieldset.remove();
})

Nico van de Kamp

unread,
Feb 7, 2025, 6:03:44 PMFeb 7
to Joomla! General Development
I have added  the field to the contact form with an override in the default_form.php. This is the code change for the contact form of Joomla 5.x
[code]
<?php foreach ($this->form->getFieldsets() as $fieldset) : ?>
<?php if ($fieldset->name === 'captcha' && $this->captchaEnabled) : ?>
<?php continue; ?>
<?php endif; ?>
<?php $fields = $this->form->getFieldset($fieldset->name); ?>

<?php if ($fieldset->name === 'contact') {
// Define the new field XML
$telephoneFieldXml = '
<field
name="contact_telephone"
type="text"
label="Telephone"
required="true"
filter="string"
id="contact-telephone-field"
/>';

$telephoneField = new SimpleXMLElement($telephoneFieldXml);

$fieldSetXml = $this->form->getXml($fieldset->name);
$fieldSetXmlElement = new SimpleXMLElement($fieldSetXml->asXML());

$newFieldSetXml = '<fieldset name="contact">';

foreach ($fieldSetXmlElement->fieldset->field as $field) {
$newFieldSetXml .= $field->asXML(); // Add existing field

// Insert new field **immediately after** contact_email
if ((string)$field['name'] === 'contact_email') {
$newFieldSetXml .= $telephoneFieldXml;
}
}

$newFieldSetXml .= '</fieldset>';

$newFieldSetXmlElement = new SimpleXMLElement($newFieldSetXml);
$this->form->setFields($newFieldSetXmlElement, null, true, $fieldset->name);

$fields = $this->form->getFieldset($fieldset->name);


}
?>
<?php if (count($fields)) : ?>
[/code]

One question: Although it is in the form, it is not send which I was expecting. Does any one has an idea why it is not sent? here is link



Steven Berkson

unread,
Feb 7, 2025, 6:30:18 PMFeb 7
to joomla-de...@googlegroups.com

Funny, I have been doing a lot with the contact component and form recently.

 

It is not sent because the contact component does not know about the field. You can use the custom fields to make a phone field. Make sure it is a “Mail” field not a contact field, and make sure you change the permissions so that editing the field is publicly allowed.

 

 

 

 

Make sure you say to show custom fields in the contact form.

 

The fields will come out Name, Email, Subject, Message, Phone. If you want it to be something like Name, Phone, Email, Subject, Message You can move the fields around with JavaScript. There was an earlier thread in this group on that which should help you do that.

Message has been deleted

Steven Berkson

unread,
Feb 14, 2025, 1:14:58 PMFeb 14
to joomla-de...@googlegroups.com

Did you modify the mail template for the contact form?

 

From: joomla-de...@googlegroups.com <joomla-de...@googlegroups.com> On Behalf Of Nico van de Kamp
Sent: Friday, February 14, 2025 03:07
To: Joomla! General Development <joomla-de...@googlegroups.com>
Subject: Re: [jgen] Contact form: how to add telephone number for the mailing?

 

Ok, I have changed it now, todo it by custom fields 'mail' for contact, with jQuery code is placed under e-mail field so that is the right position test side
But if I send this contact form, the telephone number is not send with the message.

Do I have to enable something at the custom field or at the contact?

Nico van de Kamp

unread,
Feb 20, 2025, 3:46:43 PMFeb 20
to Joomla! General Development
Sorry my mistake, I thought I was writing and suddenly I remember something. A  few days before, locally I had made, the change with custom field in combination with with you're jQuery. That is working. Here on the test side I had still my change with modifying template file default_form.php. I was testing this at the test domain and I thought that I had removed this from the test site. 

When I was writing the last message, suddenly I remembered this. I have press delete the message, but I see now it was not deleted.... Sorry!!!
Reply all
Reply to author
Forward
0 new messages