terms_from_node.inc 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <?php
  2. /**
  3. * @file
  4. * Plugin to provide an relationship handler for all terms from node.
  5. */
  6. /**
  7. * Plugins are described by creating a $plugin array which will be used
  8. * by the system that includes this file.
  9. */
  10. $plugin = array(
  11. 'title' => t('Multiple terms from node'),
  12. 'keyword' => 'terms',
  13. 'description' => t('Adds a taxonomy terms from a node context; if multiple terms are selected, they wil be concatenated.'),
  14. 'required context' => new ctools_context_required(t('Node'), 'node'),
  15. 'context' => 'ctools_terms_from_node_context',
  16. 'edit form' => 'ctools_terms_from_node_settings_form',
  17. 'defaults' => array('vocabulary' => array(), 'concatenator' => ','),
  18. );
  19. /**
  20. * Return a new context based on an existing context.
  21. */
  22. function ctools_terms_from_node_context($context, $conf) {
  23. // If unset it wants a generic, unfilled context, which is just NULL.
  24. if (empty($context->data)) {
  25. return ctools_context_create_empty('terms', NULL);
  26. }
  27. // Collect all terms for the chosen vocabulary and concatenate them.
  28. $node = $context->data;
  29. $terms = array();
  30. $fields = field_info_instances('node', $node->type);
  31. foreach ($fields as $name => $info) {
  32. $field_info = field_info_field($name);
  33. if ($field_info['type'] == 'taxonomy_term_reference' && (empty($conf['vocabulary']) || !empty($conf['vocabulary'][$field_info['settings']['allowed_values'][0]['vocabulary']]))) {
  34. $items = field_get_items('node', $node, $name);
  35. if (is_array($items)) {
  36. foreach ($items as $item) {
  37. $terms[] = $item['tid'];
  38. }
  39. }
  40. }
  41. elseif ($field_info['type'] == 'entityreference' && $field_info['settings']['target_type'] == 'taxonomy_term') {
  42. $items = field_get_items('node', $node, $name);
  43. if (is_array($items)) {
  44. $tids = array();
  45. foreach ($items as $item) {
  46. $tids[] = $item['target_id'];
  47. }
  48. $term_objects = taxonomy_term_load_multiple($tids);
  49. foreach ($term_objects as $term) {
  50. if (empty($conf['vocabulary']) || in_array($term->vocabulary_machine_name, $conf['vocabulary'])) {
  51. $terms[] = $term->tid;
  52. }
  53. }
  54. }
  55. }
  56. }
  57. if (!empty($terms)) {
  58. $all_terms = ctools_break_phrase(implode($conf['concatenator'], $terms));
  59. return ctools_context_create('terms', $all_terms);
  60. }
  61. }
  62. /**
  63. * Settings form for the relationship.
  64. */
  65. function ctools_terms_from_node_settings_form($form, &$form_state) {
  66. $conf = $form_state['conf'];
  67. $options = array();
  68. foreach (taxonomy_vocabulary_get_names() as $name => $vocabulary) {
  69. $options[$name] = $vocabulary->name;
  70. }
  71. $form['vocabulary'] = array(
  72. '#title' => t('Vocabulary'),
  73. '#type' => 'checkboxes',
  74. '#options' => $options,
  75. '#default_value' => $conf['vocabulary'],
  76. '#prefix' => '<div class="clearfix">',
  77. '#suffix' => '</div>',
  78. );
  79. $form['concatenator'] = array(
  80. '#title' => t('Concatenator'),
  81. '#type' => 'select',
  82. '#options' => array(',' => ', (AND)', '+' => '+ (OR)'),
  83. '#default_value' => $conf['concatenator'],
  84. '#prefix' => '<div class="clearfix">',
  85. '#suffix' => '</div>',
  86. '#description' => t("When the value from this context is passed on to a view as argument, the terms can be concatenated in the form of 1+2+3 (for OR) or 1,2,3 (for AND)."),
  87. );
  88. return $form;
  89. }