Error adding custom widget to a form in October CMS

561 Views Asked by At

I've created a new custom widget in a plugin trough artisan command.

php artisan create:formwidget --force DavideCasiraghi.Movies Actorbox

But when I'm trying to load it into my fields.yaml file I get this error.

Unknown control type: actorbox

enter image description here

enter image description here

This is the content of /plugins/davidecasiraghi/movies/formwidgets/Actorbox.php

<?php namespace DavideCasiraghi\Movies\FormWidgets;

use Backend\Classes\FormWidgetBase;

/**
 * Actorbox Form Widget
 */
class Actorbox extends FormWidgetBase
{
  
    protected $defaultAlias = 'actorbox';
    
    public function init()
    {
    }
    
    public function render()
    {
        $this->prepareVars();
        return $this->makePartial('actorbox');
    }
    
    public function prepareVars()
    {
        $this->vars['name'] = $this->formField->getName();
        $this->vars['value'] = $this->getLoadValue();
        $this->vars['model'] = $this->model;
    }
    
    public function loadAssets()
    {
        $this->addCss('css/select2.css', 'DavideCasiraghi.Movies');
        $this->addJs('js/select2.js', 'DavideCasiraghi.Movies');
    }

    /**
     * @inheritDoc
     */
    public function getSaveValue($value)
    {
        return $value;
    }
}

This is how the custom widget is defined in /plugins/davidecasiraghi/movies/Plugin.php

<?php namespace DavideCasiraghi\Movies;

use System\Classes\PluginBase;

class Plugin extends PluginBase
{
    public function registerComponents()
    {
    }

    public function registerSettings()
    {
    }

    public function registerFormWidgets() {
      return [
        'DavideCasiraghi\Movies\FormWidgets\Actorbox' => [
          'label' => 'Actorbox field',
          'code' => 'actorbox',
        ]
      ];
    }

}

I lost already 2 hours trying to figure out why but I didn't get it yet. What am I missing?

There is a similar form on this topic but the error that I get it's different. October CMS : Not able to create a Form Widget

1

There are 1 best solutions below

3
On

From what I can see, the error is coming from the RainLab.Builder plugin, in this method:

    public function renderControlBody($type, $properties, $formBuilder)
    {   
        if (!in_array($type, $this->defaultControlsTypes)) {
            return $this->renderUnknownControl($type, $properties);
        }   

        return $this->makePartial('control-'.$type, [
            'properties'=>$properties,
            'formBuilder' => $formBuilder
        ]);
    }

It's only checking for hardcoded control types... So it looks like the Builder plugin does not allow custom defined formwidgets... but that should work fine outside of this plugin.