views_plugin_argument_default_taxonomy_tid.inc

  1. 3.x modules/taxonomy/views_plugin_argument_default_taxonomy_tid.inc
  2. 2.x modules/taxonomy/views_plugin_argument_default_taxonomy_tid.inc

Taxonomy tid default argument.

File

modules/taxonomy/views_plugin_argument_default_taxonomy_tid.inc
View source
  1. <?php
  2. /**
  3. * @file
  4. * Taxonomy tid default argument.
  5. */
  6. class views_plugin_argument_default_taxonomy_tid extends views_plugin_argument_default {
  7. var $option_name = 'default_taxonomy_tid';
  8. function option_definition() {
  9. $options = parent::option_definition();
  10. $options[$this->option_name . '_term_page'] = array('default' => TRUE);
  11. $options[$this->option_name . '_node'] = array('default' => FALSE);
  12. $options[$this->option_name . '_limit'] = array('default' => FALSE);
  13. $options[$this->option_name . '_vids'] = array('default' => array());
  14. return $options;
  15. }
  16. function argument_form(&$form, &$form_state) {
  17. $form[$this->option_name . '_term_page'] = array(
  18. '#type' => 'checkbox',
  19. '#title' => t('Load default argument from term page'),
  20. '#default_value' => $this->argument->options[$this->option_name . '_term_page'],
  21. '#process' => array('views_process_dependency'),
  22. '#dependency' => array(
  23. 'radio:options[default_action]' => array('default'),
  24. 'radio:options[default_argument_type]' => array($this->id)
  25. ),
  26. '#dependency_count' => 2,
  27. );
  28. $form[$this->option_name . '_node'] = array(
  29. '#type' => 'checkbox',
  30. '#title' => t('Load default argument from node page, thats good for related taxonomy blocks'),
  31. '#default_value' => $this->argument->options[$this->option_name . '_node'],
  32. '#process' => array('views_process_dependency'),
  33. '#dependency' => array(
  34. 'radio:options[default_action]' => array('default'),
  35. 'radio:options[default_argument_type]' => array($this->id)
  36. ),
  37. '#dependency_count' => 2,
  38. );
  39. $form[$this->option_name . '_limit'] = array(
  40. '#type' => 'checkbox',
  41. '#title' => t('Limit terms by vocabulary'),
  42. '#default_value'=> $this->argument->options[$this->option_name . '_limit'],
  43. '#process' => array('views_process_dependency'),
  44. '#dependency' => array(
  45. 'radio:options[default_action]' => array('default'),
  46. 'radio:options[default_argument_type]' => array($this->id),
  47. 'edit-options-default-taxonomy-tid-node' => array(1),
  48. ),
  49. '#dependency_count' => 3,
  50. );
  51. $options = array();
  52. $vocabularies = taxonomy_get_vocabularies();
  53. foreach ($vocabularies as $voc) {
  54. $options[$voc->vid] = check_plain($voc->name);
  55. }
  56. $form[$this->option_name . '_vids'] = array(
  57. '#prefix' => '<div><div id="edit-options-default-taxonomy-tid-vids-wrapper">',
  58. '#suffix' => '</div></div>',
  59. '#type' => 'checkboxes',
  60. '#title' => t('Vocabularies'),
  61. '#description' => t('If you wish to limit terms for specific vocabularies, check them; if none are checked, all terms will be included.'),
  62. '#options' => $options,
  63. '#default_value' => isset($this->argument->options[$this->option_name . '_vids']) ? $this->argument->options[$this->option_name . '_vids'] : array(),
  64. '#process' => array('expand_checkboxes', 'views_process_dependency'),
  65. '#dependency' => array(
  66. 'radio:options[default_action]' => array('default'),
  67. 'radio:options[default_argument_type]' => array($this->id),
  68. 'edit-options-default-taxonomy-tid-limit' => array(1),
  69. 'edit-options-default-taxonomy-tid-node' => array(1),
  70. ),
  71. '#dependency_count' => 4,
  72. );
  73. }
  74. function get_argument() {
  75. // Load default argument from taxonomy page.
  76. if (!empty($this->argument->options[$this->option_name . '_term_page'])) {
  77. if (arg(0) == 'taxonomy' && arg(1) == 'term' && is_numeric(arg(2))) {
  78. return arg(2);
  79. }
  80. }
  81. // Load default argument from node.
  82. if (!empty($this->argument->options[$this->option_name . '_node'])) {
  83. foreach (range(1, 3) as $i) {
  84. $node = menu_get_object('node', $i);
  85. if (!empty($node)) {
  86. break;
  87. }
  88. }
  89. // Just check, if a node could be detected.
  90. if ($node) {
  91. if (!empty($this->argument->options[$this->option_name . '_limit'])) {
  92. $tids = array();
  93. // Filter by vid.
  94. foreach ($node->taxonomy as $tid => $term) {
  95. if (!empty($this->argument->options[$this->option_name . '_vids'][$term->vid])) {
  96. $tids[] = $tid;
  97. }
  98. }
  99. return implode(",", $tids);
  100. }
  101. // Return all tids.
  102. else {
  103. return implode(",", array_keys($node->taxonomy));
  104. }
  105. }
  106. }
  107. // If the current page is a view that takes tid as an argument,
  108. // find the tid argument and return it.
  109. $views_page = views_get_page_view();
  110. if ($views_page && isset($views_page->view->argument['tid'])) {
  111. return $views_page->view->argument['tid']->argument;
  112. }
  113. }
  114. }