You are here

color_field.module in Color Field 8

Same filename and directory in other branches
  1. 8.2 color_field.module
  2. 7.2 color_field.module
  3. 7 color_field.module

An color field with a custom color picker using the Field Types API.

File

color_field.module
View source
<?php

/**
 * @file
 * An color field with a custom color picker using the Field Types API.
 */
use Drupal\Core\Routing\RouteMatchInterface;

/**
 * Implements hook_help().
 */
function color_field_help($route_name, RouteMatchInterface $route_match) {
  switch ($route_name) {
    case 'help.page.color_field':
      $output = '';
      $output .= '<h3>' . t('About') . '</h3>';
      $output .= '<p>' . t('Color Field is simple field that use a hexadecimal notation (HEX) for the combination of Red, Green, and Blue color values (RGB). See the <a href="!field">Field module help</a> and the <a href="!field_ui">Field UI help</a> pages for general information on fields and how to create and manage them. For more information, see the <a href="!link_documentation">online documentation for the Link module</a>.', array(
        '!field' => \Drupal::url('help.page', array(
          'name' => 'field',
        )),
        '!field_ui' => \Drupal::url('help.page', array(
          'name' => 'field_ui',
        )),
        '!link_documentation' => 'https://drupal.org/documentation/modules/link',
      )) . '</p>';
      $output .= '<h3>' . t('Uses') . '</h3>';
      $output .= '<dl>';
      $output .= '<dt>' . t('Managing and displaying color fields') . '</dt>';
      $output .= '<dd>' . t('The <em>settings</em> and the <em>display</em> of the link field can be configured separately. See the <a href="!field_ui">Field UI help</a> for more information on how to manage fields and their display.', array(
        '!field_ui' => \Drupal::url('help.page', array(
          'name' => 'field_ui',
        )),
      )) . '</dd>';
      $output .= '<dt>' . t('Adding link text') . '</dt>';
      $output .= '<dd>' . t('In the field settings you can define additional link text to be <em>optional</em> or <em>required</em> in any link field.') . '</dd>';
      $output .= '<dt>' . t('Displaying link text') . '</dt>';
      $output .= '<dd>' . t('If link text has been submitted for a URL, then by default this link text is displayed as a link to the URL. If you want to display both the link text <em>and</em> the URL, choose the appropriate link format from the drop-down menu in the <em>Manage display</em> page. If you only want to display the URL even if link text has been submitted, choose <em>Link</em> as the format, and then change its <em>Format settings</em> to display <em>URL only</em>.') . '</dd>';
      $output .= '<dt>' . t('Adding attributes to links') . '</dt>';
      $output .= '<dd>' . t('You can add attributes to links, by changing the <em>Format settings</em> in the <em>Manage display</em> page. Adding <em>rel="nofollow"</em> notifies search engines that links should not be followed.') . '</dd>';
      $output .= '<dt>' . t('Validating URLs') . '</dt>';
      $output .= '<dd>' . t('All links are validated after a link field is filled in. They can include anchors or query strings.') . '</dd>';
      $output .= '</dl>';
      return $output;
  }
}

/**
 * Helper: Convert RGB to HEX6
 * 
 * @param $rgb Must be an array indexed on r, g and b.
 */
function color_field_rgb2hex($rgb = FALSE) {
  $hex = '';
  $hex .= str_pad(dechex($rgb[0]), 2, "0", STR_PAD_LEFT);
  $hex .= str_pad(dechex($rgb[1]), 2, "0", STR_PAD_LEFT);
  $hex .= str_pad(dechex($rgb[2]), 2, "0", STR_PAD_LEFT);
  return $hex;

  // returns the hex value including the number sign (#)
}

/**
 * Helper: Convert HEX6 to RGB
 * 
 * @param $hex.
 */
function color_field_hex2rgb($hex = FALSE) {
  $r = hexdec(substr($hex, 0, 2));
  $g = hexdec(substr($hex, 2, 2));
  $b = hexdec(substr($hex, -2));
  return compact('r', 'g', 'b');
}

Functions

Namesort descending Description
color_field_help Implements hook_help().
color_field_hex2rgb Helper: Convert HEX6 to RGB
color_field_rgb2hex Helper: Convert RGB to HEX6