Gavern Framework – An alternative to the plugins

Last Updated:
Category:
Gavern Framework

One of the major benefits of the Gavern Framework is the simplified manner by which code fragments can be created which work in a fashion similar to typical plugins during the onAfterRender event. This article demonstrates how to write a simple plugin.

An advantage to this approach is the ability we’ve introduced to create simple parameters for overridden template modules and components. As an example, this article steps through creation of code to hide the titles of selected articles (the categories can be chosen at the template options level).

The Goal

Headers can be hidden by placing them between the typical markers; after such conditions have been fulfilled – in this case opening the selected entries category – it becomes possible to remove a page fragment. If the condition will not be fulfillled, the script must remove the markers so as not to clutter the page’s code. Selection of the category is accomplished by inserting the category field into the templateDetails file.

Implementation

This article assumes headers remain hidden between the <gavern:hide> and </gavern:hide> markers for the selected category.

Markers are placed in the page code located in the file components/com_content/views/category/tmpl/blog_item.php.

In the event we have overridden this file in a template, the overridden version is the one which must be affected. It will be the file located at html/com_content/category/blog_item.php/ within the catalog implemented with the template.

The file begins as follows:

<?php if ($params->get('show_title')) : ?> <h2> <?php if ($params->get('link_titles') && $params->get('access-view')) : ?> <a href="/<?php echo JRoute::_(ContentHelperRoute::getArticleRoute($this->item->slug, $this->item->catid)); ?>"> <?php echo $this->escape($this->item->title); ?></a> <?php else : ?> <?php echo $this->escape($this->item->title); ?> <?php endif; ?> </h2> <?php endif; ?>

It must be changed to this:

<?php if ($params->get('show_title')) : ?> <gavern:hide> <h2> <?php if ($params->get('link_titles') && $params->get('access-view')) : ?> <a href="/<?php echo JRoute::_(ContentHelperRoute::getArticleRoute($this->item->slug, $this->item->catid)); ?>"> <?php echo $this->escape($this->item->title); ?></a> <?php else : ?> <?php echo $this->escape($this->item->title); ?> <?php endif; ?> </h2> </gavern:hide> <?php endif; ?>

The necessary markers have now been generated. Next, an option to select a category must be added. Within the templateDetails.xml file the following element must be inserted:

<field name="hidden_headers" type="category" extension="com_content" default="" multiple="multiple" label="Hidden headers" description="Please select categories with hidden headers" />

The following element will generate a multiple choice list with categories. A few categories can be instantly be marked in a panel in which headers will disappear.

The final bit involves PHP code. The file gk.framework.php is opened for editing and the hideHeaders method is created at the end of the GKTemplate class, as demonstrated in the following sample code fragment:

public function hideHeaders() { $hiddenHeaders = explode(',', $this->getParam('hidden_headers', '')); // (1) $option = JRequest::getCmd('option', ''); // (2) $view = JRequest::getCmd('view', ''); // (2) $catid = JRequest::getVar('catid', ''); // (2) if($option == 'com_content' && $view == 'category' && in_array($catid, $hidden_headers, false)) { // (3) GKParser::$customRules['/<gavern:hide(.*?)gavern:hide>/mis'] = ''; // (4) } else { GKParser::$customRules['/<gavern:hide>/mis'] = ''; // (5) GKParser::$customRules['/<\/gavern:hide>/mis'] = ''; // (5) } }

What have we done using the method code? Glad you asked!

First, we’re downloading the IDs for the categories previously selected. Then we’re downloading parameter values from the address derived for the component name, view and category ID using the JRequest class.

Later, we check on a condition – whether com_content is the desired component. We also check on the category view and whether the category ID is placed in a category group by a user.

If the condition is fulfilled, we add a replacement rule to the customRules associative table GKParser class. This rule is a regular expression which replaces a text suitable to a given expression with the text assigned to this expression – in this case, it is an empty sequence of symbols.

If the condition is not fulfilled, we remove the markers by adding two replacement rules which replace the opening and closing markers to an empty sequence of symbols.

To make it work, we must add our method request to the GKTemplate class constructor. It is best to insert this line at the end of the constructor:

$this->hideHeaders();

Summary

This is obviously a simple example of how to use our solution. Interesting examples may be found in the Gavern Framework code itself by analyzing the Social API operation or replacing views of the selected com_content component for mobile browsers.

To create such plugins, some creativity and a knowledge of regular expressions would be helpful.

Gavern Framework – An alternative to the plugins 5.005 (100.00%) 2 votes

This article was first published

Villa Belluci - View our NEW theme
×

Tutorials & tips delivered regularly

Expand your purchase with regular tutorials and tips to making your site better, direct to your email.