views_rss_handler_field_term_node_tid.inc 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. <?php
  2. /**
  3. * @file
  4. * Field handler to provide additional control for the All Taxonomy Terms field.
  5. */
  6. class views_rss_handler_field_term_node_tid extends views_handler_field_term_node_tid {
  7. function option_definition() {
  8. $options = parent::option_definition();
  9. $options['rss_domain'] = array('default' => 'none');
  10. $options['rss_include_parents'] = array('default' => 0);
  11. $options['rss_include_vocabulary'] = array('default' => 0);
  12. return $options;
  13. }
  14. function options_form(&$form, &$form_state) {
  15. parent::options_form($form, $form_state);
  16. // New display type: "RSS <category> element".
  17. $form['type']['#weight'] = 2;
  18. $form['separator']['#weight'] = 3;
  19. $form['type']['#options']['rss_category'] = t('RSS &lt;category&gt; element');
  20. // Additional options for "domain" attribute.
  21. $form['rss_domain'] = array(
  22. '#type' => 'select',
  23. '#title' => t('<em>Domain</em> attribute'),
  24. '#description' => t("<em>domain</em> attribute identifies the category's taxonomy.") . ' ' . l('[?]', 'http://www.rssboard.org/rss-profile#element-channel-item-category', array('attributes' => array('title' => t('Need more information?')))),
  25. '#options' => array(
  26. 'none' => t('None'),
  27. 'path' => t('Path'),
  28. 'alias' => t('URL alias'),
  29. ),
  30. '#default_value' => $this->options['rss_domain'],
  31. '#dependency' => array('radio:options[type]' => array('rss_category')),
  32. '#weight' => 3,
  33. );
  34. // Output terms with their parents.
  35. // Example: parent2/parent1/term
  36. $form['rss_include_parents'] = array(
  37. '#type' => 'checkbox',
  38. '#title' => t('Include term parents'),
  39. '#description' => t('Output terms from hierarchical vocabularies together with their parents (slash-delimited).'),
  40. '#default_value' => $this->options['rss_include_parents'],
  41. '#dependency' => array('radio:options[type]' => array('rss_category')),
  42. '#weight' => 4,
  43. );
  44. // Output terms with their parents and vocabulary.
  45. // Example: vocabulary/parent2/parent1/term
  46. $form['rss_include_vocabulary'] = array(
  47. '#type' => 'checkbox',
  48. '#title' => t('Include term vocabulary'),
  49. '#description' => t('Add vocabulary name before term parents.'),
  50. '#default_value' => $this->options['rss_include_vocabulary'],
  51. '#process' => array('ctools_dependent_process'),
  52. '#dependency_count' => 2,
  53. '#dependency' => array(
  54. 'radio:options[type]' => array('rss_category'),
  55. 'edit-options-rss-include-parents' => array(1),
  56. ),
  57. '#weight' => 5,
  58. );
  59. // Hide "Link this field to its term page" checkbox
  60. // if "RSS <category> element" is selected as "Display type".
  61. $form['link_to_taxonomy']['#weight'] = 4;
  62. $form['link_to_taxonomy']['#states'] = array(
  63. 'visible' => array(
  64. ':input[name="options[type]"]' => array('!value' => 'rss_category'),
  65. ),
  66. );
  67. }
  68. function pre_render(&$values) {
  69. parent::pre_render($values);
  70. // For "RSS <category> element" display type make sure
  71. // that field value will never be rendered as link.
  72. if ($this->options['type'] == 'rss_category') {
  73. foreach ($this->items as $node_nid => $terms) {
  74. foreach ($terms as $term_tid => $term) {
  75. if (isset($term['make_link'])) {
  76. $this->items[$node_nid][$term_tid]['make_link'] = FALSE;
  77. }
  78. if ($this->options['rss_domain'] != 'none' && empty($this->options['link_to_taxonomy'])) {
  79. $this->items[$node_nid][$term_tid]['path'] = 'taxonomy/term/' . $term_tid;
  80. }
  81. if ($this->options['rss_include_parents']) {
  82. $this->items[$node_nid][$term_tid]['parents'] = taxonomy_get_parents_all($term_tid);
  83. }
  84. if ($this->options['rss_include_vocabulary']) {
  85. $this->items[$node_nid][$term_tid]['vocabulary'] = $term['vocabulary'];
  86. }
  87. }
  88. }
  89. }
  90. }
  91. function render_items($items) {
  92. if (!empty($items)) {
  93. if ($this->options['type'] == 'rss_category') {
  94. return implode('', $items);
  95. }
  96. // Default render_items().
  97. return parent::render_items($items);
  98. }
  99. }
  100. function render_item($count, $item) {
  101. if ($this->options['type'] == 'rss_category') {
  102. // Basic XML element details.
  103. $rss_element = array(
  104. 'key' => 'category',
  105. 'value' => $item['name'],
  106. );
  107. // Slash-delimited list of all parents with the term at the end.
  108. // $item['parents'] array contains the current term as well.
  109. if ($this->options['rss_include_parents'] && isset($item['parents'])) {
  110. $parents = array();
  111. // Add vocabulary if required.
  112. if ($this->options['rss_include_vocabulary'] && isset($item['vocabulary'])) {
  113. $parents[] = $item['vocabulary'];
  114. }
  115. // Add all terms (parent and current one).
  116. foreach (array_reverse($item['parents']) as $parent) {
  117. $parents[] = $parent->name;
  118. }
  119. if (count($parents)) {
  120. $rss_element['value'] = implode('/', $parents);
  121. }
  122. }
  123. // Add "domain" attribute if required.
  124. if ($this->options['rss_domain'] == 'path') {
  125. $rss_element['attributes']['domain'] = url('<front>', array('absolute' => TRUE)) . $item['path'];
  126. }
  127. elseif ($this->options['rss_domain'] == 'alias') {
  128. $rss_element['attributes']['domain'] = url($item['path'], array('absolute' => TRUE));
  129. }
  130. // Prepare raw-rendered values to store in view results.
  131. $rendered = array(
  132. '#item' => $item,
  133. '#markup' => format_xml_elements(array($rss_element)),
  134. '#rss_element' => $rss_element,
  135. '#settings' => $this->options,
  136. );
  137. // Store raw-rendered values in view results, the same way
  138. // as values from standard field formatters are added.
  139. $row_index = $this->view->row_index;
  140. $field_name = 'field_' . $this->field;
  141. $this->view->result[$row_index]->{$field_name}[$count] = array(
  142. 'rendered' => $rendered,
  143. 'raw' => $item,
  144. );
  145. // Return formatted XML element.
  146. return format_xml_elements(array($rss_element));
  147. }
  148. // Standard Views render_item().
  149. return parent::render_item($count, $item);
  150. }
  151. }