term_from_node.inc 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. <?php
  2. /**
  3. * @file
  4. * Plugin to provide an relationship handler for term 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('Term from node'),
  12. 'keyword' => 'term',
  13. 'description' => t('Adds a taxonomy term from a node context; if multiple terms are selected, this will get the "first" term only.'),
  14. 'required context' => new ctools_context_required(t('Node'), 'node'),
  15. 'context' => 'ctools_term_from_node_context',
  16. 'edit form' => 'ctools_term_from_node_settings_form',
  17. 'defaults' => array('vid' => ''),
  18. );
  19. /**
  20. * Return a new context based on an existing context.
  21. */
  22. function ctools_term_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('entity:taxonomy_term', NULL);
  26. }
  27. if (isset($context->data->taxonomy)) {
  28. foreach ($context->data->taxonomy as $term) {
  29. if ($term->vid == $conf['vid']) {
  30. return ctools_context_create('entity:taxonomy_term', $term);
  31. }
  32. }
  33. }
  34. }
  35. /**
  36. * Settings form for the relationship.
  37. */
  38. function ctools_term_from_node_settings_form($form, &$form_state) {
  39. $conf = $form_state['conf'];
  40. $options = array();
  41. foreach (taxonomy_get_vocabularies() as $vid => $vocabulary) {
  42. $options[$vid] = $vocabulary->name;
  43. }
  44. $form['vid'] = array(
  45. '#title' => t('Vocabulary'),
  46. '#type' => 'select',
  47. '#options' => $options,
  48. '#default_value' => $conf['vid'],
  49. '#prefix' => '<div class="clearfix">',
  50. '#suffix' => '</div>',
  51. );
  52. return $form;
  53. }