How to make plugin's widget override the default CakePHP BasicWidget

好久不见. 提交于 2019-12-11 17:38:39

问题


I have baked a plugin and added the \plugins\MyPlugin\src\View\Widget\BasicWidget.php file:

namespace Cake\View\Widget;

use Cake\View\Widget\BasicWidget as BaseBasicWidget;
use Cake\View\Form\ContextInterface;

/**
 * Basic input class.
 *
 * This input class can be used to render basic simple
 * input elements like hidden, text, email, tel and other
 * types.
 */
class BasicWidget implements BaseBasicWidget
{
    public function render(array $data, ContextInterface $context)
    {
        $data += [
            'name' => '',
            'val' => null,
            'type' => 'text',
            'escape' => true,
            'templateVars' => []
        ];
        $data['value'] = $data['val'];
        unset($data['val']);

        return $this->_templates->format('input', [
            'name' => $data['name'],
            'type' => $data['type'],
            'value' => $data['value'],
            'templateVars' => $data['templateVars'],
            'attrs' => $this->_templates->formatAttributes(
                $data,
                ['name', 'type']
            ),
        ]);
    }
}

The documentation here doesn't explain how to make this widget active: https://book.cakephp.org/3.0/en/views/helpers/form.html#adding-custom-widgets.

It suggests using $config['widgets'] like so:

$this->loadHelper('Form', [
    'widgets' => [
        'basic' => ['MyPlugin.Basic']
    ]
]);

but this ends up looking in the plugin's `\config' path with the following error:

Could not load configuration file: /Users/geoidesic/MyProject/config/Basic.php

来源:https://stackoverflow.com/questions/55679980/how-to-make-plugins-widget-override-the-default-cakephp-basicwidget

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!