J3.x:Developing an MVC Component/Adding a Feed
From Joomla! Documentation
Articles in This Series
- Introduction
- Developing a Basic Component
- Adding a View to the Site Part
- Adding a Menu Type to the Site Part
- Adding a Model to the Site Part
- Adding a Variable Request in the Menu Type
- Using the Database
- Basic Backend
- Adding Language Management
- Adding Backend Actions
- Adding Decorations to the Backend
- Adding Verifications
- Adding Categories
- Adding Configuration
- Adding ACL
- Adding an Install/Uninstall/Update Script File
- Adding a Frontend Form
- Adding an Image
- Adding a Map
- Adding AJAX
- Adding an Alias
- Using the Language Filter Facility
- Adding a Modal
- Adding Associations
- Adding Checkout
- Adding Ordering
- Adding Levels
- Adding Versioning
- Adding Tags
- Adding Access
- Adding a Batch Process
- Adding Cache
- Adding a Feed
- Adding an Update Server
- Adding Custom Fields
- Upgrading to Joomla4
This is a multiple-article series of tutorials on how to develop a Model-View-Controller Component for Joomla! Version
.
Begin with the Introduction, and navigate the articles in this series by using the navigation button at the bottom or the box to the right (the Articles in this series).
This tutorial is part of the Developing an MVC Component for Joomla! 3.2 tutorial. You are encouraged to read the previous parts of the tutorial before reading this.
In this step we add a feed to our component.
Under Construction!
Introduction
If you're not familiar with feeds then it's worth doing some background reading on the Internet. Joomla provides the capability of generating a feed of featured articles, or articles in a category – see for example this tutorial.
On the Joomla website setting up a feed associated with an article category involves
- creating a menuitem pointing to a webpage showing articles in a given category
- enabling the Syndication Feed module and displaying it on that menuitem.
This module displays an RSS symbol on the webpage which has behind it a link to the URL of the RSS or Atom feed associated with that article category.
To subscribe to the feed a user copies this URL into his or her feed aggregator. When the feed aggregator sends an HTTP request to this URL, then the Joomla website returns an XML document containing details of the articles in that category – their titles, descriptions, published dates, etc.
So for example, if the webpage displays a list of the articles associated with a category "technology" then the URL behind the RSS image will point to the associated feed. When a browser or feed aggregator sends an HTTP request to that feed URL then the server will return an XML document in RSS or Atom format containing details of those articles with a category of "technology".
In this way a feed aggregator can send HTTP requests to all of the feeds to which the user has subscribed, and collate them so that user doesn't have to visit each of the individual news websites. And the user can configure the news aggregator to check the feeds at specific times, so that the information can been already collected and collated by the time the user wishes to view it.
Note that in addition to the syndicated feed which we're considering here, Joomla also has a newsfeed capability, which enables you to display a webpage showing information collected from syndicated feeds elsewhere – similar to what a feed aggregator would do – but this isn't the functionality which this tutorial step is about.
Approach
We currently have a site category view which displays the helloworld records associated with a given category. In this step we add an RSS syndicated feed to this page. This will result in an RSS symbol shown as an image, with a URL behind which will give an XML feed in RSS or Atom format of the helloworld records associated with that category.
If you haven't already got a site menuitem which points to a category view then you should configure one as a starting point.
To enable the syndicated feed we must do the following.
- Under Modules, find and enable the Syndicated Feed module. Click on this module to edit its options, and under the Menu Assignment tab ensure that it's shown just on the page which displays the helloworld category view. Note that among the options there is one which defines whether Joomla presents the XML data in the RSS or Atom format.
- Define a helloworld configuration parameter which controls whether the RSS symbol with its link is shown or not. The Joomla code displays the RSS symbol only if it finds this parameter and it's set to Show.
- In the category view.html.php we need to inherit from JViewCategory (which in the revised namespacing scheme is CategoryView in namespace Joomla\CMS\MVC\View) and call the addFeed() method of this class. This method inserts the RSS/Atom links into the document <head> – which is where the Syndicated Feed module expects to find the URL for putting behind the RSS symbol.
- The URL for the RSS feed includes the query parameter "format=feed" so we need to have a file view.feed.php and the code for this new file is found below. Note that it inherits from the class JViewCategoryfeed, or in the newer namespaced structure CategoryFeedView within the namespace Joomla\CMS\MVC\View.
- Include in the SQL query within the helloworld category model all the fields which you want to populate within the RSS/Atom feed XML document.
The URL of the RSS feed will be something like: http://yourDomain.com/yourJoomlaInstance/index.php?option=com_helloworld&view=category&id=37&Itemid=822&lang=en&format=feed&type=rss When Joomla receives a URL like this, it handles it in the same way as usual:
- the option is "com_helloworld", so it will run our helloworld.php file
- it will run the display() method within the helloworld controller – this will as usual set up the view and the model
- because the URL parameter format = "feed" it will expect the view to be in ./views/category/view.feed.html and will run the HelloworldViewCategory::display() method.
- this display() code is found from the parent class JViewCategoryfeed / CategoryFeedView. This is the code which brings together the data to be put into the response XML document – for example, the titles, descriptions, links – mapping them from the fields in the SQL query result, and I would recommend looking through the code to get an idea of what it does.
- it calls $this->get('Items'), which gets translated into a call getItems() in the model – so we need to ensure that our SQL query in this function provides all the fields we want to be present in the RSS XML response.
- it calls $this->get('Category'), so we need to have a method getCategory() in our model
- it maps some of the fields using the ucm content-types field mapping data which we supplied in the step Adding Tags. To find the correct record in this table it uses the key formed by the extension ("com_helloworld") and the view ("helloworld"), so this is why we need to set the $viewname property in this class.
- it allows us to define mappings of other fields through the method reconcileNames(). We use this to include in the description field a link to the associated image.
Configuration changes
View changes
Model changes
Updated Language Strings
Packaging the Component
Contents of your code directory. Each file link below takes you to the step in the tutorial which has the latest version of that source code file.
- helloworld.xml
- script.php
- site/router.php
- site/helloworld.php
- site/index.html
- site/controller.php
- site/controllers/helloworld.php
- site/views/index.html
- site/views/helloworld/index.html
- site/views/helloworld/view.html.php
- site/views/helloworld/view.json.php
- site/views/helloworld/tmpl/index.html
- site/views/helloworld/tmpl/default.xml
- site/views/helloworld/tmpl/default.php
- site/views/form/index.html
- site/views/form/view.html.php
- site/views/form/tmpl/index.html
- site/views/form/tmpl/edit.php
- site/views/form/tmpl/edit.xml
- site/views/category/index.html
- site/views/category/view.html.php
- site/views/category/tmpl/index.html
- site/views/category/tmpl/default.php
- site/views/category/tmpl/default.xml
- site/models/index.html
- site/models/helloworld.php
- site/models/form.php
- site/models/category.php
- site/models/forms/index.html
- site/models/forms/add-form.xml
- site/models/forms/filter_category.xml
- site/language/index.html
- site/language/en-GB/index.html
- site/language/en-GB/en-GB.com_helloworld.ini
- site/helpers/index.html
- site/helpers/route.php
- site/helpers/category.php
- site/helpers/association.php
- admin/index.html
- admin/helloworld.php
- admin/config.xml
- admin/controller.php
- admin/access.xml
- admin/helpers/helloworld.php
- admin/helpers/associations.php
- admin/helpers/index.html
- admin/helpers/html/helloworlds.php
- admin/helpers/html/index.html
- admin/sql/index.html
- admin/sql/install.mysql.utf8.sql
- admin/sql/uninstall.mysql.utf8.sql
- admin/sql/updates/index.html
- admin/sql/updates/mysql/index.html
- admin/sql/updates/mysql/0.0.1.sql
- admin/sql/updates/mysql/0.0.6.sql
- admin/sql/updates/mysql/0.0.12.sql
- admin/sql/updates/mysql/0.0.13.sql
- admin/sql/updates/mysql/0.0.14.sql
- admin/sql/updates/mysql/0.0.16.sql
- admin/sql/updates/mysql/0.0.17.sql
- admin/sql/updates/mysql/0.0.18.sql
- admin/sql/updates/mysql/0.0.20.sql
- admin/sql/updates/mysql/0.0.21.sql
- admin/sql/updates/mysql/0.0.24.sql
- admin/sql/updates/mysql/0.0.25.sql
- admin/sql/updates/mysql/0.0.26.sql
- admin/sql/updates/mysql/0.0.27.sql
- admin/sql/updates/mysql/0.0.28.sql
- admin/sql/updates/mysql/0.0.29.sql
- admin/models/index.html
- admin/models/fields/index.html
- admin/models/fields/helloworld.php
- admin/models/fields/helloworldordering.php
- admin/models/fields/helloworldparent.php
- admin/models/fields/modal/index.html
- admin/models/fields/modal/helloworld.php
- admin/models/helloworlds.php
- admin/models/helloworld.php
- admin/models/forms/filter_helloworlds.xml
- admin/models/forms/index.html
- admin/models/forms/helloworld.js
- admin/models/forms/helloworld.xml
- admin/models/rules/greeting.php
- admin/models/rules/index.html
- admin/controllers/helloworld.php
- admin/controllers/helloworlds.php
- admin/controllers/index.html
- admin/views/index.html
- admin/views/helloworld/index.html
- admin/views/helloworld/view.html.php
- admin/views/helloworld/tmpl/index.html
- admin/views/helloworld/tmpl/edit.php
- admin/views/helloworld/submitbutton.js
- admin/views/helloworlds/index.html
- admin/views/helloworlds/view.html.php
- admin/views/helloworlds/tmpl/index.html
- admin/views/helloworlds/tmpl/default.php
- admin/views/helloworlds/tmpl/default_batch_body.php
- admin/views/helloworlds/tmpl/default_batch_footer.php
- admin/views/helloworlds/tmpl/modal.php
- admin/layouts/index.html
- admin/layouts/position.php
- admin/tables/index.html
- admin/tables/helloworld.php
- admin/language/index.html
- admin/language/en-GB/index.html
- admin/language/en-GB/en-GB.com_helloworld.ini
- admin/language/en-GB/en-GB.com_helloworld.sys.ini
- media/index.html
- media/images/index.html
- media/images/tux-16x16.png
- media/images/tux-48x48.png
- media/js/index.html
- media/js/openstreetmap.js
- media/js/admin-helloworlds-modal.js
- media/css/index.html
- media/css/openstreetmap.css
helloworld.xml
<?xml version="1.0" encoding="utf-8"?>
<extension type="component" version="3.0" method="upgrade">
<name>COM_HELLOWORLD</name>
<!-- The following elements are optional and free of formatting constraints -->
<creationDate>January 2018</creationDate>
<author>John Doe</author>
<authorEmail>john.doe@example.org</authorEmail>
<authorUrl>http://www.example.org</authorUrl>
<copyright>Copyright Info</copyright>
<license>License Info</license>
<!-- The version string is recorded in the components table -->
<version>0.0.32</version>
<!-- The description is optional and defaults to the name -->
<description>COM_HELLOWORLD_DESCRIPTION</description>
<!-- Runs on install/uninstall/update; New in 2.5 -->
<scriptfile>script.php</scriptfile>
<install> <!-- Runs on install -->
<sql>
<file driver="mysql" charset="utf8">sql/install.mysql.utf8.sql</file>
</sql>
</install>
<uninstall> <!-- Runs on uninstall -->
<sql>
<file driver="mysql" charset="utf8">sql/uninstall.mysql.utf8.sql</file>
</sql>
</uninstall>
<update> <!-- Runs on update; New since J2.5 -->
<schemas>
<schemapath type="mysql">sql/updates/mysql</schemapath>
</schemas>
</update>
<!-- Site Main File Copy Section -->
<!-- Note the folder attribute: This attribute describes the folder
to copy FROM in the package to install therefore files copied
in this section are copied from /site/ in the package -->
<files folder="site">
<filename>index.html</filename>
<filename>helloworld.php</filename>
<filename>controller.php</filename>
<filename>router.php</filename>
<folder>controllers</folder>
<folder>views</folder>
<folder>models</folder>
<folder>helpers</folder>
</files>
<languages folder="site/language">
<language tag="en-GB">en-GB/en-GB.com_helloworld.ini</language>
<language tag="fr-FR">fr-FR/fr-FR.com_helloworld.ini</language>
</languages>
<media destination="com_helloworld" folder="media">
<filename>index.html</filename>
<folder>images</folder>
<folder>js</folder>
<folder>css</folder>
</media>
<administration>
<!-- Administration Menu Section -->
<menu link='index.php?option=com_helloworld' img="../media/com_helloworld/images/tux-16x16.png">COM_HELLOWORLD_MENU</menu>
<!-- Administration Main File Copy Section -->
<!-- Note the folder attribute: This attribute describes the folder
to copy FROM in the package to install therefore files copied
in this section are copied from /admin/ in the package -->
<files folder="admin">
<!-- Admin Main File Copy Section -->
<filename>index.html</filename>
<filename>config.xml</filename>
<filename>helloworld.php</filename>
<filename>controller.php</filename>
<filename>access.xml</filename>
<!-- SQL files section -->
<folder>sql</folder>
<!-- tables files section -->
<folder>tables</folder>
<!-- models files section -->
<folder>models</folder>
<!-- views files section -->
<folder>views</folder>
<!-- controllers files section -->
<folder>controllers</folder>
<!-- helpers files section -->
<folder>helpers</folder>
<!-- layout files section -->
<folder>layouts</folder>
</files>
<languages folder="admin/language">
<language tag="en-GB">en-GB/en-GB.com_helloworld.ini</language>
<language tag="en-GB">en-GB/en-GB.com_helloworld.sys.ini</language>
<language tag="fr-FR">fr-FR/fr-FR.com_helloworld.ini</language>
<language tag="fr-FR">fr-FR/fr-FR.com_helloworld.sys.ini</language>
</languages>
</administration>
</extension>