Search code examples
drupal-7fieldalterunset

Drupal 7 alter node display


I have a content type with two image fields, banner and logo.

I am trying to implement logic which will allow one of the two to display depending on whether an editor elected to display just the banner or just the logo from radio button options.

I setup a small custom module implementing hook_node_view and tried to unset the image field from the node object but no joy. Code fragment below:

function mymodule_node_view($node, $view_mode, $langcode){
    unset($node->field_main_picture[$node->language][0]);
    unset($node->field_main_picture);
    $node->field_main_picture = null;
}

None of those attempts worked.


Solution

  • I found the answer to my question.

    The node object contains an array called content which is the renderable data Drupal will print to screen. It's in that array that my unsets need to take place. I.E:

            unset($node->content['field_main_picture']);
    

    And the main picture image disappears.