Skip to content

Latest commit

 

History

History
379 lines (212 loc) · 5.82 KB

index.rst

File metadata and controls

379 lines (212 loc) · 5.82 KB

Swark

Authors

Jan Kudlicka, Seeds Consulting AS, http://www.seeds.no/

Aplia AS, https://www.aplia.no

Table of Contents

The Swark extension implements a set of highly needed template operators and workflow event types which are sorely missing from eZ publish legacy.

Operators












































Workflow event types


Custom operators

Swark also makes it easier to create custom operators.

Operators are detected from the INI file swark.ini. Adding a new operator only requires defining a new entry in the INI file under [Operators], this maps the template operator name to a PHP class that implements the operator.

For instance to expose the phpinfo() function we could do.

[Operators]
OperatorMap[phpinfo]=MyProject\PhpInfoOperator

Then create the PHP file and extend SwarkOperator, the base class will take care of all the cruft needed to define a template operator. The class must be accessible from the autoload system in PHP.

<?php
namespace MyProject;

use SwarkOperator;

class PhpInfoOperator extends SwarkOperator
{
    // ...
}

The operator class then needs a constructor to initialize its operator name and its parameters (namedParameters), and a function to execute.

Constructor

The constructore defines the name of the template operator, this must match the name as specified in swark.ini. It also defines any parameters that it supports. Each parameter is a name with an optional default value.

For instance for our phpinfo operator we have one parameter which is empty by default, this matches the $what parameter for the phpinfo() function.

<?php
class PhpInfoOperator extends SwarkOperator
{
    function __construct()
    {
        parent::__construct('phpinfo', 'what=');
    }
}

Execute

The execute function takes in two parameters $operatorValue and $namedParameters. $operatorvalue corresponds to the value that is piped to the operator, and $namedParameters is the value(s) supplied as parameters using the names defined in the constructor. Any values returned from execute will be the return value from the template operator.

The phpinfo implementation is then as follows.

<?php
class PhpInfoOperator extends SwarkOperator
{
    static function execute($operatorValue, $namedParameters)
    {
        if ($namedParameters['what']) {
            $constants = array('INFO_GENERAL' => 1, 'INFO_ALL' => -1);
            $what = $namedParameters['what'];
            if (in_array($what, $constants)) {
                phpinfo($constants[$what]);
                return;
            }
        }

        phpinfo();
    }
}

An using it in an eZ template:

{phpinfo('INFO_GENERAL')}