Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
By computerbarry on
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
Looks like $build['#entity']
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'].
I've just added:
I've just added:
Spits out a massive amount of data.
What am I looking for?
Here is a small snippet of what it shows:
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:
Barry
The more you learn.... the more you learn there is more to learn.
It looks like $build is the
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.
Note that as nodes are
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.
That makes a bit more sense,
As the error is telling you, $build['#entity'] does not exist, so this code will not result in $entity being populated:
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.
Cool, thanks for information
Cool, thanks for information I'll give this a try and do further testing.
I keep forgetting that the hash elements '#entity' = the variable '$entity' in this instance, if I have that correct?
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.
You'll need to test for the
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.
I'm back at this same problem
I'm back at this same problem, I've never manage to fix this :(
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.