color_field.module 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <?php
  2. /**
  3. * @file
  4. * A color field with a custom color picker using the Field Types API.
  5. */
  6. use Drupal\Core\Routing\RouteMatchInterface;
  7. use Drupal\Core\Url;
  8. /**
  9. * Implements hook_help().
  10. */
  11. function color_field_help($route_name, RouteMatchInterface $route_match) {
  12. switch ($route_name) {
  13. case 'help.page.color_field':
  14. $output = '';
  15. $output .= '<h3>' . t('About') . '</h3>';
  16. $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' => Url::fromRoute('help.page', array('name' => 'field')), '!field_ui' => Url::fromRoute('help.page', array('name' => 'field_ui')), '!link_documentation' => 'https://drupal.org/documentation/modules/link')) . '</p>';
  17. $output .= '<h3>' . t('Uses') . '</h3>';
  18. $output .= '<dl>';
  19. $output .= '<dt>' . t('Managing and displaying color fields') . '</dt>';
  20. $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' => Url::fromRoute('help.page', array('name' => 'field_ui')))) . '</dd>';
  21. $output .= '<dt>' . t('Adding link text') . '</dt>';
  22. $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>';
  23. $output .= '<dt>' . t('Displaying link text') . '</dt>';
  24. $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>';
  25. $output .= '<dt>' . t('Adding attributes to links') . '</dt>';
  26. $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>';
  27. $output .= '<dt>' . t('Validating URLs') . '</dt>';
  28. $output .= '<dd>' . t('All links are validated after a link field is filled in. They can include anchors or query strings.') . '</dd>';
  29. $output .= '</dl>';
  30. return $output;
  31. }
  32. }
  33. /**
  34. * Implements hook_theme().
  35. */
  36. function color_field_theme() {
  37. $theme = [];
  38. $theme['color_field_formatter_swatch'] = array(
  39. 'variables' => array(
  40. 'shape' => NULL,
  41. 'color' => NULL,
  42. 'width' => NULL,
  43. 'height' => NULL,
  44. ),
  45. );
  46. $theme['color_field_widget_box'] = array(
  47. 'render element' => 'element',
  48. );
  49. $theme['color_field_widget_spectrum'] = array(
  50. 'render element' => 'element',
  51. );
  52. return $theme;
  53. }
  54. /**
  55. * Prepares variables for the color_field formatter swatch template.
  56. *
  57. * This template outputs a color swatch.
  58. *
  59. * Default template: color-field-formatter-swatch.html.twig.
  60. *
  61. * @param array $variables
  62. * An associative array containing:
  63. * - color: The color background.
  64. * - shape: The shape of the color swatch.
  65. * - width: The width of the color swatch.
  66. * - height: The height of the color swatch.
  67. */
  68. function template_preprocess_color_field_formatter_swatch(&$variables) {
  69. }
  70. /**
  71. * Prepares variables for color_field widget box wrapper template.
  72. *
  73. * Default template: color-field-widget-box.html.twig.
  74. *
  75. * @param array $variables
  76. * An associative array containing:
  77. * - element: An associative array containing the properties of the element.
  78. * Properties used: #title, #children, #required, #attributes.
  79. */
  80. function template_preprocess_color_field_widget_box(&$variables) {
  81. $element = $variables['element'];
  82. if (!empty($element['#title'])) {
  83. $variables['title'] = $element['#title'];
  84. }
  85. if (!empty($element['#description'])) {
  86. $variables['description'] = $element['#description'];
  87. }
  88. $variables['color'] = $element['color'];
  89. if (!empty($element['opacity'])) {
  90. $variables['opacity'] = $element['opacity'];
  91. }
  92. // Suppress error messages.
  93. $variables['errors'] = NULL;
  94. if (!empty($element['#description'])) {
  95. $variables['description'] = $element['#description'];
  96. }
  97. $variables['required'] = FALSE;
  98. if (!empty($element['#required'])) {
  99. $variables['required'] = TRUE;
  100. }
  101. }
  102. /**
  103. * Prepares variables for color_field widget box wrapper template.
  104. *
  105. * Default template: color-field-widget-box.html.twig.
  106. *
  107. * @param array $variables
  108. * An associative array containing:
  109. * - element: An associative array containing the properties of the element.
  110. * Properties used: #title, #children, #required, #attributes.
  111. */
  112. function template_preprocess_color_field_widget_spectrum(&$variables) {
  113. $element = $variables['element'];
  114. if (!empty($element['#title'])) {
  115. $variables['title'] = $element['#title'];
  116. }
  117. if (!empty($element['#description'])) {
  118. $variables['description'] = $element['#description'];
  119. }
  120. $variables['color'] = $element['color'];
  121. if (!empty($element['opacity'])) {
  122. $variables['opacity'] = $element['opacity'];
  123. }
  124. // Suppress error messages.
  125. $variables['errors'] = NULL;
  126. if (!empty($element['#description'])) {
  127. $variables['description'] = $element['#description'];
  128. }
  129. $variables['required'] = FALSE;
  130. if (!empty($element['#required'])) {
  131. $variables['required'] = TRUE;
  132. }
  133. }