You are here

function textimage_color_element_process in Textimage 7.3

Process callback for 'textimage_color' form element.

Enable capturing color information. Depending on the options, uses a normal textfield or a jquery_colorpicker element to capture the HEX value of a color. The following elements further qualify the process: '#allow_transparent' - if set to TRUE, a checkbox is displayed to set the color as a full transparency, In this case, color hex and opacity are hidden, and the value returned is NULL. '#allow_opacity' - if set to TRUE, a textfield is displayed to capture the 'opacity' value, as a percentage.

1 string reference to 'textimage_color_element_process'
textimage_element_info in ./textimage.module
Implements hook_element_info().

File

./textimage.module, line 661
Textimage - Provides text to image manipulations.

Code

function textimage_color_element_process($element, &$form_state) {

  // Make sure element properties are set.
  $element['#allow_transparent'] = isset($element['#allow_transparent']) ? $element['#allow_transparent'] : FALSE;
  $element['#allow_opacity'] = isset($element['#allow_opacity']) ? $element['#allow_opacity'] : FALSE;
  $element['#description'] = isset($element['#description']) ? $element['#description'] : NULL;

  // In case default value is transparent, set hex and opacity to default
  // values (white, fully opaque) so that if transparency is unchecked,
  // we have a starting value.
  $transparent = empty($element['#default_value']) ? TRUE : FALSE;
  $hex = $transparent ? '#FFFFFF' : drupal_substr($element['#default_value'], 0, 7);
  $opacity = $transparent ? 100 : _textimage_rgba_to_opacity($element['#default_value']);
  if ($element['#allow_transparent'] || $element['#allow_opacity']) {

    // If more sub-fields are needed to define the color, wrap them in a
    // fieldset.
    $element['container'] = array(
      '#type' => 'fieldset',
      '#description' => $element['#description'],
      '#title' => $element['#title'],
    );

    // Checkbox for transparency.
    if ($element['#allow_transparent']) {
      $element['container']['transparent'] = array(
        '#type' => 'checkbox',
        '#title' => t('Transparent'),
        '#default_value' => $transparent,
      );
    }

    // Color field.
    // Renders a jquery_colorpicker element if available and enabled, otherwise
    // a textfield.
    if (_textimage_get_variable('color_selector') == 'jquery_colorpicker') {
      $element['container']['hex'] = array(
        '#title' => t('Color'),
        '#type' => 'jquery_colorpicker',
        '#default_value' => drupal_substr($hex, -6),
      );
    }
    else {
      $element['container']['hex'] = array(
        '#title' => t('Color'),
        '#type' => 'textfield',
        '#default_value' => $hex,
        '#maxlength' => 7,
        '#size' => 7,
        '#element_validate' => array(
          'image_effect_color_validate',
        ),
      );
    }

    // States management for color field.
    $element['container']['hex']['#states'] = array(
      'visible' => array(
        ':input[name="' . $element['#name'] . '[container][transparent]"]' => array(
          'checked' => FALSE,
        ),
      ),
    );

    // Textfield for opacity.
    if ($element['#allow_opacity']) {
      $element['container']['opacity'] = array(
        '#type' => 'textfield',
        '#title' => t('Opacity'),
        '#default_value' => $opacity,
        '#maxlength' => 3,
        '#size' => 2,
        '#field_suffix' => '%',
        '#element_validate' => array(
          'element_validate_integer',
        ),
        '#states' => array(
          'visible' => array(
            ':input[name="' . $element['#name'] . '[container][transparent]"]' => array(
              'checked' => FALSE,
            ),
          ),
        ),
      );
    }
  }
  else {

    // No transparency or opacity, straight color field.
    if (_textimage_get_variable('color_selector') == 'jquery_colorpicker') {
      $element['hex'] = array(
        '#type' => 'jquery_colorpicker',
        '#title' => $element['#title'],
        '#description' => $element['#description'],
        '#default_value' => drupal_substr($hex, -6),
      );
    }
    else {
      $element['hex'] = array(
        '#type' => 'textfield',
        '#title' => $element['#title'],
        '#description' => $element['#description'],
        '#default_value' => $hex,
        '#maxlength' => 7,
        '#size' => 7,
        '#element_validate' => array(
          'image_effect_color_validate',
        ),
      );
    }
  }
  unset($element['#description'], $element['#title']);
  return $element;
}