Problem/Motivation

In at least one instance, after the node view is built via node_build_content, the some of the standard alter hooks are not invoked as might be expected.

Line 204-206 in quiz_question.module, specifically.

    node_build_content($node, 'question');
    unset($node->content['answers']);
    unset($node->content['links']);

In the Node module, the following snippet happens in node_view() after calling node_build_content().

  // Allow modules to modify the structured node.
  $type = 'node';
  drupal_alter(array('node_view', 'entity_view'), $build, $type);<

Proposed resolution

Consider calling node_view() instead of node_build_content(). This will invoke hook_node_view() and hook_node_view_alter() (and the entity counterparts) without having to explicitly do it in this module.

If there are reasons that calling node_view() was avoided, just make sure that this and other instances of view building are followed by

  // Allow modules to modify the structured node.
  $type = 'node';
  drupal_alter(array('node_view', 'entity_view'), $build, $type);<

Comments

djdevin’s picture

There were issues using node_view() since it renders the title and other things as well so we used node_build_content().

I agree I think manually invoking that hook would be the way to go.

wwedding’s picture

Status: Active » Needs review
StatusFileSize
new703 bytes

Yeah, you end up having to add at least 4 additional lines just to unset all the extra stuff that this module doesn't care about. It looks nicer just invoking the alter hooks.

djdevin’s picture

Status: Needs review » Fixed

Looks good. Thanks!

  • djdevin committed a5accac on 7.x-5.x authored by stickywes
    Issue #2541788 by stickywes: Entity/Node_view_alter hooks not invoked...
wwedding’s picture

Status: Fixed » Needs review
StatusFileSize
new619 bytes

Sorry, I found another case that I missed. I should've done a thorough search in all the files before making that patch. Nothing else I've missed at this point, though.

Status: Needs review » Needs work

The last submitted patch, 5: quiz-2541788-invoke_entity_view_alter-5.patch, failed testing.

  • djdevin committed e878ee9 on 7.x-5.x authored by stickywes
    Issue #2541788 by stickywes: Entity/Node_view_alter hooks not invoked...
djdevin’s picture

Status: Needs work » Fixed

Fixed some more!

Tests are fine, the random one fails from time to time (because it is random).

djdevin’s picture

Status: Fixed » Needs review
StatusFileSize
new1.24 KB

Whoops, got a fatal on this one.

hook_node_view_alter
and
hook_entity_view_alter

Both take $build as input, not the node. They should receive the render array generated by node_view or entity_view().

djdevin’s picture

Status: Needs review » Fixed

  • djdevin committed a565231 on 7.x-5.x
    Issue #2541788 by stickywes, djdevin: Entity/Node_view_alter hooks not...

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.