By Stephen Ash


2019-05-14 21:29:12 8 Comments

If I'm on a node, inside preprocess_page(&$variables), I can check

if (isset($variables['node']) && $variables['node']->bundle() == 'my-type')

to see if I'm on a node, and a node of a certain type.

Then I can get field information with

$variables['node']->field_icon->entity->getFileUri()

How can I do the same thing for a taxonomy term page to know that I'm on a taxonomy term page and get the field values?

3 comments

@berramou 2019-05-15 00:48:53

There is two ways to get the term in preprocess_page.

  1. Get the tid from parameter and then load the term.
  2. Get the Term directly from ParameterBag.
    Her is code snippet

use Drupal\taxonomy\Entity\Term;
/**
 * Implements hook_preprocess_HOOK().
 */
function myModule_preprocess_page(&$variables) {
  // Get the tid.
  $tid = \Drupal::routeMatch()->getRawParameter('taxonomy_term');
  // Load the term.
  $term = Term::load($tid);

   // Get possible params.
   /** @var \Symfony\Component\HttpFoundation\ParameterBag $possible_parameters */
   $possible_parameters = \Drupal::routeMatch()->getParameters();
    // Get term
    $term = $possible_parameters->get('taxonomy_term');
}

@100pic 2019-05-14 23:24:36

Unfortunately I don't think there is anything that the hook gives you directly that you can use in this instance. However you can always check the route name to determine if you are on a taxonomy term page.

// Will be "true" if you are currently on a taxonomy term page.
if (\Drupal::routeMatch()->getRouteName() == 'entity.taxonomy_term.canonical') {

  // Since this is a taxonomy term page, you may wish to get the term ID.
  $term_id = \Drupal::routeMatch()->getRawParameter('taxonomy_term');
}

@kirtivaland 2019-05-14 23:23:25

To check term data in theme_preprocess_page for drupal 7. you can use below code.

if (arg(0) == 'taxonomy' && arg(1) == 'term') {
if (isset($variables['page']['content']['system_main']['term_heading']['term']['#term'])) {
  $term_data = $variables['page']['content']['system_main']['term_heading']['term']['#term'];
}

}

Hope this helps.

Related Questions

Sponsored Content

1 Answered Questions

Taxonomy-term based block display

0 Answered Questions

How to grab taxonomy term with node_presave hook

2 Answered Questions

[SOLVED] Get taxonomy terms added to nodes

2 Answered Questions

[SOLVED] Taxonomy view page for double nested terms

2 Answered Questions

[SOLVED] How to get add term form of taxonomy inside node type form?

1 Answered Questions

How can I tell if a node has more than one parent taxonomy term assigned to it

  • 2016-10-26 04:24:33
  • jdh1285
  • 45 View
  • 0 Score
  • 1 Answer
  • Tags:   taxonomy-terms

2 Answered Questions

[SOLVED] duplicate all instances of a term

  • 2015-10-22 22:50:10
  • Chimekin
  • 32 View
  • 1 Score
  • 2 Answer
  • Tags:   taxonomy-terms

2 Answered Questions

[SOLVED] Add taxonomy field to a bundle programmatically

  • 2015-09-11 08:39:52
  • Alireza Tabatabaeian
  • 2374 View
  • 2 Score
  • 2 Answer
  • Tags:   7 taxonomy-terms

1 Answered Questions

[SOLVED] Change layout of views page based on taxonomy term field

1 Answered Questions

[SOLVED] How to know, if on taxonomy page?

  • 2012-12-13 11:19:58
  • yunzen
  • 1898 View
  • 1 Score
  • 1 Answer
  • Tags:   7 taxonomy-terms

Sponsored Content