Hi all,
I have inherited a Drupal 7 website that was built by someone else. I have gotten my feet wet with Drupal over the last few months and like it overall, but there are still some things I am not sure about - one of which is writing code in PHP.
We have a content type 'employee' that shows their picture and bio information, etc. We have another content type 'articles' that are written by the employees, so when a new article is written we can link the article to the employee as a node reference. When an employee leaves, their bio page is just unpublished rather than deleted from the site. It was brought to my attention an error was occurring on my site when users would search for certain content that involved employees that are no longer here. Rather than get "no results found" in their search, they would get the rather unhelpful (and dreaded) "an unexpected error has occurred on this site" message instead. When I logged into the view the recent message logs, I was greeted with the following error:
EntityMalformedException: Missing bundle property on entity of type node. in entity_extract_ids() (line 7663 of /path/to/includes/common.inc).
What struck me first is that this was listed as a cron error, and not a PHP error. I researched this error for weeks, coming across several threads with suggestions to try. This thread has a good summary of issues and suggestions, but none of them have worked for me.
A little more investigation revealed that just before the above cron error in the message logs, I would also see this error each time:
Notice: Trying to get property of non-object in get_employee_right_formatter_markup() (line 124 of /path/to/sites/all/modules/custom/custom.module).
The site contains a custom module, built by the party who made the site (who is no longer available for help). I went into the module code and tracked down the above function it was complaining about. Here is the full code for that:
function get_employee_right_formatter_markup($item){
//echo "<pre>";
//print_r($item);
$type = $item['node']->type;
ob_start();
$uri = entity_uri('node', $item['node']);
?>
<div class="employee_container">
<div class="left_image_container">
<?php if($type == 'employee'){ ?>
<a href="/<?php echo drupal_lookup_path('alias',$uri['path'])?>"><img src="<?php echo image_style_url('thumb_70_by_85',$item['node']->field_user_image['und'][0]['uri'])?>" /></a>
<?php } else { ?>
<img src="<?php echo image_style_url('thumb_70_by_85',$item['node']->field_user_image['und'][0]['uri'])?>" />
<?php } ?>
</div>
<div class="right_content_container">
<div class="employee_name">
<?php if($type == 'employee'){ ?>
<a href="/<?php echo drupal_lookup_path('alias',$uri['path'])?>"><?php echo $item['node']->title?></a>
<?php } else { ?>
<?php echo $item['node']->title?>
<?php } ?>
<?php if(!empty($item['node']->field_position['und'][0]['value'])){ ?>
<?php if(!empty($item['node']->field_employee_main_title['und'][0]['value'])){ ?>
<br />
<em><?php echo $item['node']->field_employee_main_title['und'][0]['value']; ?></em>
<?php } else { ?>
<br />
<em><?php echo $item['node']->field_position['und'][0]['value']; ?></em>
<?php } ?>
<?php } ?>
</div>
<?php echo $item['node']->field_user_phone['und'][0]['value']?>
<br />
<a href="mailto:<?php echo $item['node']->field_user_email['und'][0]['safe_value']?>"><?php echo $item['node']->field_user_email['und'][0]['safe_value']?></a>
</div>
<div class="clearer"></div>
</div>
<?php
return ob_get_clean();
}The offending line in the above code is $type = $item['node']->type;.
Again, this only happens when you search for employees who are no longer here and have had their bio unpublished. Any articles they had written are still published, so obviously that's where the problem comes in. If I disable this custom module, the errors go away (but then we lose the functionality of this module, obviously).
Can anyone help me out or point me in the right direction? I would assume that if we can fix the problem with the custom module, it will make the original error go away too. I was wondering if there was some way to add a simple if/else statement somewhere - checking to see if the employee node status is published or not - would work? I am not the greatest programmer. Any help is appreciated, and I can offer more information/code if you need it.
Thank you!
Comments
Just before $type =
Just before
I would add
which checks to make sure $item['node'] is actually set and if it is set, the nid is non-zero.
This appears to be working
This appears to be working perfectly so far!!! Thank you!!!!