title.field.inc 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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('title_style' => '', 'title_link' => '', 'title_class' => ''),
  15. ),
  16. );
  17. }
  18. /**
  19. * Implements hook_field_formatter_settings_form().
  20. */
  21. function title_field_formatter_settings_form($field, $instance, $view_mode, $form, &$form_state) {
  22. $settings = $instance['display'][$view_mode]['settings'];
  23. $element = array();
  24. $wrap_tags = array(
  25. '_none' => t('- None -'),
  26. 'div' => t('DIV'),
  27. 'h1' => t('H1'),
  28. 'h2' => t('H2'),
  29. 'h3' => t('H3'),
  30. 'h4' => t('H4'),
  31. 'h5' => t('H5'),
  32. 'h6' => t('H6'),
  33. 'span' => t('SPAN'),
  34. );
  35. $element['title_style'] = array(
  36. '#title' => t('Wrap title in tag'),
  37. '#type' => 'select',
  38. '#default_value' => !empty($settings['title_style']) ? $settings['title_style'] : '_none',
  39. '#options' => $wrap_tags,
  40. );
  41. $link_types = array(
  42. 'content' => t('Content'),
  43. );
  44. $element['title_link'] = array(
  45. '#title' => t('Link title to'),
  46. '#type' => 'select',
  47. '#default_value' => $settings['title_link'],
  48. '#empty_option' => t('Nothing'),
  49. '#options' => $link_types,
  50. );
  51. $element['title_class'] = array(
  52. '#title' => t('Tag classes'),
  53. '#type' => 'textfield',
  54. '#description' => t('A CSS class to use in the wrapper tag for the title.'),
  55. '#default_value' => $settings['title_class'],
  56. '#element_validate' => array('_title_validate_class'),
  57. );
  58. return $element;
  59. }
  60. /**
  61. * Implements hook_field_formatter_settings_summary().
  62. */
  63. function title_field_formatter_settings_summary($field, $instance, $view_mode) {
  64. $settings = $instance['display'][$view_mode]['settings'];
  65. $summary = array();
  66. $tag = isset($settings['title_style']) && $settings['title_style'] != '' && $settings['title_style'] != '_none' ? $settings['title_style'] : t('- None -');
  67. $summary[] = t('Title wrap tag: @tag', array('@tag' => $tag));
  68. $link_types = array(
  69. 'content' => t('Linked to content'),
  70. );
  71. // Display this setting only if field is linked.
  72. if (isset($link_types[$settings['title_link']])) {
  73. $summary[] = $link_types[$settings['title_link']];
  74. }
  75. // Display this setting only if wrapper has a class.
  76. if (isset($settings['title_class']) && $settings['title_class'] != '_none' && $settings['title_class'] != '') {
  77. $summary[] = t('Wrap tag classes: @classes', array('@classes' => $settings['title_class']));
  78. }
  79. return implode('<br />', $summary);
  80. }
  81. /**
  82. * Implements hook_field_formatter_view().
  83. */
  84. function title_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
  85. $settings = $display['settings'];
  86. $output = '';
  87. if (isset($items[0]['safe_value'])) {
  88. $output = $items[0]['safe_value'];
  89. }
  90. elseif (isset($items[0]['value'])) {
  91. $output = _text_sanitize($instance, $langcode, $items[0], 'value');
  92. }
  93. $element = array();
  94. if (!empty($output)) {
  95. if ($settings['title_link'] == 'content') {
  96. $uri = entity_uri($entity_type, $entity);
  97. $options = array('html' => TRUE);
  98. if (!empty($uri['options'])) {
  99. $options = array_merge($options, $uri['options']);
  100. }
  101. $output = l($output, $uri['path'], $options);
  102. }
  103. $wrap_tag = empty($settings['title_style']) ? '_none' : $settings['title_style'];
  104. if ($wrap_tag != '_none') {
  105. $variables = array(
  106. 'element' => array(
  107. '#tag' => $wrap_tag,
  108. '#value' => $output,
  109. ),
  110. );
  111. if (!empty($settings['title_class'])) {
  112. $variables['element']['#attributes'] = array('class' => $settings['title_class']);
  113. }
  114. $output = theme('html_tag', $variables);
  115. }
  116. $element = array(
  117. array(
  118. '#markup' => $output,
  119. ),
  120. );
  121. }
  122. return $element;
  123. }
  124. /**
  125. * Validate that a space-separated list of values are lowercase and appropriate for use as HTML classes.
  126. *
  127. * @see title_field_formatter_settings_form()
  128. */
  129. function _title_validate_class($element, &$form_state) {
  130. $value = drupal_array_get_nested_value($form_state['values'], $element['#parents']);
  131. $classes = explode(' ', $value);
  132. foreach ($classes as $class) {
  133. if ($class != drupal_html_class($class)) {
  134. form_error($element, t('Wrapper classes contain illegal characters; classes should be lowercase and may contain letters, numbers, and dashes.'));
  135. return;
  136. }
  137. }
  138. }