J3.x

Developing a component frontend update function/Improved export function

From Joomla! Documentation

< J3.x:Developing a component frontend update function
Joomla! 
3.x
Tutorial
Developing a component frontend update function


21 Improved csv export[edit]

21.1 Description[edit]

As explained in the previous chapters, Joomla usually displays data in html format. However, it does support other formats as well, including the 'raw' format meaning that in that case Joomla does not perform any layout operations at all for the data.

That is exactly the way to go for downloading an export file as generally we have to assume that such file just contains 'binary' data.

21.2 The changes[edit]

21.2.1 Files:[edit]

  • README.md
  • helloworld.xml
  • admin/button/index.html

Again, have been updated to reflect version info. A new directory admin/button is created in this chapter, and therefore an additional admin/button/index.html placeholder file as well. helloworld.xml is updated accordingly.

21.2.2 admin/button/rawformat.php[edit]

In Joomla processing of a page begins with starting the concerning controller. To indicate that processing should yield raw data rather than an html page, the controller must receive the 'format=raw' input parameter. These parameter is typically provided by adding an hidden input tag to the form that starts the controller. Like:

<form action="index.php?option=com_helloworld" method="post" name="adminForm">
...
<input type="hidden" name="format" value="raw"/>
</form>

The HelloWorld database items are handled from the HelloWorld admin page with form:

admin/components/com_helloworld/views/helloworlds/tmpl/default.php

or its frontend counterpart. The available functions like create new item, delete an item or update an item are offered to the user as buttons above this form. Our export button is here as well.

Pushing a button typically navigates to another web form, like the edit form to update a particular item. These forms are in html format, so including the 'format=raw' input tag permanently to the above form definition file tmpl/default.php would cause a conflict for these functions.

So only pushing our export button should submit the 'format=raw' tag. As this tag is not a standard part of the form, the button function will add the tag on the fly as post variable. Joomla has no standard button function that allows this, so we define an additional button called 'RawFormat'. It is defined in admin/button/rawformat.php and basically just clones the Joomla 'Standard' button replacing the reference to the 'Joomla.submitbutton' javascript by a reference to the 'RawFormatSubmitbutton' javascript. This javascript submits (posts) the form data to the server, and this will schedule the controller.

21.2.3 admin/views/helloworlds/submitbutton.js[edit]

This javascript injects the tag '<input type="hidden" name="format" value="raw"/>' in the form. Thereafter it submits the form using the 'Joomla.submitform(task)' call, just like the Joomla 'Standard' button would do. The input tag is reset to html format after this call for future form submissions.

21.2.4 admin/views/helloworlds/view.html.php[edit]

This php file prepares the display of the HelloWorld management page. It is adapted to include the above javascript 21.2.3 and display our new 'RawFormat' export button in stead of the previous one.

21.2.5 admin/controllers/helloexport.raw.php[edit]

This is almost the same file as the previous 'admin/controllers/helloexport.php' except that its name has been changed in view of Joomla rules. Joomla expects a controller for raw format to have a filename <controller>.raw.php, although the class name is still <component>Controller<controller>, so 'HelloWorldControllerHelloExport' in this case.

The big difference with the previous controller version is that the application does not need to be forcibly closed as Joomla now understands from the 'format=raw' tag that the controller yields a raw format layout and embedding in html is not required anymore.

The final statements

$app->sendHeaders();
$app->close();

therefore have been removed as calling these functions will now be performed by Joomla in the standard way when the controller finishes the 'exportcsv' function.

21.3 Source files for this step[edit]

21.4 Testing the features of this step[edit]

  • Perform the same test as in the previous section 20.4 , and verify that the export works as it did before.