title.field.inc 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. <?php
  2. /**
  3. * @file
  4. * Implement a title field formater.
  5. */
  6. /**
  7. * Implements hook_field_formatter_info().
  8. */
  9. function title_field_formatter_info() {
  10. return array(
  11. 'title_linked' => array(
  12. 'label' => t('Linked and wrapped'),
  13. 'field types' => array('text'),
  14. 'settings' => array(
  15. 'title_style' => '',
  16. 'title_link' => '',
  17. 'title_class' => '',
  18. 'enable_trim' => FALSE,
  19. 'trim_length' => 50,
  20. ),
  21. ),
  22. );
  23. }
  24. /**
  25. * Implements hook_field_formatter_settings_form().
  26. */
  27. function title_field_formatter_settings_form($field, $instance, $view_mode, $form, &$form_state) {
  28. $settings = $instance['display'][$view_mode]['settings'];
  29. $element = array();
  30. $wrap_tags = array(
  31. '_none' => t('- None -'),
  32. 'div' => t('DIV'),
  33. 'h1' => t('H1'),
  34. 'h2' => t('H2'),
  35. 'h3' => t('H3'),
  36. 'h4' => t('H4'),
  37. 'h5' => t('H5'),
  38. 'h6' => t('H6'),
  39. 'span' => t('SPAN'),
  40. );
  41. $element['title_style'] = array(
  42. '#title' => t('Wrap title in tag'),
  43. '#type' => 'select',
  44. '#default_value' => !empty($settings['title_style']) ? $settings['title_style'] : '_none',
  45. '#options' => $wrap_tags,
  46. );
  47. $link_types = array(
  48. 'content' => t('Content (relative url)'),
  49. 'content_absolute' => t('Content (absolute url)'),
  50. );
  51. $element['title_link'] = array(
  52. '#title' => t('Link title to'),
  53. '#type' => 'select',
  54. '#default_value' => $settings['title_link'],
  55. '#empty_option' => t('Nothing'),
  56. '#options' => $link_types,
  57. );
  58. $element['enable_trim'] = array(
  59. '#type' => 'checkbox',
  60. '#title' => t('Enable trimming?'),
  61. '#default_value' => $settings['enable_trim'],
  62. );
  63. $element['trim_length'] = array(
  64. '#title' => t('Trim length'),
  65. '#type' => 'textfield',
  66. '#size' => 10,
  67. '#default_value' => $settings['trim_length'],
  68. '#element_validate' => array('element_validate_integer_positive'),
  69. '#states' => array(
  70. 'visible' => array(
  71. ':input[name*="enable_trim"]' => array('checked' => TRUE),
  72. ),
  73. ),
  74. );
  75. $element['title_class'] = array(
  76. '#title' => t('Tag classes'),
  77. '#type' => 'textfield',
  78. '#description' => t('A CSS class to use in the wrapper tag for the title.'),
  79. '#default_value' => $settings['title_class'],
  80. '#element_validate' => array('_title_validate_class'),
  81. );
  82. return $element;
  83. }
  84. /**
  85. * Implements hook_field_formatter_settings_summary().
  86. */
  87. function title_field_formatter_settings_summary($field, $instance, $view_mode) {
  88. $settings = $instance['display'][$view_mode]['settings'];
  89. $summary = array();
  90. $tag = isset($settings['title_style']) && $settings['title_style'] != '' && $settings['title_style'] != '_none' ? $settings['title_style'] : t('- None -');
  91. $summary[] = t('Title wrap tag: @tag', array('@tag' => $tag));
  92. $link_types = array(
  93. 'content' => t('Linked to content'),
  94. 'content_absolute' => t('Linked to content(absolute url)'),
  95. );
  96. // Display this setting only if field is linked.
  97. if (isset($link_types[$settings['title_link']])) {
  98. $summary[] = $link_types[$settings['title_link']];
  99. }
  100. // Display this setting only if wrapper has a class.
  101. if (isset($settings['title_class']) && $settings['title_class'] != '_none' && $settings['title_class'] != '') {
  102. $summary[] = t('Wrap tag classes: @classes', array('@classes' => $settings['title_class']));
  103. }
  104. // Display this setting only if trim is enabled.
  105. if (!empty($settings['enable_trim'])) {
  106. $summary[] = t('Trim length: @length', array('@length' => $settings['trim_length']));
  107. }
  108. return implode('<br />', $summary);
  109. }
  110. /**
  111. * Implements hook_field_formatter_view().
  112. */
  113. function title_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
  114. $settings = $display['settings'];
  115. $output = '';
  116. if (isset($items[0]['safe_value'])) {
  117. $output = $items[0]['safe_value'];
  118. }
  119. elseif (isset($items[0]['value'])) {
  120. $output = _text_sanitize($instance, $langcode, $items[0], 'value');
  121. }
  122. $element = array();
  123. if (!empty($output)) {
  124. if (in_array($settings['title_link'], array(
  125. 'content',
  126. 'content_absolute',
  127. ))) {
  128. $uri = entity_uri($entity_type, $entity);
  129. // If trimmed is enabled.
  130. if (!empty($settings['enable_trim'])) {
  131. $trimmed_output = text_summary($output, $instance['settings']['text_processing'] ? $items[0]['format'] : NULL, $display['settings']['trim_length']);
  132. // If we performed trimmed, add ellipsis in the end.
  133. if (drupal_strlen($output) != drupal_strlen($trimmed_output)) {
  134. $output = $trimmed_output . '&hellip;';
  135. }
  136. }
  137. $options = array(
  138. 'html' => TRUE,
  139. 'absolute' => $settings['title_link'] === 'content_absolute',
  140. );
  141. if (!empty($uri['options'])) {
  142. $options = array_merge($options, $uri['options']);
  143. }
  144. $output = l($output, $uri['path'], $options);
  145. }
  146. $wrap_tag = empty($settings['title_style']) ? '_none' : $settings['title_style'];
  147. if ($wrap_tag != '_none') {
  148. $variables = array(
  149. 'element' => array(
  150. '#tag' => $wrap_tag,
  151. '#value' => $output,
  152. ),
  153. );
  154. if (!empty($settings['title_class'])) {
  155. $variables['element']['#attributes'] = array('class' => $settings['title_class']);
  156. }
  157. $output = theme('html_tag', $variables);
  158. }
  159. $element = array(
  160. array(
  161. '#markup' => $output,
  162. ),
  163. );
  164. }
  165. return $element;
  166. }
  167. /**
  168. * Validate the list of values.
  169. *
  170. * Validate that a space-separated list of values are lowercase and appropriate
  171. * for use as HTML classes.
  172. *
  173. * @see title_field_formatter_settings_form()
  174. */
  175. function _title_validate_class($element, &$form_state) {
  176. static $validated_class = array();
  177. $value = drupal_array_get_nested_value($form_state['values'], $element['#parents']);
  178. $classes = explode(' ', $value);
  179. foreach ($classes as $class) {
  180. if (!isset($validated_class[$class])) {
  181. // Using drupal_clean_css_identifier() directly to allow a double
  182. // underscore in addition to core defaults.
  183. // @see: https://www.drupal.org/node/2009584.
  184. $validated_class[$class] = drupal_clean_css_identifier(
  185. drupal_strtolower($class),
  186. array(
  187. ' ' => '-',
  188. '__' => '__',
  189. '_' => '-',
  190. '/' => '-',
  191. '[' => '-',
  192. ']' => '',
  193. )
  194. );
  195. }
  196. if ($class !== $validated_class[$class]) {
  197. form_error($element, t('Wrapper classes contain illegal characters; classes should be lowercase and may contain letters, numbers, dashes and double underscores.'));
  198. return;
  199. }
  200. }
  201. }