Skip to content

Instantly share code, notes, and snippets.

@pgasiorowski
Created December 21, 2013 12:08
Show Gist options
  • Save pgasiorowski/8068574 to your computer and use it in GitHub Desktop.
Save pgasiorowski/8068574 to your computer and use it in GitHub Desktop.
Bug in JForm->load() implementation
# Lets consider a content plugin with the following implementation:
# Basically, its intention is to add/replace elements from a form,
# Joomla 'Global Configuration' form in this case
<?php
public function onContentPrepareForm($form, $data)
{
// The name of the form we want to process
$formName = 'com_config.application';
// The xml definition of our additions/replacements
$fileName = __DIR__.'/forms/'.$formName.'.xml';
// Only process com_config application forms and only process it if the override file exists
if ($form->getName() == $formName && is_file($fileName))
{
// Attempt to load the XML file as a simpleXml object
$gaeXml = simplexml_load_file($fileName);
// Replace XML nodes[aka Joomla Form fields] in this form with ones that have been defined in our replacement file
$form->load($gaeXml);
}
}
?>
# Original definition of a form field from com_config.application
<field
name="mailer"
type="list"
default="mail"
label="COM_CONFIG_FIELD_MAIL_MAILER_LABEL"
description="COM_CONFIG_FIELD_MAIL_MAILER_DESC"
required="true"
filter="word">
<option value="mail">COM_CONFIG_FIELD_VALUE_PHP_MAIL</option>
<option value="sendmail">COM_CONFIG_FIELD_VALUE_SENDMAIL</option>
<option value="smtp">COM_CONFIG_FIELD_VALUE_SMTP</option>
</field>
# Field definition we want to set instead and also the expected result (added 'gae' option and changed 'default' attribute)
<field
name="mailer"
type="list"
default="gae"
label="COM_CONFIG_FIELD_MAIL_MAILER_LABEL"
description="COM_CONFIG_FIELD_MAIL_MAILER_DESC"
required="true"
filter="word">
<option value="gae">COM_CONFIG_FIELD_VALUE_GAE_MAIL</option>
<option value="mail">COM_CONFIG_FIELD_VALUE_PHP_MAIL</option>
<option value="sendmail">COM_CONFIG_FIELD_VALUE_SENDMAIL</option>
<option value="smtp">COM_CONFIG_FIELD_VALUE_SMTP</option>
</field>
# Actual result is as follows (the descendants options are not copied over):
<field
name="mailer"
type="list"
default="gae"
label="COM_CONFIG_FIELD_MAIL_MAILER_LABEL"
description="COM_CONFIG_FIELD_MAIL_MAILER_DESC"
required="true"
filter="word">
</field>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment