Skip to main content
Drupalista.sk

Main navigation

  • Home
User account menu
  • Kontakt

Breadcrumb

  1. Home

Quick idea: How to filter view result by content entity ID?

By drupalista, 11 March, 2024

Imagine this scenario: We have a view of certain content type nodes. This content type has field of type taxonomy term reference and we want to filter the view by taxonomy term ID. It means, we only want to display content assigned to particular taxonomy term. Easy, right? Obviously, you'd create this view on your local or development environment. Then you push the configuration on production and what??? The filter does not work! Why? Because the ID of the taxonomy term may be defferent on production website. It's a content entity so it would not be a part of configuration.

The fix is very easy and if you started to think of it by the end of previous paragraph, you probably have the solution now. However, in very unlikely case you have not, this is what I did:

  1. Create a new field on appropriate vocabulary. Name it ID (field_id). In the description put that this is a text ID and it's same on all environments and should not be changed otherwise the view will stop working. Think of this as a machine name for taxonomy term.
  2. Add the value in the field for every taxonomy term you want to filter views by. For example, if your vocabulary is Fruit and your term is Orange, put orange.
  3. Edit your view. Add relation to appropriate taxonomy and filter by field_id.
  4. Have a coffee and enjoy the moment of genius and the power of Drupal structured content!

Tags

  • views
  • taxonomy
  • field
  • filter
  • Log in or register to post comments

Comments

Language switcher

  • English
  • Slovak

Recent content

  • Quick idea: How to filter view result by content entity ID?
    2 years ago
  • How to add a new token for existing module?
    2 years ago
  • Drupal Migrate API: How to migrate images
    2 years 5 months ago
RSS feed
Powered by Drupal