term_parent.inc 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. <?php
  2. /**
  3. * @file relationships/term_parent.inc
  4. * Plugin to provide an relationship handler for term parent.
  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 parent'),
  12. 'keyword' => 'parent_term',
  13. 'description' => t('Adds a taxonomy term parent from a term context.'),
  14. 'required context' => new ctools_context_required(t('Term'), 'entity:taxonomy_term'),
  15. 'context' => 'ctools_term_parent_context',
  16. 'edit form' => 'ctools_term_parent_settings_form',
  17. 'defaults' => array('type' => 'top'),
  18. );
  19. /**
  20. * Return a new context based on an existing context.
  21. */
  22. function ctools_term_parent_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');
  26. }
  27. if (isset($context->data)) {
  28. $result = db_query('SELECT t1.* FROM {taxonomy_term_hierarchy} t1 INNER JOIN {taxonomy_term_hierarchy} t2 ON t1.tid = t2.parent WHERE t2.tid = :tid', array(':tid' => $context->data->tid))->fetchAssoc();
  29. // If top level term, keep looking up until we see a top level.
  30. if ($conf['type'] == 'top') {
  31. // If looking for top level, and there are no parents at all, make sure
  32. // the current term is the 'top level'.
  33. if (empty($result)) {
  34. $result['tid'] = $context->data->tid;
  35. }
  36. while (!empty($result['parent'])) {
  37. $result = db_query('SELECT * FROM {taxonomy_term_hierarchy} WHERE tid = :tid', array(':tid' => $result['parent']))->fetchAssoc();
  38. }
  39. }
  40. // Load the term.
  41. if ($result) {
  42. $term = taxonomy_term_load($result['tid']);
  43. return ctools_context_create('entity:taxonomy_term', $term);
  44. }
  45. }
  46. }
  47. /**
  48. * Settings form for the relationship.
  49. */
  50. function ctools_term_parent_settings_form($form, &$form_state) {
  51. $conf = $form_state['conf'];
  52. $form['type'] = array(
  53. '#type' => 'select',
  54. '#title' => t('Relationship type'),
  55. '#options' => array('parent' => t('Immediate parent'), 'top' => t('Top level term')),
  56. '#default_value' => $conf['type'],
  57. );
  58. return $form;
  59. }