You are here

function jquery_colorpicker_process_element in Jquery Colorpicker 7

Callback function to process jquery_colorpicker elements

See also

jquery_colorpicker_element_info()

1 string reference to 'jquery_colorpicker_process_element'
jquery_colorpicker_element_info in ./jquery_colorpicker.module
Implements hook_elements().

File

./jquery_colorpicker.module, line 79
JQuery Colorpicker primary module file.

Code

function jquery_colorpicker_process_element($element, &$form_state) {
  $element['#id'] = drupal_html_id('edit-' . implode('-', $element['#parents']));
  $library_path = libraries_get_path('colorpicker');

  // 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.
  $background_url = file_create_url($library_path . '/images/' . $background);

  // Attach the 3rd party CSS and JS files, and attach the module's JS files.
  $element['#attached'] = array(
    'css' => array(
      // Add the 3rd party CSS files required for the form elmeent.
      $library_path . '/css/colorpicker.css',
    ),
    'js' => array(
      // Add the 3rd party JS files required for the form element.
      $library_path . '/js/colorpicker.js',
      // Add the module js files.
      drupal_get_path('module', 'jquery_colorpicker') . '/js/jquery_colorpicker.js',
    ),
  );

  // And we pass the settings in a namespace to the Javascript.
  $element['#attached']['js'][] = array(
    'data' => array(
      'jqueryColorpicker' => array(
        $element['#id'] => $background_url,
      ),
    ),
    'type' => 'setting',
  );
  return $element;
}