Joomla! 1.7 Highlight - Batch Processing

Written by | 01 July 2011 | Published in 2011 July
Elin Waring recently blogged on the Joomla! Community Portal about some of the features coming with Joomla! 1.7, appropriately titled Peeking at 1.7. Aside from the separation of the framework and CMS, I think (and I may be a bit biased) one of the better changes is the ability to batch process items being added to the framework. A popular feature excluded from Joomla! 1.6, its return for 1.7 marks benefit for users and developers alike.

Back Story

Getting batch processing into the core for 1.6 and 1.7 hasn't been an easy task. At the first beta release of 1.6, only the Menu Manager had it in the items view. After one tracker item, the methods were carried over to the Category Manager. The place that it was most asked for, the Article Manager, just couldn't get the same treatment in a timely manner and it was decided that this feature wasn't important enough to block the release of 1.6. Fast forward a few months, and Dennis Hermatski submitted a patch to batch process articles. It was tested and implemented into the development head, which was moving away from the 1.6 series and beginning to take shape for the forthcoming 1.7 release. Upon seeing this commit, I remembered the lack of batch processing being available for the core and began testing Dennis' code on other core components. I realized that Dennis had not only coded a solution for the Article Manager, but the code was easily reusable throughout the CMS.

Into the Framework

I quickly requested an SVN branch to begin incorporating this feature into the framework to make it available for the core components and all third party developers. The first task was to move the code out of ContentControllerArticle and ContentModelArticle into the methods that they had overridden, JControllerForm and JModelAdmin (respectively). The model functions were a simple move which required only changing hard coded com_content references into an $extension variable that would act based on which component was using the code at the time. The controller method took a little bit of outside thinking because each controller sets a redirect for after the process is complete. Instead of each controller duplicating all the batch function code, I decided instead to move the common code into JControllerForm with each controller presetting the actionable model and the redirect before feeding into the parent function.

Once all the code was moved, I began reviewing the existing code for com_categories and com_menus to determine how much of this extended code could be deleted. I found several lines that were deletable, including making the controller methods follow my boilerplate code. As a further demonstration of the implementation, batch processing was added to com_weblinks as well.

Two additional patches after this feature was originally implemented cleans up the code a bit more. Both resolve small bugs and make the interface more consistent. The first patch fixed a bug where "Save as Copy" when editing Articles would fail because the title and alias weren't made unique. The second fixes a bug in the batch processing for the Category Manager wouldn't do the same uniqueness checking. This is processed through a function added to JModelAdmin called generateNewTitle, which is too reusable by developers.

So How Do I Add Batch Processing?

If your extension follows a category -> item structure, and you're dying to add batch processing to your extensions and release the new versions tomorrow, you can simply grab the methods from JControllerForm and JModelAdmin in the current trunk and push them into your own extension's code, or you can wait for 1.7 to release and add a simple boilerplate function to your single item's controller (which should be extending JControllerForm). Here's the snippet from ContentControllerArticle:

/**
* Method to run batch operations.
*
* @return void
* @since 1.7
*/
public function batch($model)
{
JRequest::checkToken() or jexit(JText::_('JINVALID_TOKEN'));

// Set the model
$model = $this->getModel('Article', '', array());

// Preset the redirect
$this->setRedirect(JRoute::_('index.php?option=com_content&view=articles'.$this->getRedirectToListAppend(), false));

return parent::batch($model);
}

Very easy, isn't it?

The difficult part is if your extension doesn't follow this structure, in which case you'll need to extend those functions from JControllerForm and JModelAdmin. CategoriesModelCategory has a good example of extended functions where items are stored in a nested tree.

Joomla! 1.7 is just around the corner. Are you ready for it?

Read 22113 times Tagged under Did you know...?
Michael Babker

Michael Babker

Michael is an active member of the Joomla! community, participating in the Bug Squad, contributing code to the CMS and Framework, and is currently a member of the Production Leadership Team.

His contributions to the community have been recognized with the 2012 J!OSCAR "GitHub Junkie" Award. His high standards for code and focus on automated testing have raised the overall quality of Joomla's code and helps ensure a stable and quality product for every Joomla user.

When he isn't working on core code, he can be found working on his own set of extensions, opinionating on Twitter, or out in the real world enjoying the sights and sounds of wherever he may be at the time.