0

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

Davide Casiraghi
  • 15,591
  • 9
  • 34
  • 56
  • builder plugin can not recognize your `FormWidget actorbox` you need tell your specification to builder plugin explicitly so, it can recognize your component, check my other answer about that => https://stackoverflow.com/questions/58019789/october-cms-range-field-type-or-custom-in-create-update-form/58038711#58038711 – Hardik Satasiya Sep 08 '20 at 16:31
  • Hi Hardik, in which file and function do I have to specify it? I'm going through your code but I don't understand yet. – Davide Casiraghi Sep 09 '20 at 07:42
  • its written there `add this code to your plugin boot method` `plugin.php` file, and for other files you need to create new :) – Hardik Satasiya Sep 09 '20 at 07:45

1 Answers1

0

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.

  • I tried to substitute with this code but nothing change, I get the same error. – Davide Casiraghi Sep 08 '20 at 13:37
  • I agree with you. I have checked 100 times if there is any typo but everything seems correct. – Davide Casiraghi Sep 08 '20 at 13:41
  • yes problem is that it uses hard-coded values and there is no way to extend that, forcefully you have to create your own `CustomDesignTimeProvider` to add your own values and tempaltes => https://stackoverflow.com/questions/58019789/october-cms-range-field-type-or-custom-in-create-update-form/58038711#58038711 – Hardik Satasiya Sep 08 '20 at 16:33