You are here

function theme_jquery_colorpicker in Jquery Colorpicker 7

callback theme for the new form element

1 theme call to theme_jquery_colorpicker()
jquery_colorpicker_element_info in ./jquery_colorpicker.module
Implements hook_elements().

File

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

Code

function theme_jquery_colorpicker($variables) {
  $element = $variables['element'];
  $class = array(
    'form-colorpicker',
  );
  $output = '';
  unset($element['#prefix']);
  unset($element['#suffix']);

  // Determine the default color for the form element
  $default_color = "#ffffff";
  if (isset($element['#value']) && strlen($element['#value'])) {
    $default_color = '#' . $element['#value'];
  }
  elseif (isset($element['#default_value']) && strlen($element['#default_value']) == 6 && preg_match('/^[0-9a-f]{6}$/i', $element['#default_value'])) {
    $default_color = '#' . strtolower($element['#default_value']);
  }

  // Over the next few lines we build the output of the element in HTML and to send to the browser.
  _form_set_class($element, $class);
  $name = isset($element['#name']) ? $element['#name'] : $element['#id'];
  $value = isset($element['#value']) ? check_plain($element['#value']) : '';
  if (preg_match('/^#/', $value)) {
    $value = substr($value, 1);
  }
  $output .= '<div class="jquery_colorpicker">';
  $output .= '<div id="' . $element['#id'] . '-inner_wrapper" class="inner_wrapper">';
  $output .= '<div class="color_picker" style="background-color:' . $default_color . '">';
  $output .= '<span class="hash">#</span>';
  $output .= '<input type="text"' . ' maxlength="7"' . ' name="' . $name . '" id="' . $element['#id'] . '"' . ' size="7"' . ' value="' . $value . '"' . drupal_attributes($element['#attributes']) . ' />';
  $output .= '<div class="description">' . t('Enter a hexidecimal color value. Enabling javascript will replace this input with a graphical color selector.') . '</div>';
  $output .= '</div>';
  $output .= '</div>';
  if (isset($element['#cardinality'])) {
    if ($element['#cardinality'] == FIELD_CARDINALITY_UNLIMITED) {
      $output .= '<div>' . l(t('Remove'), '#', array(
        'attributes' => array(
          'class' => array(
            'jquery_colorpicker_field_remove_link',
          ),
        ),
      )) . '</div>';
    }
    else {
      $output .= '<div>' . l(t('Clear'), '#', array(
        'attributes' => array(
          'class' => array(
            'jquery_colorpicker_field_clear_link',
          ),
        ),
      )) . '</div>';
    }
  }
  $output .= '</div>';
  return $output;
}