By plocks

2019-01-11 13:13:14 8 Comments

I have a view where I want to output a list of content node teasers. At the moment all content nodes are used, but I want to filter the output to only contain nodes that have a certain value in a taxonomy field property they have, check if the current user has a certain role that is similar to the property and only then show them in the output.

So far I've tried to programmatically filter the output in the hook mytheme_preprocess_views_view_unformatted of my theme with the following code

function mytheme_preprocess_views_view_unformatted(&$variables) {
    $result = $variables['view']->result;
    $entity = $result->entity;

but that crashes the site and doesn't give me the results to filter.

Is there a way to filter the content nodes programmatically or view the view settings and if so how?


@Tyler Fahey 2019-01-11 14:34:54

I think I understand what you're describing, and it is theoretically possible to handle in the hook as you've started, with something like this:

 * Implements hook_preprocess_views_view_unformatted().
function mymodule_preprocess_views_view_unformatted(&$variables) {
  if (isset($variables['view'])) {
    $view = $variables['view'];
    // You'll want to check the ID && possibly the current_display so as not to fire on every View!
    // In this example, my View is called "test", and I have a page display called "page_1".
    if ($view->id() == "test" && $view->current_display == "page_1") {
      $result = $view->result;
      // You mentioned doing something regarding the user, so you could grab that here.
      $current_user = $variables['user']->account;
      // The $result will be an array of ResultRow entities.
      foreach ($result as $key => $resultRowEntity) {
        // Do some customized logic on each row.
        // You can access the entire entity here if needed like this:
        $row_entity = $resultRowEntity->_entity;
        // In my case this is a Node, so can do something to get a term field from it.
        // In this example, getting the "some_field" and the first delta of it as a string value.
        $some_field_on_the_node = $row_entity->get('field_some_field')->getValue();
        // You could do some sort of logic here to determine whether or not to unset this row.
        // In this example, I'll say if the field doesn't equal 2, OR the user ID is not equal to 1, unset the row.
        // This would remove it from the result.
        if ($some_field_on_the_node != "2" || $current_user->uid != 1) {
          // You'll want to unset it from 'rows' to not display it.

I think the takeaway is to use "isset" to avoid those nasty PHP errors, and I highly recommend using XDebug, or at the least the devel && kint modules, so that in your code, you can put things like ksm($variables['view']) to get some insight into what variables you have available to use. PHP can be very finicky when you try and reference things that don't exist!

Even though this might technically work, it's worth noting that a better approach would be to implement a relationship in Views to bring in the taxonomy term entity being referenced on your node, and that would allow you to add View filters in the UI to massage out the data that way. You could also add a role that could be associated with viewing your particular content type, and this would automatically exclude it from Views results regarding the user if they didn't have this role.

Of course, I may me misunderstanding the complexity of your use case. Another "better practice" approach would be to implement a custom Views filter, which could then be added via the Views UI and maintained separately in a custom module, and would possibly allow for a cleaner implementation - It's somewhat of a broad topic, but I think this is a great intro to the topic:

I hope this helps point you in the right direction!

@plocks 2019-01-11 14:38:39

Wow, thanks for the in depth answer, I'll look into it right away!

@plocks 2019-01-11 15:23:55

With a few changes here and there the programmatical solutions works like a charm! Thanks again.

Related Questions

Sponsored Content

1 Answered Questions

Filter by field user role permission

  • 2016-05-18 06:39:50
  • Praveen Srinivasan
  • 714 View
  • 0 Score
  • 1 Answer
  • Tags:   views users

0 Answered Questions

View presents fewer items per page after using Unset

  • 2019-04-26 22:43:17
  • yorchperaza
  • 20 View
  • 0 Score
  • 0 Answer
  • Tags:   views 8 hooks

2 Answered Questions

Views Dropdown List of valid items

  • 2013-03-03 21:01:55
  • TheMad Hatter
  • 1517 View
  • 2 Score
  • 2 Answer
  • Tags:   views

1 Answered Questions

How to override output (query) for views exposed filter?

  • 2018-09-25 10:01:17
  • Erbie
  • 174 View
  • 0 Score
  • 1 Answer
  • Tags:   views 8

1 Answered Questions

[SOLVED] Filter referenced item without losing other content

  • 2018-04-09 18:44:06
  • GoempieK
  • 38 View
  • 0 Score
  • 1 Answer
  • Tags:   7 entities views

1 Answered Questions

[SOLVED] Filter search results based on user field

  • 2017-06-09 19:28:06
  • lokisapocalypse
  • 164 View
  • 0 Score
  • 1 Answer
  • Tags:   views

3 Answered Questions

one view show nodes and their referenced nodes - both only published

  • 2014-08-01 18:10:13
  • loparr
  • 37 View
  • 0 Score
  • 3 Answer
  • Tags:   7 views

2 Answered Questions

[SOLVED] Drupal 7 Views, List, Filters and User

  • 2012-01-18 04:26:04
  • Sithu
  • 1927 View
  • 0 Score
  • 2 Answer
  • Tags:   views

2 Answered Questions

[SOLVED] View with entityreference: make exposed filter autocomplete

  • 2012-08-01 11:52:40
  • nicolasleroy
  • 2891 View
  • 1 Score
  • 2 Answer
  • Tags:   views entities ajax

Sponsored Content