You are here

colorpicker_example.module in Colorpicker 5

File

examples/colorpicker_example.module
View source
<?php

/*
 * Implementation of hook_menu().
 */
function colorpicker_example_menu($may_cache) {
  $items = array();
  if (!$may_cache) {
    $items[] = array(
      'title' => t('Color Picker Example'),
      'path' => 'colorpicker/example',
      'description' => t('An example of the color picker in use.'),
      'callback' => 'colorpicker_example_callback',
      'access' => user_access('access colorpicker example'),
    );
  }
  return $items;
}

/**
 * Implementation of hook_perm().
 */
function colorpicker_example_perm() {
  return array(
    'access colorpicker example',
  );
}

/*
 * Callback for example color picker
 */
function colorpicker_example_callback() {
  $output = "<p>" . t('This is an example of a color picker in use.  Notice how some form elements are tied to one colorpicker, and others to another colorpicker.') . "</p>";
  $output .= drupal_get_form('colorpicker_example_callback_form');
  return $output;
}

/*
 * Form builder function for custom colorpicker example callback
 */
function colorpicker_example_callback_form() {
  $form = array();
  $form['colorpicker_example'] = array(
    '#type' => 'colorpicker',
    '#title' => t('Color picker'),
    '#description' => t('This is the Farbtastic colorpicker.'),
  );
  $form['colorpicker_example_textfield'] = array(
    '#type' => 'colorpicker_textfield',
    '#title' => t('Color picker textfield'),
    '#description' => t('This is a textfield associated with the first Farbtastic color picker'),
    '#default_value' => variable_get('colorpicker_example_textfield', '#ff33dd'),
    '#colorpicker' => 'colorpicker_example',
  );
  $form['colorpicker_example_textfield_2'] = array(
    '#type' => 'colorpicker_textfield',
    '#title' => t('Color picker textfield 2'),
    '#description' => t('This is another textfield associated with the first Farbtastic color picker'),
    '#default_value' => variable_get('colorpicker_example_textfield', '#eedd55'),
    '#colorpicker' => 'colorpicker_example',
  );
  $form['colorpicker_example_2'] = array(
    '#type' => 'colorpicker',
    '#title' => t('Color picker'),
    '#description' => t('This is another Farbtastic colorpicker.'),
  );
  $form['colorpicker_example_textfield_3'] = array(
    '#type' => 'colorpicker_textfield',
    '#title' => t('Color picker textfield 3'),
    '#description' => t('This is a textfield associated with the second Farbtastic color picker'),
    '#default_value' => variable_get('colorpicker_example_textfield', '#cccccc'),
    '#colorpicker' => 'colorpicker_example_2',
  );
  $form['colorpicker_example_textfield_4'] = array(
    '#type' => 'colorpicker_textfield',
    '#title' => t('Color picker textfield 4'),
    '#description' => t('This is another textfield associated with the second Farbtastic color picker'),
    '#default_value' => variable_get('colorpicker_example_textfield', '#000000'),
    '#colorpicker' => 'colorpicker_example_2',
  );
  $form['submit'] = array(
    '#type' => 'submit',
    '#title' => t('Save'),
    '#description' => t('Save the default values of this form'),
    '#value' => t('Submit'),
    '#submit' => TRUE,
  );
  return $form;
}

/*
 * Form builder function for custom colorpicker example callback
 */
function colorpicker_example_callback_form_submit($form_id, $form_values) {
  variable_set('colorpicker_example_color', $form_values['colorpicker_example_color']);
  variable_set('colorpicker_example_textarea', $form_values['colorpicker_example_textarea']);
}