Macros

Macros allow overriding specific class methods, without having to override the entire class, making them easier to maintain.

Overview

Development & Support
Customizations are not included with support. We provide this information to make it easier for developers to extend the functionality. From time to time we may have some availability for custom work. Get in touch to see if there's an opportunity to work together.

At this time they are available for controllers routing methods, and they can also be used for creating completely new controller methods.

You can use Theme Explorer to find out which controller, and which method, are used on any given page. For form submissions or ajax requests you would need to examine the form or the request and look for the controller and action values, where the action value represents the name of the contorller class method.

Getting Started

Macros are created in the overrides folder, in the same path structure as the original file that the macro is written for. For example, if you are overriding a method for the CategoryController class in categories_controller.php, you create a new empty file with the same name, but suffixed with _macros:

Joomla

templates/
`-- jreviews_overrides/
    `-- controllers/
        `-- categories_controller_macros.php

WordPress

jreviews_overrides/
`-- controllers/
    `-- categories_controller_macros.php

Inside this file, we are going to override the existing category method (function) by creating a macro with the same name, suffixed with _override:

<?php
defined('MVC_FRAMEWORK') or die;

CategoriesController::macro('category_override', function()
{
 	return "Hello World!";
});

Clear Cache & File Registry
Clear the registry in the JReviews admin CP after creating new files in overrides.

The above code overrides the CategoriesController::category method and will output "Hello World" in all category pages.

Creating a macro for category using CategoriesController::macro('category', function() {}) will not work, because the original category method will be called instead of your macro.

Creating New Methods

It's possible to create new methods on existing classes and call them within other methods. For example, the following code will output "Hello World!" in the category page, and it does it by calling a new 'hello' macr, and passing it a $string parameter:

<?php
defined('MVC_FRAMEWORK') or die;

CategoriesController::macro('category_override', function() {
	return $this->hello("World");
});

CategoriesController::macro('hello', function($string) {
	return "Hello {$string}!";
});