sisko
sisko

Reputation: 9910

Drupal 7 hook_node_view remove Views generated content

I have a need to programmatically remove an image field on a node display.

To achieve this my module implements hook_node_view and I can unset data in $node->content which prevents it from displaying on the node page. However, because Views generates the image, it is not represented in $node->content.

Can anyone suggest a solution that will allow me to remove Views generated content from a module?

Upvotes: 0

Views: 360

Answers (2)

sisko
sisko

Reputation: 9910

With thanks to @justinelejeune for her efforts, I found a simpler solution.

Implementing hook_node_view, you can just unset the target field or set the field array to blank I.E:

function mymodule_node_view($node, $view_mode, $langcode){
    if( $view_mode != 'full')
        return;

  unset($node->field_banner); // or     $node->field_banner = array();

}

Upvotes: 0

Djouuuuh
Djouuuuh

Reputation: 1167

If you are using Views to generate your node, I'll suggest you to unset your field in template_preprocess_views_view(&$vars) where you can access it via $vars['view']->result which is an array with all the rows displayed in your view. Then you can search for your field in each row and unset it.

Example for a image field structure:

function template_preprocess_views_view(&$vars) {
  foreach($vars['view']->result as $test){
    $test->field_field_image[0]['rendered']['#access'] = FALSE;
  }
}

I hope it helped.

Upvotes: 1

Related Questions