Jump to content

Hookable Namespaced Module


wumbo
 Share

Recommended Posts

Hi everybody,

im planning to release an updated version of the TwigReplace module for ProcessWire 3.

In addition to the known config options for setting up the twig environment, I'm wantg to provide the means to enable twig extensions at will. But this can't be done via module config options because of different reasons (finding the correct twig extenion classes - installed or self-made, providing constructor arguments to the twig extension, ...).

To solve this I plan to add a hookable method to my module that instantiates and returns the Twig_Environment object. If this works, users of the module may create their own project specific modules to intercept this hook and modify the Twig_Environment instance by adding twig extensions at will.

Here's the skeleton of my module (with ___initTwig() being the hookable method):

namespace nw\ProcessWire3;

use ProcessWire\FileCompilerModule;

class FileCompilerTwig extends FileCompilerModule
{
    /**
     * @var \Twig_Environment
     */
    protected $twig;

    /**
     * Creates the twig render engine if necessary
     */
    public function init()
    {
        /*
         * gather module config settings ...
         */
        $this->twig = $this->initTwig($pwTemplatesPath, $twigOptions);
    }

    /**
     * Instantiates a Twig environment
     *
     * THIS IS THE HOOKABLE METHOD.
     *
     * @param string $pwTemplatesPath
     * @param array $twigOptions
     * @return \Twig_Environment
     */
    public function ___initTwig($pwTemplatesPath, array $twigOptions)
    {
        $loader = new \Twig_Loader_Filesystem($pwTemplatesPath);

        return new \Twig_Environment($loader, $twigOptions);
    }

This works fine so far.

But when I try to hook into the ___initTwig method via another module, nothing happens. Now I'm not sure if the entire concept (hooking into a module from another module) isn't supported by ProcessWire, or if I just haven't found the correct way to address the desired hook (maybe due to namespacing issues).

So far I tried:

namespace nw\ProcessWire3;

use ProcessWire\HookEvent;
use ProcessWire\Module;
use ProcessWire\WireData;

class FileCompilerTwigExtensions extends WireData implements Module
{
    public function init()
    {
        $this->addHookAfter('FileCompilerTwig::initTwig', $this, 'addTwigExtensions');

        // didn't work either
        // $this->addHookAfter('nw\ProcessWire3\FileCompilerTwig::initTwig', $this, 'addTwigExtensions');
        // $this->addHookAfter('\nw\ProcessWire3\FileCompilerTwig::initTwig', $this, 'addTwigExtensions');
    }

    public function addTwigExtensions(HookEvent $event)
    {
        /** @var \Twig_Environment $twigEnv */
        $twigEnv = $event->return;

        $twigEnv->addExtension(new \Twig_Extension_Debug());
    }
}

I'm thankful for any suggestions.

wumbo

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...