You are here

function jquery_colorpicker_cck_process in Jquery Colorpicker 6.2

Process an individual element.

Build the form element. When creating a form using FAPI #process, note that $element['#value'] is already set.

The $fields array is in $form['#field_info'][$element['#field_name']].

1 string reference to 'jquery_colorpicker_cck_process'
jquery_colorpicker_cck_elements in jquery_colorpicker_cck/jquery_colorpicker_cck.module
Implementation of FAPI hook_elements().

File

jquery_colorpicker_cck/jquery_colorpicker_cck.module, line 67
This file holds the main Drupal hook functions and private functions for the jquery_colorpicker_cck module.

Code

function jquery_colorpicker_cck_process($element, $edit, $form_state, $form) {
  $field_name = $element['#field_name'];
  $field = $form['#field_info'][$field_name];
  $field_key = $element['#columns'][0];
  $value = isset($element['#value'][$field_key]) ? substr($element['#value'][$field_key], -6) : (isset($field['widget']['default_value'][0]['value']) ? substr($field['widget']['default_value'][0]['value'], 1) : 'FFFFFF');
  $element[$field_key] = array(
    '#type' => 'colorpicker',
    '#default_value' => empty($value) ? 'FFFFFF' : $value,
    '#title' => $element['#title'],
    '#description' => $element['#description'],
    '#required' => $element['#required'],
    '#field_name' => $element['#field_name'],
    '#type_name' => $element['#type_name'],
    '#delta' => $element['#delta'],
    '#columns' => $element['#columns'],
  );

  // Make sure we don't wipe out element validation added elsewhere.
  if (empty($element['#element_validate'])) {
    $element['#element_validate'] = array(
      'jquery_colorpicker_cck_integration',
    );
  }
  if (!empty($field['prefix'])) {
    $prefixes = explode('|', $field['prefix']);
    $element[$field_key]['#field_prefix'] = array_pop($prefixes);
  }
  if (!empty($field['suffix'])) {
    $suffixes = explode('|', $field['suffix']);
    $element[$field_key]['#field_suffix'] = array_pop($suffixes);
  }

  // Used so that hook_field('validate') knows where to flag an error.
  $element['_error_element'] = array(
    '#type' => 'value',
    '#value' => implode('][', array_merge($element['#parents'], array(
      $field_key,
    ))),
  );

  // Next we decide what background to use to render the element. In order to ensure the background exists, we create an array of
  // the two possibilities, that we will use to compare the value submitted in the Form API definition
  $backgrounds = array(
    'select.png',
    'select2.png',
  );

  // Now we check to see if the value in the Form API definition is valid. If it is, we use it, if it's not, we use a default value
  $background = in_array($element['#jquery_colorpicker_background'], $backgrounds) ? $element['#jquery_colorpicker_background'] : 'select.png';

  // Since we know the background, we can then get the URL of it to pass to the javascript function
  $path = module_exists('libraries') && in_array('colorpicker', array_keys(libraries_get_libraries())) ? libraries_get_path('colorpicker') : drupal_get_path('module', 'jquery_colorpicker') . '/colorpicker';
  $background_url = base_path() . $path . '/images/' . $background;

  // next we determine what the default value for the form element is. This will also be passed to the javascript function.
  if (isset($element['#value']['value'])) {
    $default_color = '#' . $element['#value']['value'];
  }
  else {
    $default_color = strtolower("#" . $default_value);
  }

  // Finally we build an array of all the settings to be used by the javascript function
  $settings = array(
    'ids' => array(
      $element['#id'] . '-value',
    ),
    'backgrounds' => array(
      $background_url,
    ),
    'backgroundColors' => array(
      $default_color,
    ),
    $element['#id'] . '-defaultColor' => $default_color,
  );

  // And we pass the settings in a namespace to the Javascript
  drupal_add_js(array(
    'jqueryColorpicker' => $settings,
  ), 'setting');
  return $element;
}