2

I have a taxonomy based menu (3 tiers) that displays a specific content type. Content items are always tagged at the lowest level child (*) so when the menu items are selected a range of content is displayed.

top-level | mid-level1 | child1*
          |            | child2*
          |
          | mid-level2 - child3*
          |
          | child4*

When the parent levels are selected, I would like a summary page for the parent to show. I am using views and panels and getting the lowest level children to show is easily accomplished, but how do I set a filter or context to determine if the view is not at the lowest level?

Panels exposes a context that selects content with a parent (or not) so I can easily identify the top level menu selections, but the bottom children can occur at either level two or three and there is no context for 'has children'.

What can I use as a context to select the parents and which hook to use?

0

1 Answer 1

1

This is an excellent example of a custom Ctools plugin. In this case, our plugin type is "Access". The below is copied from "term_has_parent", and adopted to search for the reverse. It could probably be improved with some more robust error checking.

In Panels, you will get a new "Selection Rule", so that you can distinguish between terms that have children, from those that don't.

<?php
/**
 * Plugins are described by creating a $plugin array which will be used
 * by the system that includes this file.
 */
$plugin = array(
  'title' => t("Taxonomy: term has child(ren)"),
  'description' => t('Control access if a term has children.'),
  'callback' => 'term_has_child_ctools_access_check',
  'default' => array('negate' => 0),
  'summary' => 'term_has_child_ctools_access_summary',
  'required context' => new ctools_context_required(t('Term'), array('taxonomy_term', 'terms')),
);

/**
 * Check for access.
 */
function term_has_child_ctools_access_check($conf, $context) {
  $children = db_select('taxonomy_term_hierarchy', 't')
    ->fields('t')
    ->condition('parent', $context->data->tid)
    ->range(0, 1)
    ->execute()
    ->fetchAssoc();

  return count($children) > 1;
}

/**
 * Provide a summary description based upon the checked terms.
 */
function term_has_child_ctools_access_summary($conf, $context) {
  return 'Returns true if the current term has children';
}

If you don't know how to use a custom Ctools plugin, I suggest that is the topic of another question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.