There may be reasons why you don't do this but usually an entity load function returns FALSE if no entity is returned by entity_load(). For example node_load() does:

<?php
 
return $node ? reset($node) : FALSE;
?>

Patch to follow.

Comments

tanc’s picture

Status:Active» Needs review
StatusFileSize
new518 bytes
PASSED: [[SimpleTest]]: [MySQL] 0 pass(es).
[ View ]
Dave Reid’s picture

Category:bug» task
Status:Needs review» Needs work
+++ w/fieldable_panels_panes.moduleundefined
@@ -679,9 +679,8 @@ function fieldable_panels_panes_load($fpid, $vid = NULL) {
+  return $entities ? reset($entities) : FALSE;

I'm ok with this cleanup, but this actually only needs to be return reset($entities). The docs for reset() show that it will return FALSE if the array is empty.

merlinofchaos’s picture

Though the construct is safer if, for some reason, the prior load function returns false or null or an empty array.

Dave Reid’s picture

I don't think returning anything but an array is even possible from entity_load(). It's either an array or an exception.

tanc’s picture

StatusFileSize
new498 bytes

Here's a patch with the method Dave mentioned.

Dave Reid’s picture

It looks like we may need to test some of the Views handlers to make sure they work ok if the function now returns FALSE:

git grep -C 2 'fieldable_panels_panes_load(' .

plugins/views/fieldable_panels_panes_handler_field_is_current.inc-      foreach ($values as $row_index => $value) {
plugins/views/fieldable_panels_panes_handler_field_is_current.inc-        if (!empty($value->{$this->aliases['fpid']}) && !empty($value->{$this->aliases['vid']})) {
plugins/views/fieldable_panels_panes_handler_field_is_current.inc:          $this->entities[$row_index] = fieldable_panels_panes_load($value->{$this->aliases['fpid']}, $value->{$this->aliases['vid']});
plugins/views/fieldable_panels_panes_handler_field_is_current.inc-
plugins/views/fieldable_panels_panes_handler_field_is_current.inc-          if ($this->entities[$row_index]->vid == $this->entities[$row_index]->current_vid) {
--
plugins/views/fieldable_panels_panes_handler_field_view_revision.inc-      foreach ($values as $row_index => $value) {
plugins/views/fieldable_panels_panes_handler_field_view_revision.inc-        if (!empty($value->{$this->field_alias}) && !empty($value->{$this->aliases['vid']})) {
plugins/views/fieldable_panels_panes_handler_field_view_revision.inc:          $this->entities[$row_index] = fieldable_panels_panes_load($value->{$this->field_alias}, $value->{$this->aliases['vid']});
plugins/views/fieldable_panels_panes_handler_field_view_revision.inc-        }
plugins/views/fieldable_panels_panes_handler_field_view_revision.inc-      }

DamienMcKenna’s picture

Category:Task» Feature request
Issue summary:View changes

Per the documentation for entity_load():

An array of entity objects indexed by their ids. When no results are found, an empty array is returned.

That said, per the issue summary, node_load() does do it slightly differently:

<?php
 
return $node ? reset($node) : FALSE;
?>

On the other hand, taxonomy_term_load() does this:

<?php
 
return $term ? $term[$tid] : FALSE;
?>

.. and taxonomy_vocabulary_load() does this:

<?php
 
return reset($vocabularies);
?>

.. and user_load() does this:

<?php
 
return reset($users);
?>

So, the question is, is there a particular reason taxonomy_vocabulary_load() and user_load() do it one way while node_load() and taxonomy_term_load() will return FALSE if nothing is found?

DamienMcKenna’s picture

Lets fix this for the next release.

DamienMcKenna’s picture

Status:Needs work» Needs review
StatusFileSize
new15.41 KB
PASSED: [[SimpleTest]]: [MySQL] 0 pass(es).
[ View ]

This builds upon @tanc's patch. I went through and ensured all occasions where foreach() loops were used or the results of the entity load functions were used, were properly checked to not be empty() first.

mglaman’s picture

Curious why we need to check empty() if it's returning return $entities ? reset($entities) : FALSE;. Couldn't we just keep !$entities or at least $entities === false?

+++ b/fieldable_panels_panes.module
@@ -719,7 +719,8 @@ function template_preprocess_fieldable_panels_pane(&$vars) {
+    $entity = fieldable_panels_panes_load_entity($vars['pane']->subtype);
+    if (!empty($entity)) {

Maybe it's just bad PHP habits, but the FALSE check and variable definition could be within the if(), that's how Commerce does it in a few places.

Just my two bits reviewing through Dreditor and not applying patch yet.

DamienMcKenna’s picture

StatusFileSize
new15.42 KB
PASSED: [[SimpleTest]]: [MySQL] 0 pass(es).
[ View ]

I prefer being verbose about it. A slight update.

jibran’s picture

+++ b/fieldable_panels_panes.module
@@ -762,9 +763,7 @@ function fieldable_panels_panes_load($fpid, $vid = NULL) {
+  return !empty($entities) ? reset($entities) : FALSE;

+1 to this.

DamienMcKenna’s picture

StatusFileSize
new15.45 KB
PASSED: [[SimpleTest]]: [MySQL] 0 pass(es).
[ View ]

Rerolled.

DamienMcKenna’s picture

I double-checked the Commerce module and all of the bundled entities follow the same pattern, so I think this will be fine as-is.

  • DamienMcKenna committed 704f321 on 7.x-1.x
    Issue #1985894 by DamienMcKenna, tanc: Return FALSE in load function if...
DamienMcKenna’s picture

Status:Needs review» Fixed

Committed.

Status:Fixed» Closed (fixed)

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