Overriding Form Field XML?

Everything to do with Joomla! 2.5 templates and templating.

Moderator: General Support Moderators

Forum rules
Forum Rules
Absolute Beginner's Guide to Joomla! <-- please read before posting, this means YOU.
Forum Post Assistant - If you are serious about wanting help, you will use this tool to help you post.
Locked
bizzynate
Joomla! Fledgling
Joomla! Fledgling
Posts: 2
Joined: Fri May 19, 2006 3:45 am

Overriding Form Field XML?

Post by bizzynate » Thu Jan 13, 2011 12:00 am

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!

User avatar
dylanjh
Joomla! Ace
Joomla! Ace
Posts: 1823
Joined: Fri Sep 22, 2006 6:22 pm
Location: UK
Contact:

Re: Overriding Form Field XML?

Post by dylanjh » Thu Jan 13, 2011 10:13 pm

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
EmailAsUsername - Remove Usernames Joomla! Virtuemart And JomSocial registration http://www.lunarhotel.co.uk Many other extensions supported.

User avatar
dylanjh
Joomla! Ace
Joomla! Ace
Posts: 1823
Joined: Fri Sep 22, 2006 6:22 pm
Location: UK
Contact:

Re: Overriding Form Field XML?

Post by dylanjh » Thu Jan 13, 2011 10:17 pm

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.
EmailAsUsername - Remove Usernames Joomla! Virtuemart And JomSocial registration http://www.lunarhotel.co.uk Many other extensions supported.

User avatar
dylanjh
Joomla! Ace
Joomla! Ace
Posts: 1823
Joined: Fri Sep 22, 2006 6:22 pm
Location: UK
Contact:

Re: Overriding Form Field XML?

Post by dylanjh » Thu Jan 13, 2011 10:21 pm

doesnt work putting into /templates/<template name>/xml/com_user/registration either... hmmmm....
EmailAsUsername - Remove Usernames Joomla! Virtuemart And JomSocial registration http://www.lunarhotel.co.uk Many other extensions supported.

User avatar
dylanjh
Joomla! Ace
Joomla! Ace
Posts: 1823
Joined: Fri Sep 22, 2006 6:22 pm
Location: UK
Contact:

Re: Overriding Form Field XML?

Post by dylanjh » Fri Jan 14, 2011 8:09 am

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
EmailAsUsername - Remove Usernames Joomla! Virtuemart And JomSocial registration http://www.lunarhotel.co.uk Many other extensions supported.

daniel_jackson2
Joomla! Enthusiast
Joomla! Enthusiast
Posts: 175
Joined: Wed Mar 03, 2010 7:58 pm

Re: Overriding Form Field XML?

Post by daniel_jackson2 » Wed Jan 19, 2011 8:59 pm

@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.

gavingrubb
Joomla! Apprentice
Joomla! Apprentice
Posts: 9
Joined: Sun Feb 24, 2008 4:47 am

Re: Overriding Form Field XML?

Post by gavingrubb » Fri Mar 25, 2011 1:40 am

"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)

davidbalt
Joomla! Fledgling
Joomla! Fledgling
Posts: 2
Joined: Mon May 09, 2011 6:41 pm

Re: Overriding Form Field XML?

Post by davidbalt » Mon May 09, 2011 6:48 pm

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.

topperharley122
Joomla! Intern
Joomla! Intern
Posts: 52
Joined: Tue Dec 29, 2009 5:35 am

Re: Overriding Form Field XML?

Post by topperharley122 » Tue Nov 08, 2011 6:59 pm

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

bornakke
Joomla! Apprentice
Joomla! Apprentice
Posts: 28
Joined: Tue Jan 22, 2008 8:09 pm

Re: Overriding Form Field XML?

Post by bornakke » Sun Nov 20, 2011 10:10 am

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 :)

davidbalt
Joomla! Fledgling
Joomla! Fledgling
Posts: 2
Joined: Mon May 09, 2011 6:41 pm

Re: Overriding Form Field XML?

Post by davidbalt » Mon Nov 21, 2011 6:31 pm

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.

aldoyh
Joomla! Apprentice
Joomla! Apprentice
Posts: 7
Joined: Sat Jun 25, 2011 12:25 am

Re: Overriding Form Field XML?

Post by aldoyh » Wed Dec 14, 2011 9:59 pm

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.

kompozitpanel
Joomla! Apprentice
Joomla! Apprentice
Posts: 9
Joined: Tue Feb 07, 2012 12:20 pm
Location: istanbul
Contact:

Re: Overriding Form Field XML?

Post by kompozitpanel » Tue Feb 07, 2012 12:41 pm

http://docs.joomla.org/Creating_a_profile_plugin[/quote]


usefull content , i got it thanks

User avatar
hbmarar
Joomla! Enthusiast
Joomla! Enthusiast
Posts: 213
Joined: Mon Oct 10, 2005 7:09 am
Location: Payyannur, Kannur, Kerala,India
Contact:

Re: Overriding Form Field XML?

Post by hbmarar » Wed Apr 18, 2012 6:17 am

@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
Technology to Solutions ( Skype : holy.geek_saffrongeek )
Given enough eyeballs , All bugs are shallow.
Collecting data is only the first step toward wisdom. But sharing data is the first step toward community

valouxxx
Joomla! Fledgling
Joomla! Fledgling
Posts: 2
Joined: Sun Dec 29, 2013 12:14 pm

Re: Overriding Form Field XML?

Post by valouxxx » Sun Dec 29, 2013 12:17 pm

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!

valouxxx
Joomla! Fledgling
Joomla! Fledgling
Posts: 2
Joined: Sun Dec 29, 2013 12:14 pm

Re: Overriding Form Field XML?

Post by valouxxx » Wed Jan 08, 2014 12:16 pm

Up ;)


Locked

Return to “Templates for Joomla! 2.5”