Page 1 of 1

Overriding Form Field XML?

Posted: Thu Jan 13, 2011 12:00 am
by bizzynate
The new security feature of specifying available and allowed form fields is quite nice and it's definitely going to help keep hackers from exploiting sites. But sometimes there are situations where one would like to override certain fields at the template level. Is it possible?

Here's a scenario:
I create a new template. In it, I am displaying some additional fields within /templates/mytemplate/html/com_content/form/edit.php so my administrators who log in to the front end of the site can control additional parameters for articles ("created" and "created_by", for example). Simply adding them to the override file displays them, but because /components/com_content/models/forms/article.xml has filter="unset" for each of these and lacks proper label, description and other attributes, the adaptability seems to stop there.

In a 1.5 template, because this security procedure wasn't in place, it was easy to simply add in missing fields and the logic to support them. But 1.6 seems to present a new challenge.

Given that example...
  • Can you specify an override XML file for forms so the core doesn't have to be hacked? Or does this defeat the purpose?
  • Where would this need to be placed within the template package if it is an option?
  • What would be a good alternative if this isn't an option?
Thanks in advance!

Re: Overriding Form Field XML?

Posted: Thu Jan 13, 2011 10:13 pm
by dylanjh
Yes, Id like to know how to do this too. I havent tried putting a modified ( for example ) registration.xml in /templates/<template>/com_users/register/
Ill probably try it tomorrow

Re: Overriding Form Field XML?

Posted: Thu Jan 13, 2011 10:17 pm
by dylanjh
Curiosity got the better of me. No that didnt work. Im looking to remove some fields from the xml file. I could just tell the template not to display a specific field, but that seems a bit messy.

Re: Overriding Form Field XML?

Posted: Thu Jan 13, 2011 10:21 pm
by dylanjh
doesnt work putting into /templates/<template name>/xml/com_user/registration either... hmmmm....

Re: Overriding Form Field XML?

Posted: Fri Jan 14, 2011 8:09 am
by dylanjh
Ok, well Ive got a solution, as to whether its the "correct" way is still unknown.

So we know we can override output still, by using the normal template override feature. For me, Im trying to override the login page. So, Im taking a copy of /components/com_users/views/login/tmpl/default.php and putting it into /templates/beez_20/html/com_users/login.

So far so 1.5.

Now take a copy of /components/com_users/models/forms/login.xml and place in /templates/beez_20/html/com_users/login as well

now edit /templates/beez_20/html/com_users/login/default.php and add at the top of the form (I added mine just after the form tag) the following commands:

Code: Select all

$this->form->reset( true ); // to reset the form xml loaded by the view
	$this->form->loadFile( dirname(__FILE__) . DS . "login.xml"); // to load in our own version of login.xml
Then you can safely edit /templates/beez_20/html/com_users/registration/registration.xml

Re: Overriding Form Field XML?

Posted: Wed Jan 19, 2011 8:59 pm
by daniel_jackson2
@bizzynate

No, you cannot add additional fields for an article the way you are thinking (automatically). The edit.php doesn't echo the whole form, only the default fields for an article and metadata params. You can tell your edit.php file to echo those if you'd like but it would get messy as you would have to change the model class for com_content.

Yes, you CAN over ride the values of the fields at the article. You need to create an alternate layout. Copy and paste edit.php into the same folder and rename it to something else.

From there you need to tell JForm to load your new file path for your form and then the fields will be available. You can also put an .xml file in the folder and define the new settings there, but at some point you need to manually echo the params to the page. You can echo all fieldsets in your new .xml file... go to the docs to learn how to over-ride default fields from alternate layouts.

See the problem is, when you create a new menu item, your new layout will appear lol. There are several ways of doing things but, by default, its one or the other (alternate layout or alternate menu item). If you have an .xml file in your folder (say newform.xml) and your new alternate layout is named (newform.php), your alternate layout option will not show up (at the config options for that item). Its kinda messy.

Re: Overriding Form Field XML?

Posted: Fri Mar 25, 2011 1:40 am
by gavingrubb
"dylanjh" provided the best answer, however I found that doing that with com_content in admin caused problems. Specifically, the whole form would be "reset", meaning that any data already loaded would be lost - the form would always be empty even when it shouldn't be. Also, the form data wasn't getting saved in the database, since Joomla was automatically loading the (non-custom) article.xml file while doing a save()

An alternate solution, that works for com_content in admin console:
1) Copy /administrator/components/com_content/models/forms/article.xml to /administrator/templates/YOUR_ADMIN_TEMPLATE_NAME/html/com_content/article/article.xml
2) Copy /administrator/components/com_content/views/article/tmpl/edit.php to /administrator/templates/YOUR_ADMIN_TEMPLATE_NAME/html/com_content/article/edit.php
3) Create your own custom Joomla Plugin. It must be a "system" plugin. Inside your plugin add this function:
function onAfterRoute() {
jimport('joomla.form.form');
JForm::addFormPath(JPATH_ADMINISTRATOR.DS.'components'.DS.'com_content/models/forms');
JForm::addFormPath(JPATH_ADMINISTRATOR.'/templates/bluestork/html/com_content/article');
}

NOTE: you should change "/templates/bluestork/" above to the path to your admin template, if it's something other than 'bluestork'
4) Install your system plugin and ensure it is enabled
5) Customise both custom files: edit.php and article.xml as necessary. NOTE that if you're just adding basic parameters, all you need to do is add those parameters to article.xml under the '<fields name="attribs">' section.... however if you're adding an extra 'big' custom field (like a text editor), you'll need to edit both article.xml (under the first fieldset), ** AND ** add the custom field to your edit.php file, ** AND ** add the extra field to the end of your 'jos_content' database table (using PhpMyAdmin or similar)

Re: Overriding Form Field XML?

Posted: Mon May 09, 2011 6:48 pm
by davidbalt
The approach I am taking is to use the onContentPrepareForm event to inject custom fields into the Form as it's being loaded. This is triggered during the Model initialization, so it should occur during administrative functions like editing the article. As long as you are storing your custom attributes either in attribs or metadata, they should be available to your front-end.

Re: Overriding Form Field XML?

Posted: Tue Nov 08, 2011 6:59 pm
by topperharley122
davidbalt wrote:The approach I am taking is to use the onContentPrepareForm event to inject custom fields into the Form as it's being loaded. This is triggered during the Model initialization, so it should occur during administrative functions like editing the article. As long as you are storing your custom attributes either in attribs or metadata, they should be available to your front-end.
I did some searching to try and figure out exactly how you did this and I found this official Joomla tutorial which actually shows how to write a simple profile plugin using the same approach:

http://docs.joomla.org/Creating_a_profile_plugin

Re: Overriding Form Field XML?

Posted: Sun Nov 20, 2011 10:10 am
by bornakke
I had a lot of trouble getting the 'onContentPrepareForm' approach to work. Fx. it seems that the
$form->loadFile has changed to
$form->load();

I finaly got my fields in, but couldn't make it save any of them.

However the solution GavinGrup showed directly out of the box. Thanks! This was the last joomla part which we haven't been able to override :)

Re: Overriding Form Field XML?

Posted: Mon Nov 21, 2011 6:31 pm
by davidbalt
You're correct that saving is a challenge. I gave myself the limitation that I would not modify any system files nor would I change what is stored in the system tables. I wanted to write a new component that could be installed and uninstalled without contaminating the rest of the system, but would still add additional metadata to articles. My onContentPrepareForm approach has this advantage. I define an XML form that extends the com_content default form using a plugin, the load() method mentioned elsewhere. I then have a validation rule on that form field. This validation rule is actually responsible for all of the logic, including saving the information to the database. It's backwards and awkward, but nothing I do touches any existing files and it's all totally atomic, both in terms of files and in terms of database information.

Re: Overriding Form Field XML?

Posted: Wed Dec 14, 2011 9:59 pm
by aldoyh
very useful, I used the onContentPrepareForm method and it works better than including the intended xml.
Works on 1.7.3 perfectly so far.

Thank you.

Re: Overriding Form Field XML?

Posted: Tue Feb 07, 2012 12:41 pm
by kompozitpanel
http://docs.joomla.org/Creating_a_profile_plugin[/quote]


usefull content , i got it thanks

Re: Overriding Form Field XML?

Posted: Wed Apr 18, 2012 6:17 am
by hbmarar
@gavingrubb, @dylanjh
Thanks to both of you.

"gavingrubb" solution worked wonderfully well on my home machine but when I tried the same on my workstation at office, it didnt. After trying on a fresh installation and not finding it work , thought about trying the same following "dylanjh" approach.

"gavingrubb" mentioning about form data not loaded is true but the below worked :-

Code: Select all

  //$this->form->reset( true ); // to reset the form xml loaded by the view
    $this->form->loadFile( dirname(__FILE__) . DS . "article.xml");
The only issue is that its not able to save the custom field to database. I request some advice on this. thanks in advance

Regards

hbmarar

Re: Overriding Form Field XML?

Posted: Sun Dec 29, 2013 12:17 pm
by valouxxx
Hi there!

I'm also trying to override the 'administrator/components/models/forms/article.xml' by folowing the gavingrubb method.

But I didn't how to modify the edit.php file....

Someone could tell me how to add the custum fields in it?

Thanks a lot!

Re: Overriding Form Field XML?

Posted: Wed Jan 08, 2014 12:16 pm
by valouxxx
Up ;)