Hi all

I had a similar problem a few days back, though this time with the code below using hook_entity_view_alter. Everything works when using node_view_alter for other pages.

Keeps showing the error:

Notice: Undefined index: #entity in theme1_entity_view_alter() (line 92 of htdocs/example.dev/themes/theme1/template.php).
Notice: Trying to get property of non-object in theme1_entity_view_alter() (line 93 of htdocs/example.dev/themes/theme1/template.php).

/**
 * 
 * hook_entity_view_alter for customise breadcrumbs
 */
function theme1_entity_view_alter(&$build)
{
    $entity = $build['#entity'];
    if ($build['#view_mode'] == 'full' && $entity->type == 'event')
    {
      $breadcrumb = array();
      $breadcrumb[] = l('Home', '<front>');
      $breadcrumb[] = l('Whats on', 'whats-on');
      drupal_set_breadcrumb($breadcrumb);
    }
}

Can anybody help?

I checked the api page, though not sure how to integrate their code, or if I need this:

function hook_entity_view_alter(&$build, $type) {
  if ($build['#view_mode'] == 'full' && isset($build['an_additional_field'])) {
    // Change its weight.
    $build['an_additional_field']['#weight'] = -10;

    // Add a #post_render callback to act on the rendered HTML of the entity.
    $build['#post_render'][] = 'my_module_node_post_render';
  }
}

Thanks, Barry

Comments

Jaypan’s picture

Looks like $build['#entity'] doesn't exist. Dump $build and see where the entity resides. I'm guessing the ECK module doesn't store the entity in $build['#entity'].

computerbarry’s picture

I've just added:

print '<pre>';
      var_dump($build); 
print '</pre>';

Spits out a massive amount of data.
What am I looking for?

Here is a small snippet of what it shows:

array(13) {
  ["#view_mode"]=>
  string(4) "full"
  ["field_event_date"]=>
  array(16) {
    ["#theme"]=>
    string(5) "field"
...
......
["#entity_type"]=>
    string(6) "events"
    ["#bundle"]=>
    string(5) "event"
    ["#object"]=>
object(Entity)#6 (16) {
      ["entityType":protected]=>
      string(6) "events"
      ["entityInfo":protected]=>
      array(22) {
        ["label"]=>
        string(6) "Events"
        ["base table"]=>
        string(10) "eck_events"
        ["entity class"]=>
        string(6) "Entity"
        ["controller class"]=>
        string(19) "EntityAPIController"
        ["form callback"]=>
        string(17) "eck__entity__form"
        ["access callback"]=>
        string(18) "eck__entity_access"
        ["module"]=>
        string(3) "eck"
        ["fieldable"]=>
        bool(true)
        ["entity keys"]=>
        array(4) {
          ["id"]=>
          string(2) "id"

Is this useful?

update

The pages that run nodes (node_view_alter) are now the only page showing the errors, though still the entity error, maybe something to do with using $build in both.

This is what I have above the entity:

function theme1_node_view_alter(&$build)
{
    $node = $build['#node'];
    // Add extra breadcrumb for content type
    if($build['#view_mode'] == "full" && $node->type == "about")
    {...

Barry

The more you learn.... the more you learn there is more to learn.

Jaypan’s picture

It looks like $build is the entity. It's not contained in an element of the array, it is the array.

Note that as nodes are entities, hook_entity_view() will also be called on nodes.

computerbarry’s picture

Note that as nodes are entities, hook_entity_view() will also be called on nodes.

That makes a bit more sense, though I thought by using:

if ($build['#view_mode'] == 'full' && $entity->type == 'event')

...this would only then target the entity type of event, and not interfere with the content type?

So how do we fix it?

The more you learn.... the more you learn there is more to learn.

Jaypan’s picture

That makes a bit more sense, though I thought by using:

if ($build['#view_mode'] == 'full' && $entity->type == 'event')

...this would only then target the entity type of event

As the error is telling you, $build['#entity'] does not exist, so this code will not result in $entity being populated:

$entity = $build['#entity'];

And therefore $entity->type will not exist.

You'll need to test for the type of entity using the data you've got access to. You've already seen that data when doing the dump that you posted earlier. Dig through that to find where the type is being set, and use that for your check.

computerbarry’s picture

Cool, thanks for information I'll give this a try and do further testing.

As the error is telling you, $build['#entity'] does not exist, so this code will not result in $entity being populated:

I keep forgetting that the hash elements '#entity' = the variable '$entity' in this instance, if I have that correct?

And therefore $entity->type will not exist.

The funny thing is, both snippets of code I use for entity_type and content_type to update the breadcrumbs, they work.
Extra breadcrumbs are added to the correct pages, its just on the content_type page node that it shows the entity error.

Should have enough information for testing, I'll get back if I can't fix it, thanks again.

Barry

The more you learn.... the more you learn there is more to learn.

computerbarry’s picture

You'll need to test for the type of entity using the data you've got access to. You've already seen that data when doing the dump that you posted earlier. Dig through that to find where the type is being set, and use that for your check.

I've checked and tried numerous combinations, can't get it to work.

Can anybody advise what I need to replace #entity with so this will work?

Thanks,
Barry

The more you learn.... the more you learn there is more to learn.

computerbarry’s picture

I'm back at this same problem, I've never manage to fix this :(

And therefore $entity->type will not exist.

You'll need to test for the type of entity using the data you've got access to. You've already seen that data when doing the dump that you posted earlier. Dig through that to find where the type is being set, and use that for your check.

What am I looking for, and how do I check?

Can anybody advise?

Thank you!

The more you learn.... the more you learn there is more to learn.