title.field.inc 4.2 KB

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