1

Older versions of Joomla were able to just use JHtml::_('behavior.colorpicker'); and then have inputs with the class minicolors minicolors-input to use a color picker.

That functionality no long exists in Joomla 4.x. The old Html\Behavior file mentions in a deprecation note that users should either switch to either the field or the layout for rendering. The issue I'm having is figuring out how exactly to go about this.

Thus far I have:

$color = new Joomla\CMS\Form\Field\ColorField();
$color->__set('name', 'debug');
echo $color->renderField();

Not only does that not feel like the correct method, but it also throws an error/warning:

Notice: Trying to access array offset on value of type null in /libraries/src/Form/FormField.php on line 834

1
+100

Neither form fields, nor their layouts are really designed to be used outside of Form API. Things can break very easily this way. A form instance is expected in some fields and a field instance in some layouts. To use a form field alone you have to create a SimpleXMLElement instance with the field tag as it would appear in the form definition and pass it to field's setup() method:

$field = new Joomla\CMS\Form\Field\ColorField;
$field->setup(new SimpleXMLElement('<field type="color" name="myfield" />'), $value);
echo $field->renderField();

That said, if you were using color picker outside of Form API, you can continue doing that. Manually load the minicolors assets and initialization script, which is essentially what JHtml::_('behavior.colorpicker') was doing:

// Get the web asset manager.
$webAssetManager = Joomla\CMS\Factory::getApplication()->getDocument()->getWebAssetManager();

// Load the assets.
$webAssetManager
    ->usePreset('minicolors')
    ->useScript('field.color-adv');

This will add minicolors to all input elements with minicolors class as it was in J3.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.