0

I created a view of terms using views_tree module. I need to remove some of the terms under some conditions. This is my code

function THEME_views_pre_render(&$views) {
    for($i = 0 ; $i < sizeof($views->result) ; $i++)
       unset($views->result[$i]);
}

But it still prints all of the terms and doesn't delete any of them. I tried this code

function THEME_views_pre_render(&$views) {
    for($i = 0 ; $i < sizeof($views->result) ; $i++){
        $views->result[$i]->taxonomy_term_data_name ="Hello";
    }
}

It successfully renamed every term, But when I tried the following code, every name turned back to what it was from the beginning and nothing removed.

function THEME_views_pre_render(&$views) {
    for($i = 0 ; $i < sizeof($views->result) ; $i++){
        $views->result[$i]->taxonomy_term_data_name ="Hello";
        unset($views->result[$i]);
    }
}

How can I remove some terms ?

3 Answers 3

1

It should work on template.php too. Module implementation does not look mandatory here. I have noticed a few issues in the code sample that you have pasted.

  1. There is a typo. It should be $view not $views.
  2. Instead of

    for($i = 0 ; $i < sizeof($view->result) ; $i++){
    }
    

    Do

    $sizeof_result = sizeof($view->result);
    for($i = 0 ; $i < $sizeof_result ; $i++)
    

The following code works for me:

function MYTHEME_views_pre_render(&$view){
  if($view->name == 'MY_VIEW'){
    $sizeof_result = sizeof($view->result);
    for($i = 0 ; $i < $sizeof_result ; $i++){
      //IF Condition
        unset($view->result[$i]);
    } 
  }
}

The reason for using variable $sizeof_result instead of sizeof($view->result) in for loop is:

For each execution of the loop, the value of sizeof($view->result) changes(decreases by 1). Due to this, the for loops does not work as expected.

1
  • Yes it worked. Thanks. It is very surprising that the name view is important
    – M a m a D
    Jun 10, 2018 at 12:14
0

Use basically the same code in THEME_views_post_execute(&$view) https://api.drupal.org/api/views/views.api.php/function/hook_views_post_execute/7.x-3.x

4
  • I tried it, drupal doesn't run that function at all. I cleared the caches too
    – M a m a D
    Jun 9, 2018 at 13:56
  • I'd recommend putting it in a custom module and not the theme. Jun 9, 2018 at 14:03
  • I swear I've unset rows before in the hook_view_pre_render() before though too..But I always use custom modules. Maybe somebody will come along with a theme based answer, if it has to be there. Jun 9, 2018 at 14:11
  • Ok, I will try it in module level implementation
    – M a m a D
    Jun 9, 2018 at 18:31
0

As jackrabbithanna said, maybe implementation of the code in a module may resolve your problem, I also had excluded some view results in a project using an auxiliary result set as code below , hope that helps:

/**
  * Implementation of hook_views_pre_render().
  */
function MYMODULE_views_pre_render(&$view) {
  $new_result = [];
  foreach ($view->result as $key => $value) {
   if(SHOULD NOT EXCLUDED)
    $new_result[] = $value;
  }
  $view->result = $new_result;
}

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.