Home > Blockchain >  Cannot override BakeHelper using my plugin
Cannot override BakeHelper using my plugin

Time:10-17

So far, I've been able to override ControllerTask, ModelTask and TemplateTask in my own plugin by adding and aliasing the parent task. Example using ModelTask.

namespace WetKit\Shell\Task;
...
use Bake\Shell\Task\ModelTask as WetKitModelTask;

class ModelTask extends WetKitModelTask
{...}

However, when I attempt to apply the same logic to BakeHelper.php, located in plugin/WetKit/src/View/Helper, it doesn't work and functionality cannot be found:

me@laptop MINGW64 ~/Downloads/xampp3/xampp/htdocs/twofk (master)
$ bin/cake bake controller dummy_lists -t WetKit
*** WetKit Controller Task! ***

Baking controller class for DummyLists...
Warning Error: Method Bake\View\Helper\BakeHelper::isBilingualTable does not exist in [C:\Users\me\Downloads\xampp3\xampp\htdocs\twofk\vendor\cakephp\cakephp\src\View\Helper.php, line 116]

Here is my custom BakeHelper class definition:

namespace WetKit\View\Helper;
...
use Bake\View\Helper as WetKitHelper;

/**
 * Bake helper
 */
class BakeHelper extends WetKitHelper
{
    ...

    public function isBilingualTable($table)
       {...}
}

This will work if I put the file under the vendor folder.

CodePudding user response:

Helpers cannot be "overwritten" like tasks, as they are loaded completely different. Tasks are being collected in a list, where tasks collected at a later point can replace existing entries in the list in case they are using the same name.

Helpers are loaded via CakePHP's default helper loading functionality, eg loadeHelper(), specifically using a short name that resolves to a fully qualified classname, which cannot be replaced.

Unless you actually want to override any of the bake helper functionality, there's not really a reason to not just use a separate helper, which you can load in the Bake.initialize event:

// in your plugin's bootstrap

\Cake\Event\EventManager::instance()
    ->on('Bake.initialize', function (\Cake\Event\EventInterface $event) {
        /** @var \Cake\View\View $view */
        $view = $event->getSubject();

        // load `\WetKit\View\Helper\CustomBakeHelper`
        $view->loadHelper('WetKit.CustomBake');
    });

The helper can then be used like CustomBake.isBilingualTable(...) in the bake twig templates.

For sake of completion, if you actually needed to overwrite the bake helper, you'd first have to unload the existing one, and then load yours:

$view->helpers()->unload('Bake');
$view->loadHelper('WetKit.Bake');

See also

  • Related