CTools Access plugin: Check if term has children

Public

Allows you to use this as a Panels selection tool for term_view. Check if the current term has child terms.

Get raw version
php
  1. <?php
  2. /**
  3.  * @file
  4.  * Plugin to provide access control based upon a terms children.
  5.  */
  6.  
  7. /**
  8.  * Plugins are described by creating a $plugin array which will be used
  9.  * by the system that includes this file.
  10.  */
  11. $plugin = array(
  12. 'title' => t('Taxonomy term: has children'),
  13. 'description' => t('Does this term has child terms?.'),
  14. 'callback' => 'mymodule_term_has_children_access_check',
  15. 'default' => array('negate' => 0),
  16. 'settings form' => 'mymodule_term_has_children_access_settings',
  17. 'summary' => 'mymodule_term_has_children_access_summary',
  18. 'required context' => new ctools_context_required(t('Term'), array('taxonomy_term', 'terms')),
  19. );
  20.  
  21. function mymodule_term_has_children_access_settings($form, &$form_state, $conf) {
  22. // If no configuration was saved before, set some defaults.
  23. if (empty($conf)) {
  24. $conf = array(
  25. 'vid' => 0,
  26. );
  27. }
  28. if (!isset($conf['vid'])) {
  29. $conf['vid'] = 0;
  30. }
  31.  
  32. $form['settings']['vid'] = array(
  33. '#title' => t('Vocabulary'),
  34. '#type' => 'select',
  35. '#options' => array(),
  36. '#description' => t('Select the vocabulary for this form.'),
  37. '#id' => 'ctools-select-vid',
  38. '#default_value' => $conf['vid'],
  39. '#required' => TRUE,
  40. );
  41.  
  42. ctools_include('dependent');
  43. $options = array();
  44.  
  45. // A note: Dependency works strangely on these forms as they have never been
  46. // updated to a more modern system so they are not individual forms of their
  47. // own like the content types.
  48.  
  49. $form['settings']['#tree'] = TRUE;
  50.  
  51. // Loop over each of the configured vocabularies.
  52. foreach (taxonomy_get_vocabularies() as $vid => $vocabulary) {
  53. $options[$vid] = $vocabulary->name;
  54. }
  55. $form['settings']['vid']['#options'] = $options;
  56.  
  57. return $form;
  58. }
  59.  
  60. function mymodule_term_has_children_access_check($conf, $context) {
  61. // As far as I know there should always be a context at this point, but this
  62. // is safe.
  63. if (empty($context) || empty($context->data) || empty($context->data->vid) || empty($context->data->tid)) {
  64. return FALSE;
  65. }
  66. // Get the $vid.
  67. if (!isset($conf['vid'])) {
  68. return FALSE;
  69. }
  70.  
  71. if ($conf['vid'] == $context->data->vid) {
  72. $term_children = taxonomy_get_children($context->data->tid, $context->data->vid);
  73. if (!empty($term_children)) {
  74. return TRUE;
  75. }
  76. else {
  77. return FALSE;
  78. }
  79. }
  80.  
  81. return FALSE;
  82. }
  83.  
  84. function mymodule_term_has_children_access_summary() {
  85.  
  86. }

Comments

Vlad Moyseenko's picture

Thanks! You saved me a lot of time of researching.