terms_from_node.inc 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. }
  42. if (!empty($terms)) {
  43. $all_terms = ctools_break_phrase(implode($conf['concatenator'], $terms));
  44. return ctools_context_create('terms', $all_terms);
  45. }
  46. }
  47. /**
  48. * Settings form for the relationship.
  49. */
  50. function ctools_terms_from_node_settings_form($form, &$form_state) {
  51. $conf = $form_state['conf'];
  52. $options = array();
  53. foreach (taxonomy_vocabulary_get_names() as $name => $vocabulary) {
  54. $options[$name] = $vocabulary->name;
  55. }
  56. $form['vocabulary'] = array(
  57. '#title' => t('Vocabulary'),
  58. '#type' => 'checkboxes',
  59. '#options' => $options,
  60. '#default_value' => $conf['vocabulary'],
  61. '#prefix' => '<div class="clearfix">',
  62. '#suffix' => '</div>',
  63. );
  64. $form['concatenator'] = array(
  65. '#title' => t('Concatenator'),
  66. '#type' => 'select',
  67. '#options' => array(',' => ', (AND)', '+' => '+ (OR)'),
  68. '#default_value' => $conf['concatenator'],
  69. '#prefix' => '<div class="clearfix">',
  70. '#suffix' => '</div>',
  71. '#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)."),
  72. );
  73. return $form;
  74. }