How to implement Object/Form in Joomla Framework

93 views
Skip to first unread message

Henk te Sligte

unread,
May 26, 2014, 8:33:08 AM5/26/14
to joomla-dev...@googlegroups.com
Hi everyone,

I am starting some work on a Joomla module that will allow a customer of us (a union) show information about it's members. I don't have much experience with the Joomla Framework but I do have some experience with other frameworks.

However, I'm a little unsure in how to implement the object structure in Joomla. Usually I create my own very basic Model class from which each object inherits a save() and a remove() function and the object is created by a factory. The properties (like username, email address, province, etc) are regular protected properties and they can be accessed by getters and setters.

Now that you know where I'm coming from, I'm very interested how you would implement the following. 
- The member objects have a set of pre-defined properties and I want to be able to add properties without breaking the existing data. (I'm not worried that adding some fields means an update to the current module).
- I want to be able to create a form from each object so each property (or at least most of them) needs to be able to generate a form field. Some are text fields (like Company name), others are one or more of a set of available options (like Province or Interests). I should also be able to create a custom field type (like a map of the country to select a region). 
- The data needs to be stored in the database. That could be in a pre-defined table with each field corresponding to a column or a table with columns like 'member', 'key' and 'value'.
- The member overview page needs to be able to filter the members by it's properties (like all members in a specific province that have these interests).

I hope I explained my idea. I am unsure how to do this in Joomla but I'm guessing the keywords are Joomla\Form, Joomla\Form\Field and Joomla\Model. Should I extend my Member class from Joomla\Form and then have a class Member\Field that extends from Joomla\Form\Field? Or should I extend from Joomla\Model and have a function like createForm() that returns a Joomla\Form? In that case, how would you add the fields? I know the fields need to be instance of SimpleXMLElement so would you store the field configuration in XML? 

I'm very interested in your ideas, I'm not necessarily looking for concrete code fragments but some pseudo code or pointers towards classes or documentation would be awesome. If anything is unclear, please let me know, english is not my first language and I'm in the very beginning of the design of this module.

Thank you in advance!

Greetings,
Henk te Sligte

Eric Fernance

unread,
Jun 10, 2014, 3:18:01 AM6/10/14
to joomla-dev...@googlegroups.com
Hey Henk,

Sounds like you're on the right track.  

If you're looking for some direction there's the Framework sample app here (https://github.com/dbhurley/framework-app) and Andrew Eddie has put together a series of introductory tutorials here - (http://learn.theartofjoomla.com/framework-solutions.html)

However, I'm a little unsure in how to implement the object structure in Joomla. Usually I create my own very basic Model class from which each object inherits a save() and a remove() function and the object is created by a factory. The properties (like username, email address, province, etc) are regular protected properties and they can be accessed by getters and setters.

You can do the same thing in the Framework.  Just create your basic model to extend the Joomla\Model\AbstractModel or Jooma\Model\AbstractDatabaseModel.  

Now that you know where I'm coming from, I'm very interested how you would implement the following. 
- I want to be able to create a form from each object so each property (or at least most of them) needs to be able to generate a form field. Some are text fields (like Company name), others are one or more of a set of available options (like Province or Interests). I should also be able to create a custom field type (like a map of the country to select a region). 

You were spot on.  You would use the Joomla\Form class.  This has a method loadFile that allows you to load a form definition from an xml file.  I just maintain one xml file for each form object I want to display.

You can get more info about how to create the XML file here: (https://github.com/joomla-framework/form/blob/master/README.md)

- The data needs to be stored in the database. That could be in a pre-defined table with each field corresponding to a column or a table with columns like 'member', 'key' and 'value'.

Currently you would probably manage the database interaction in your model.  The Joomla Framework doesn't have JTable the way the CMS does but something similar has been implemented in the sample app (https://github.com/dbhurley/framework-app/blob/master/App/Table/DefaultDatabaseTable.php).  I've been using this in the apps I have been writing.

- The member overview page needs to be able to filter the members by it's properties (like all members in a specific province that have these interests).

I have done this in one of the apps I have used the Framework for. The Framework doesn't have a lot of the helpers that the CMS has for back end tabular data (ie. what you would see in something like the com_content table) but the barebones are there.  You just need to do things like the pagination and the filtering yourself.

Cheers,
Eric.
--
Framework source code: https://github.com/joomla/joomla-framework
Visit http://developer.joomla.org for more information about developing with Joomla!
---
You received this message because you are subscribed to the Google Groups "Joomla! Framework Development" group.
To unsubscribe from this group and stop receiving emails from it, send an email to joomla-dev-frame...@googlegroups.com.
Visit this group at http://groups.google.com/group/joomla-dev-framework.

signature.asc
Reply all
Reply to author
Forward
0 new messages