defined('_JEXEC') or die;
public static function addSubmenu($vName = '')
$vName == 'breeds'
Joomla allows you to set a submenu by using a helper class as shown in the above code. First you have to declare a helper class using component name, without com_. In this class, we have simply created a static method called addSubmenu and then have called addEntry method of JHtmlSidebar class and passed values like the url, submenu name and view name.
In the main controller.php file, we are going to include the helper file in the display method, so that it could be accessed throughout our component. Note that we are including it here by using require_once and not the joomla JLoader::import() method. The reason for this is, this method tries to load a file using include_once but we need here “require_once” because we want our component to stop working when the system is unable to load our helper file. But in future project, you can use any of these depending on the requirement. You can also use just import() but keep in mind that JLoader::register() will not work here as it only registers or prepares the file for auto loading.
The first change, we have made in this model is, we have first created the populateState() method, like we have created while implementing search feature in the frontend, to hold the state of the variables, involved in the searching and filtering process. To hold the Joomla application object, first we have initialized a variable, called $app in this method.
From line 39 to 45, we are setting and stroing the starte of the form variables including search, state and breed category. Search string is the value it gets from the search field, when user tries to search something, publish and preedcat are the values from filter_publish and filter_breedcat dropdown filter fields. The values of all of these are extracted by getUserStateFromRequest() method and are then set and temporarily stored in the model state through setstate() method.
Then from line 53 to 59, through getStoreId method, we are trying to get the ids from the search field and state or publish field. If that string is an interger, it will add “id:” to it. Next in this step of tutorial, I will show you why it is done and what is its benefit, when we will be building our query.
// Filter by published state
$published = $this->getState('filter.state');
$query->where('a.state = ' . (int) $published);
elseif ($published === '')
$query->where('(a.state IN (0, 1))');
// Filter by search in title
$search = $this->getState('filter.search');
if (stripos($search, 'id:') === 0)
$query->where('a.id = ' . (int) substr($search, 3));
$search = $db->Quote('%' . $db->escape($search, true) . '%');
$query->where('( a.`breedname` LIKE ' . $search . ' )');
$filter_breedcat = $this->state->get("filter.breedcat");
$query->where("a.`breedcat` = '".$db->escape($filter_breedcat)."'");
To implement the search feature, first we have to prepare the proper query, so that it can take a value to be searched and it should know, from which column it should check.From line 100 to 113, we have added the search query code. On line 100, we are getting the search value from the model state. On line 102, we are making a not empty check on search variable, so that a search could be made on the database table. Next we are preparing the search string, so that it could go to database query through escape() method which will made search term ready to go to query. This function prohibits any command to be run through search field on database. We are wrapping this string in “%”, means, match the string and anything before and after it. After this, on line 120 we are adding this to where clause to make it the part of our main query.
In this part of our Joomla extension development tutorial, we have added some more lines of code to our query, from line 83 to 115. First we have made a WHERE clause for publish state and it will give the results on the basis of the value it gets from getState(‘filter.state’) method. Normally it would be an integer value, either 1 or 0. And if the getstate method has returned an empty result, on line 91, the WHERE clause returns all the results, inspite of what their state is, published or un published, 0 or 1.
Next we are getting the value of search field through get method, and this is the same value we have set in the populatestate method. Now, we are assigning this to a variable $search and on line 97, we have actually started to build the search query. First we are assuming that the search field value is not empty and if it is not empty, we are checking that whether an id number is placed in the search field and the user wants to make a search according to the record id. If this is the case, then, we will create the WHERE clause on the basis of id field. And if this is not the case, i.e, the search field value is a string, then we will create a WHERE clause which would look into the breedname field, to make a search. We have also implemented the escape method to escape any html or php tags, if they are intentionally or by mistake passed to the $search property.
Next on line, 111, we are trying to filter the records on the basis of breed category. We have get and set the value of breed category filter drop down selector in the populate state function and here we are getting this value and passing it to $filter_breedcat property. Then we are making a search on the breedcat database table field on the basis of the value of $filter_breedcat.
There are some new addition to this view.html.php file, as compared to what we have added into this file in the previous step. In this part of our tutorial series, we are trying to add search, filter and sorting feature to table list view, so we are updating this file accordingly. Let me go through the changes we have made here in this file.
On line 33, we are setting the value of the action attribute of filter form. Then we are adding our first filter, that is a most commonly used publish / unpublish filter through JHtmlSidebar::addFilter() method. It is taking the text for the blank option, then the name of the filter field and then getting an array of standard published state filter options and passing its values to select options of the select field in terms of values and text and then we are getting the current state of this field through get method.
After that, from line 46 to 58, we are doing the same thing, but with the breed category filter. First we have created a two dimensional options array to hold the values and text of the breed category filter. When assignment process is field, we are rendering the category filter in the same way, as we have rendered the state filter select field. Then on line 60, we are rendering the sidebar through render() method of JHtmlSidebar class.
Next, from line 66 to 76, we are creating a two dimensional array in the function getSortFields() which is holding the name of table columns and the language strings which would be shown in a dropdown select box, on the basis of which sorting function could be performed on the listing table. We will call this function in our default.php view template file and will then display the array contents in the form of a select field.
Search Box: In the default.php file listed above, the search box code is from line 73 to 92. The code <?php echo $this->escape($this->state->get(‘filter.search’)); ?> will auto populate the search field with search term. After this, we have a buttons for submit and clear the search form field respectively.
Next from line 86 to 92, we have added the code to populate a select box through which we can limit the search results., by calling getLimitBox() method of pagination class.
After that from line 93 to 108, we are displaying a select list for breeds list ordering, either it would be ascending or descending and then from line 109 to 115, we are creating a select list of fields on the basis of which sorting process could occur. We are getting these values from the same array of sort fields, that we have created in view.html.php file here, in this step.
I basically work in the CMS, like Joomla and WordPress and in framework like Laravel and have keen interest in developing mobile apps by utilizing hybrid technology. I also have experience working in AWS technology. In terms of CMS, I give Joomla the most value because I found it so much user freindly and my clients feel so much easy to manage their project in it.
Enjoy best web development services at an affordable price. Looking forward to build a good relationship and serve you better...
My name is Nohman Habib and I am a web developer with over 10 years of experience, programming in Joomla, Wordpress, WHMCS, vTiger and Hybrid Apps. My plan to start codingace.com is to share my experience and expertise with others. Here my basic area of focus is to post tutorials primarily on Joomla development, HTML5, CSS3 and PHP.