title.field.inc 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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 = isset($items[0]) ? $items[0]['safe_value'] : '';
  87. if (!empty($output) && $settings['title_link'] == 'content') {
  88. $uri = entity_uri($entity_type, $entity);
  89. $output = l($output, $uri['path'], array('html' => TRUE));
  90. }
  91. $wrap_tag = empty($settings['title_style']) ? '_none' : $settings['title_style'];
  92. if ($wrap_tag != '_none') {
  93. $element = array(
  94. 'element' => array(
  95. '#tag' => $wrap_tag,
  96. '#value' => $output,
  97. ),
  98. );
  99. if (!empty($settings['title_class'])) {
  100. $element['#attributes'] = array('class' => $settings['title_class']);
  101. }
  102. $output = theme('html_tag', $element);
  103. }
  104. $element = array(
  105. array(
  106. '#markup' => $output,
  107. ),
  108. );
  109. return $element;
  110. }
  111. /**
  112. * Validate that a space-separated list of values are lowercase and appropriate for use as HTML classes.
  113. *
  114. * @see title_field_formatter_settings_form()
  115. */
  116. function _title_validate_class($element, &$form_state) {
  117. $value = drupal_array_get_nested_value($form_state['values'], $element['#parents']);
  118. $classes = explode(' ', $value);
  119. foreach ($classes as $class) {
  120. if ($class != drupal_html_class($class)) {
  121. form_error($element, t('Wrapper classes contain illegal characters; classes should be lowercase and may contain letters, numbers, and dashes.'));
  122. return;
  123. }
  124. }
  125. }