I'm trying to use color_field within my theme-settings.php file but can't get it to show up.

Does it support this and if so is there documentation available for it?

Comments

inteja’s picture

FYI: Managed to do this manually with the following code. Doing it this way also means you can use the extra features of the spectrum color picker (like rgba colors) that color_field doesn't yet support.

In your theme's theme-settings.php:

function mytheme_form_system_theme_settings_alter(&$form, &$form_state)  {

  // Add spectrum color picker js and css. See http://bgrins.github.io/spectrum
  // Requires libraries api and spectrum installed in libraries/bgrins-spectrum
  // as per color_field install instructions
  drupal_add_js(libraries_get_path('bgrins-spectrum') . '/spectrum.js');
  drupal_add_css(libraries_get_path('bgrins-spectrum') . '/spectrum.css');
  $spectrum_js = 'jQuery(".spectrum-color-picker").spectrum({
    showInput: true,
    allowEmpty: true,
    showAlpha: true,
    showInitial: true,
    showInput: true,
    preferredFormat: "rgb",
    clickoutFiresChange: true,
    showButtons: false
  });';
  drupal_add_js($spectrum_js, array('type' => 'inline', 'scope' => 'footer'));
  // Spectrum color picker field
  $form['myform']['color'] = array(
    '#type' => 'textfield',
    '#default_value' => theme_get_setting('color'),
    '#attributes' => array('class' => array('spectrum-color-picker')), 
  );
targoo’s picture

Thanks for the feedback and the code snippet.

I will add an extra documentation page to cover use cases like this one.

navinder’s picture

I was trying to add color_field to a custom form by referring to code snippet above in a Drupal 8 site, but it is only showing me a plain text field. Is something additional that I need to do for Drupal 8? I have this module installed and working.

nickdickinsonwilde’s picture

hmm I've never had that use case. For some cases, just changing `#type` to `color` rather than `textfield` might work, but that won't do everything that color field can do. If that doesn't provide what you need, let me know and I'll try and take a look sometime soon.
BTW, for old threads like this, instead of reviving a closed issue from 4 years ago (aka thread necromancy), better to create a new ticket linking if relevant.
Ps. welcome fellow Canadian :D

navinder’s picture

Thank you Nick! Replacing type with "color" only producing a black rectangle not the color field. And I agree about creating a new thread but here I thought putting it here might be easier for others as they already have a sense of the issue.

nickdickinsonwilde’s picture

Status: Active » Closed (outdated)

As there hasn't been a D7 Color Field maintainer in a long time, closing this issue. Feel free to re-open if you are interested in getting involved.