color_field.module 5.83 KB
Newer Older
targoo's avatar
targoo committed
1 2 3
<?php
/**
 * @file
targoo's avatar
targoo committed
4
 * A color field with a custom color picker using the Field Types API.
targoo's avatar
targoo committed
5 6
 */

7 8
use Drupal\Core\Routing\RouteMatchInterface;

targoo's avatar
targoo committed
9
/**
10
 * Implements hook_help().
targoo's avatar
targoo committed
11
 */
12 13 14
function color_field_help($route_name, RouteMatchInterface $route_match) {
  switch ($route_name) {
    case 'help.page.color_field':
15 16
      $output = '';
      $output .= '<h3>' . t('About') . '</h3>';
17 18 19 20 21 22 23 24 25 26 27 28 29 30
      $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>';
31 32
      return $output;
  }
targoo's avatar
targoo committed
33 34
}

35
/**
targoo's avatar
targoo committed
36
 * Implements hook_theme().
37
 */
targoo's avatar
targoo committed
38 39 40
function color_field_theme() {
  $theme = [];

targoo's avatar
targoo committed
41
  $theme['color_field_formatter_swatch'] = array(
targoo's avatar
targoo committed
42
      'variables' => array(
targoo's avatar
targoo committed
43
        'shape' => NULL,
targoo's avatar
targoo committed
44 45 46 47 48 49
        'color' => NULL,
        'width' => NULL,
        'height' => NULL,
      ),
  );

targoo's avatar
targoo committed
50
  $theme['color_field_widget_box'] = array(
targoo's avatar
targoo committed
51 52 53
    'render element' => 'element',
  );

54 55 56 57
  $theme['color_field_widget_spectrum'] = array(
    'render element' => 'element',
  );

targoo's avatar
targoo committed
58
  return $theme;
59 60 61
}

/**
62
 * Prepares variables for the color_field formatter swatch template.
targoo's avatar
targoo committed
63
 *
targoo's avatar
targoo committed
64
 * This template outputs a color swatch.
targoo's avatar
targoo committed
65
 *
targoo's avatar
targoo committed
66
 * Default template: color-field-formatter-swatch.html.twig.
targoo's avatar
targoo committed
67 68 69 70
 *
 * @param array $variables
 *   An associative array containing:
 *     - color: The color background.
targoo's avatar
targoo committed
71
 *     - shape: The shape of the color swatch.
targoo's avatar
targoo committed
72 73
 *     - width: The width of the color swatch.
 *     - height: The height of the color swatch.
74
 */
targoo's avatar
targoo committed
75
function template_preprocess_color_field_formatter_swatch(&$variables) {
76
}
targoo's avatar
targoo committed
77 78 79


/**
80
 * Prepares variables for color_field widget box wrapper template.
targoo's avatar
targoo committed
81
 *
targoo's avatar
targoo committed
82
 * Default template: color-field-widget-box.html.twig.
targoo's avatar
targoo committed
83 84 85 86 87 88
 *
 * @param array $variables
 *   An associative array containing:
 *   - element: An associative array containing the properties of the element.
 *     Properties used: #title, #children, #required, #attributes.
 */
targoo's avatar
targoo committed
89
function template_preprocess_color_field_widget_box(&$variables) {
targoo's avatar
targoo committed
90 91 92 93 94 95
  $element = $variables['element'];

  if (!empty($element['#title'])) {
    $variables['title'] = $element['#title'];
  }

targoo's avatar
targoo committed
96 97 98 99 100 101
  if (!empty($element['#description'])) {
    $variables['description'] = $element['#description'];
  }

  $variables['color'] = $element['color'];

102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
  if (!empty($element['opacity'])) {
    $variables['opacity'] = $element['opacity'];
  }

  // Suppress error messages.
  $variables['errors'] = NULL;

  if (!empty($element['#description'])) {
    $variables['description'] = $element['#description'];
  }

  $variables['required'] = FALSE;
  if (!empty($element['#required'])) {
    $variables['required'] = TRUE;
  }

}

/**
 * Prepares variables for color_field widget box wrapper template.
 *
 * Default template: color-field-widget-box.html.twig.
 *
 * @param array $variables
 *   An associative array containing:
 *   - element: An associative array containing the properties of the element.
 *     Properties used: #title, #children, #required, #attributes.
 */
function template_preprocess_color_field_widget_spectrum(&$variables) {
  $element = $variables['element'];

  if (!empty($element['#title'])) {
    $variables['title'] = $element['#title'];
  }

  if (!empty($element['#description'])) {
    $variables['description'] = $element['#description'];
  }

  $variables['color'] = $element['color'];

  if (!empty($element['opacity'])) {
    $variables['opacity'] = $element['opacity'];
  }
targoo's avatar
targoo committed
146

targoo's avatar
targoo committed
147 148 149 150 151 152 153 154 155 156 157 158 159
  // Suppress error messages.
  $variables['errors'] = NULL;

  if (!empty($element['#description'])) {
    $variables['description'] = $element['#description'];
  }

  $variables['required'] = FALSE;
  if (!empty($element['#required'])) {
    $variables['required'] = TRUE;
  }

}