Macro Plugin Development Quick Start

In SCHLIX CMS, a macro is a piece of code that is executed before a text is sent for output to render an HTML page. For each of the view calls in an application or block, if the application calls processDataOutputWithMacro(array $data, string $function_name, array $extra_info), then the data, which is an associative array containing information such as id, title, text, etc  will be processed and all macros with Active status will be executed. A macro may or may not have a view template, since it function can be different.

The following is a basic an example of a macro.

namespace Macro;

class ExtensionName extends \SCHLIX\cmsMacro {


    protected static $has_this_macro_been_called;
    /*
     * Run the macro
     * @global \SCHLIX\cmsHTMLPageHeader $HTMLHeader
     * @param array|string $data
     * @param object $caller_object
     * @param string $caller_function
     * @param array $extra_info
     * @return bool
     */
    public function Run(&$data, $caller_object, $caller_function, $extra_info = NULL) {
        global $HTMLHeader;

        // This is just an example macro
        switch ($caller_function)
        {
            case 'viewItemByID':   break;
            case 'viewCategoryByID':  break;
            case 'viewChildCategory':  break;
            case 'viewChildItem': break;
            case 'viewMainPage':  break;
            default:  break;
        }
	// Who's calling this?
        if (is_a($caller_object,'\\SCHLIX\\cmsApplication_Basic'))
        {
            $app_name  = $caller_object->getApplicationName();
            print_r($data); // what is the data that's passing through?
            $sanitized_xss_title = SAFE_HTML($data['title']);
            $HTMLHeader->Add('<meta property="og:title" content="'.$sanitized_xss_title.'" />');
            // ....
            $this->appendAfterArticleOuput($data, 'Test: after article output');

        } else if (is_a($caller_object,'\\SCHLIX\\cmsBlock'))
        {
            // ....
            //print_r($data);

        }
        // This is an example of how to make sure that this macro is only loaded once
        // because macros get called repeatedly if there's a "repeater" list item
        if (static::$has_this_macro_been_called != 'yes')
        {
            $HTMLHeader->JAVASCRIPT_EXTERNAL('https://cdnjs.cloudflare.com/ajax/libs/animejs/2.0.2/anime.min.js');

            static::$has_this_macro_been_called = 'yes';
        }
        // Get the value from config key 'bool_checkbox_example'
        if ($this->config['bool_checkbox_example'] == 1)
        {
            $this->appendBeforeArticleOuput($data, 'Test: before article output. Checkbox enabled');
        }
        $app_name = '';

        // $result = $this->loadTemplateFile('view.macro', $local_variables);

        return true;
    }


}

Example of the configuration keys in config.template.php:

<schlix-config:textbox config-key='str_example_1' label='<?= ___('Textbox Example') ?>' class='form-control' />
<schlix-config:textarea config-key='str_example_2' label="<?= ___('Textarea Example') ?>" class='wysiwyg' />             
<schlix-config:textarea config-key='str_example_3' label="<?= ___('Non-WYSIWYG textarea example') ?>" class='form-control'  />             

<schlix-config:integerbox config-key='int_integerbox_example' config-default-value="1" min="1" max="200"  label='<?= ___('Integer box example with default value') ?>' class='form-control' />

<schlix-config:checkbox config-key='bool_checkbox_example' label='<?= ___('Checkbox Example') ?>' />

<schlix-config:dropdownlist class="form-control" config-key="str_option_select" label="<?= ___('Dropdown list example') ?>" >
    <schlix-config:option value="0"><?= ___('Please select') ?></schlix-config:option>
    <schlix-config:option value="<?= SAFE_HTML('opt1') ?>"><?= SAFE_HTML('Option 1') ?></schlix-config:option>
    <schlix-config:option value="<?= SAFE_HTML('opt2') ?>"><?= SAFE_HTML('Option 2') ?></schlix-config:option>
    <schlix-config:option value="<?= SAFE_HTML('opt3') ?>"><?= SAFE_HTML('Option 3') ?></schlix-config:option>
</schlix-config:dropdownlist> 

<schlix-config:radiogroup config-key="int_option_radio" label="<?= ('Radio group example') ?>">
    <schlix-config:option value="1"><?= SAFE_HTML('Option 1') ?></schlix-config:option>
    <schlix-config:option value="2"><?= SAFE_HTML('Option 2') ?></schlix-config:option>
    <schlix-config:option value="3"><?= SAFE_HTML('Option 3') ?></schlix-config:option>
</schlix-config:radiogroup>

<schlix-config:checkboxgroup config-key="int_option_checkbox" label="<?= ('Checkbox group example') ?>">
    <schlix-config:option value="1"><?= SAFE_HTML('Option 1') ?></schlix-config:option>
    <schlix-config:option value="2"><?= SAFE_HTML('Option 2') ?></schlix-config:option>
    <schlix-config:option value="3"><?= SAFE_HTML('Option 3') ?></schlix-config:option>
</schlix-config:checkboxgroup>