Hello,
I am trying to add in a Color Field to display the Widget Boxes and while looking through the code, I just cannot get it to display. Here is my code that I have that I'm trying to add the field for:
$form['bg_colors'] = [
#type' => 'color_field_type',
#title' => $this->t('Background Colors'),
#default_value' => $this->configuration['bg_colors'],
#description' => $this->t('Choose a background color for the whole layout.'),
];
But this doesn't render or show anything. If I change the '#type' variable to 'color', I get an HTML5 Color select box, but that will not work for me, as I need to limit the selection of colors to some very specific colors.
How can I implement the Color Field programmatically?
Comments
Comment #2
kittiradovics commentedYou will need a custom form element to be able to use it in a form. I created this one for my custom project, but it's not a perfect solution because it has redundant code from
ColorFieldWidgetBox.php. It would be great to have form elements for each widget type in the module.Comment #3
cozydrupalnerd commentedThank you for this! I'll give this a try and let you know if I have any other questions. :D
Comment #4
TheWinkingSkeever commentedI too am interested in this, but for spectrum widget. Have you got it working in the layout builder?
Comment #5
kittiradovics commentedNo, unfortunately I don't have form elements for all widgets. You can use the code in
formElement()methods ofColorFieldWidgetBaseandColorFieldWidgetSpectrumclasses for creating your custom form element.Comment #6
aliyakhan commentedSince color_field option is not available, try creating a select list of list colors. Adding example code:
Comment #7
netprogfr commentedI know that this publication is quite old, but I think it is useful to know that there is a simple alternative solution to have a field with a color palette : Color.php (Drupal API)
Comment #8
le72Thanks for the direction @kittiradovics. Here is my implementation of the spectrum widget.
If the patch is needed I can provide it. But it's may be better to develop a submodule with 3-4 new form elements.
Comment #9
nickdickinsonwildeSounds like working with layout builder (and working for me) with #3183101: jQuery spectrum widget text input does not work with modals
Comment #10
schoenef commented#8 works fantastic! Thx @le72.
I feel this is kind of a missing feature of the moule.