2

On my Drupal 7 site, I have a module which needs to determine the type of page being shown / being loaded.

I first implemented hook_entity_view_alter. Doing this:

/**
 * Implements hook_entity_view_alter
 */
/*
function rsc_search_entity_view_alter(&$build, $type)
{
  echo "$type <br/>";
  echo $build['#entity_type'].'<br/>';
}

shows the info I need, so I know that the hook is being called. BUT, on taxonomy term pages, nothing shows up, although there should be an entity with type 'taxonomy_term'. See the last few lines of code at: http://api.drupal.org/api/drupal/modules!taxonomy!taxonomy.module/function/taxonomy_term_view/7

So I tried the next thing, which is implementing hook_taxonomy_term_view_alter to catch those term pages:

/**
 * Implements hook_taxonomy_term_view_alter 
 */
function rsc_search_taxonomy_term_view_alter(&$build)
{
  $var = $build;
  $build['extra'] = array('#markup'=>'nothing');
  echo 'tax_term';
  if ($build['#view_mode']=='full')
  {
    echo '<pre>';
    var_dump($build);
    echo '</pre>';
  }
}

This, however, NEVER DOES ANYTHING!

As far as I understand the documentation, I * used the correct function names * used the correct function parameters * remembered to clear the caches

Any ideas on what went wrong? Why can't I implement taxonomy alteration hooks?

EDIT

Clive asked whether I am using views taxonomy. But I am using a custom taxonomy page, which still calls taxonomy_term_view

function CSL_custom_admin_menu_alter(&$items)
{
  $items['taxonomy/term/%taxonomy_term'] = array(
    'title' => 'Taxonomy term',
    'title callback' => 'taxonomy_term_title',
    'title arguments' => array(2),
    'page callback' => 'taxonomy_term_page',
    'page arguments' => array(2),
    'access arguments' => array('access content'),
    'file' => 'taxonomy.pages.inc',
    'file path' => 'sites/all/modules/CSL_custom_admin'
  );
}

Here is an extract from my taxonomy.pages.inc:

/**
 * Menu callback; displays all nodes associated with a term.
 *
 * @param $term
 *   The taxonomy term.
 * @return
 *   The page content.
 */
function taxonomy_term_page($term)
{
  drupal_set_breadcrumb(CSL_taxonomy_breadcrumb($term));

  drupal_add_feed('taxonomy/term/' . $term->tid . '/feed', 'RSS - ' . $term->name);

  $subs = CSL_taxonomy_subs_count(taxonomy_get_tree($term->vid, $term->tid, 1));

  $nids = CSL_taxonomy_nids($term->tid, TRUE, variable_get('default_nodes_main', 10), ( isset($term->field_enable_sort['und'][0]['value'])&&$term->field_enable_sort['und'][0]['value'] ) || ( isset($term->field_toelaat_sorteer['und'][0]['value'])&&$term->field_toelaat_sorteer['und'][0]['value'] ) );

  $build = array();

  // Add term heading if the term has a description
  if (!empty($term->description))
  {
    $build['term_heading'] = array(
        '#prefix' => '<div class="term-listing-heading">',
        '#suffix' => '</div>',
        'term' => taxonomy_term_view($term, 'full'),
        '#weight' => 1
    );
  }

  if ($nids && $subs)
  {
    $build['subterms'] = array
    (
        '#prefix' => '<div class="taxonomy-subterms-wrapper">',
        '#suffix' => '</div>',
        'subterms' => array
        (
            '#theme' => 'CSL_custom_admin_taxsubstable',
            '#rows' => $subs,
            '#weight' => 2
        )
    );

    $build['nodes'] = array
    (
        '#prefix' => '<div class="taxonomy-nodes-wrapper">',
        '#suffix' => '</div>',
        'nodes' => node_view_multiple(node_load_multiple($nids)),
        '#weight' => 3
    );
  }
  else if ($nids) // no subs
  {
    $build['subterms'] = array
    (
        '#prefix' => '<div class="taxonomy-subterms-wrapper">',
        '#suffix' => '</div>',
        '#markup' => '<strong>'.t('This term has no subcategories.').'</strong>',
        '#weight' => 2
    );

    $build['nodes'] = array
    (
        '#prefix' => '<div class="taxonomy-nodes-wrapper">',
        '#suffix' => '</div>',
        'nodes' => node_view_multiple(node_load_multiple($nids)),
        '#weight' => 3
    );
  }
  else if ($subs) // no nids
  {
    $build['subterms'] = array
    (
        '#prefix' => '<div class="taxonomy-subterms-wrapper">',
        '#suffix' => '</div>',
        'subterms' => array
        (
            '#theme' => 'CSL_custom_admin_taxsubstable',
            '#rows' => $subs
        ),
        '#weight' => 2
    );

    $build['nodes'] = array
    (
        '#prefix' => '<div class="taxonomy-nodes-wrapper">',
        '#suffix' => '</div>',
        '#markup' => '<strong>'.t('Refer to one of the subcategories for articles.').'</strong>',
        '#weight' => 3
    );
  }
  else // nothing to see
  {
    $build['nodes'] = array
    (
        '#prefix' => '<div class="taxonomy-nodes-wrapper">',
        '#suffix' => '</div>',
        '#markup' => '<strong>'.t('There is currently no content in this category.').'</strong>',
        '#weight' => 3
    );
  }

  $build['pager_top'] = array(
      '#theme' => 'pager',
      '#weight' => -5
  );

  $build['pager_bottom'] = array(
      '#theme' => 'pager',
      '#weight' => 5
  );

  return $build;
}

I know my module names should not start with a capital letter (CSL_), but that should not affect anything here.

  • Any chance you're using the taxonomy page provided by the Views module instead of the default one? That would stop the hook from being called – Clive Jul 3 '12 at 20:57
  • No, I am using the core taxonomy module, with the exception of tagonomy.pages.inc . I have overridden the menu callback for taxonomy pages, but my override still calls taxonomy_term_view, which calls drupal_alter, which should invoke hook_taxonomy_term_view_alter. I will edit the above post and add my taxonomy.pages.inc and menu callback. – rudolfbyker Jul 3 '12 at 21:31
  • I just saw some strange bahaviour today... I was moving on to other things on the site, and I installed the Relation Module. To make the relation module work, I had to update CTools (from some rc to 7.x-1.0). So I updated all my modules (on the dev site). Suddenly, rsc_search_taxonomy_term_view_alter started outputting the echo and var_dump mentioned above... Everything else still works fine. My problem is solved; I just hope the solution is persistent. This leads to the next question, however: What triggered the change? Was it a bug in CTools rc perhaps? – rudolfbyker Jul 4 '12 at 21:36
  • And then it disappeared... The old behaviour is back for no apparrent reason. Once again, no hook is ever called in which an entity of type 'taxonomy_term' appears. – rudolfbyker Jul 4 '12 at 23:24
2

It is a bug / missing feature in Drupal Core!

The taxonomy module does not exactly conform to standards... For example, where an $entity of type 'node' has the properties bundle and content, 'taxonomy_term' doesn't. Also, in core taxonomy, _view hooks are never called and _alter hooks are only called when the taxonomy term has a non-blank description field. There are some patches available in this discussion, but I wrote my own, because I override taxonomy.pages.inc in any case.

I see a lot of modules struggle with this, and it is still an arguing point for Drupal 8. http://drupal.org/search/apachesolr_search/hook_taxonomy_term_view

Thanks for pointing me in the right direction, Clive. I would otherwise not have looked at my taxonomy.pages.inc

An excerpt from my new custom taxonomy.pages.inc follows. I hope this helps someone!

function taxonomy_term_page($term)
{  
  // set breadcrumbs
  // add rss feeds
  // count subcategories and nodes

  $build = array();

  $view_mode = 'full';
  $langcode = $GLOBALS['language_content']->language;

  // NB Always call this, even if no description,
  // because other modules need hook_taxonomy_term_view_alter
  // and hook_entity_view_alter
  $term_renderarray = taxonomy_term_view($term, $view_mode, $langcode);

  // Add term heading if the term has a description
  if (!empty($term->description))
  {
    $build['term_heading'] = array(
        '#prefix' => '<div class="term-listing-heading">',
        '#suffix' => '</div>',
        'term' => $term_renderarray,
        '#weight' => 1
    );
  }

  // add stuff to $build['subterms'] and $build['nodes']

  $build['pager_top'] = array(
      '#theme' => 'pager',
      '#weight' => -5
  );

  $build['pager_bottom'] = array(
      '#theme' => 'pager',
      '#weight' => 5
  );

  // Keep modules that expect $entity->content to exist happy
  // otherwise this causes errors with implementations such as
  // field_conditional_state_entity_view()
  $term->content = $build;

  // Allow modules to make their own additions to the term.
  module_invoke_all('taxonomy_term_view', $term, $view_mode, $langcode);
  module_invoke_all('entity_view', $term, 'taxonomy_term', $view_mode, $langcode);

  return $build;
}

whew :)

  • This still is an issue un Drupal 7.43 right? Because metatag module prints global default metatags when viewing a taxonomy page and looking around the code it looks like metatag_entity_view from metatag.module does not get triggered when viewing a taxonomy page and in there is what it sets metatags.... – Gianni Di Falco Jun 22 '16 at 11:11
  • 1
    I think so. It's fixed for Drupal 8, but the issue is still tagged with "Needs backport to D7". – rudolfbyker Jun 22 '16 at 11:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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