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:

  return $node ? reset($node) : FALSE;

Patch to follow.

#13 fieldable_panels_panes-n1985894-13.patch15.45 KBDamienMcKenna
PASSED: [[SimpleTest]]: [MySQL] 0 pass(es). View
#11 fieldable_panels_panes-n1985894-11.patch15.42 KBDamienMcKenna
PASSED: [[SimpleTest]]: [MySQL] 0 pass(es). View
#9 fieldable_panels_panes-n1985894-9.patch15.41 KBDamienMcKenna
PASSED: [[SimpleTest]]: [MySQL] 0 pass(es). View
#5 return_false_if_no_entity-1985894-5.patch498 bytestanc
#1 return_false_if_no_entity-1985894-1.patch518 bytestanc
PASSED: [[SimpleTest]]: [MySQL] 0 pass(es). View
Members fund testing for the Drupal project. Drupal Association Learn more


tanc’s picture

Status: Active » Needs review
518 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

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/      foreach ($values as $row_index => $value) {
plugins/views/        if (!empty($value->{$this->aliases['fpid']}) && !empty($value->{$this->aliases['vid']})) {
plugins/views/          $this->entities[$row_index] = fieldable_panels_panes_load($value->{$this->aliases['fpid']}, $value->{$this->aliases['vid']});
plugins/views/          if ($this->entities[$row_index]->vid == $this->entities[$row_index]->current_vid) {
plugins/views/      foreach ($values as $row_index => $value) {
plugins/views/        if (!empty($value->{$this->field_alias}) && !empty($value->{$this->aliases['vid']})) {
plugins/views/          $this->entities[$row_index] = fieldable_panels_panes_load($value->{$this->field_alias}, $value->{$this->aliases['vid']});
plugins/views/        }
plugins/views/      }
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:

  return $node ? reset($node) : FALSE;

On the other hand, taxonomy_term_load() does this:

  return $term ? $term[$tid] : FALSE;

.. and taxonomy_vocabulary_load() does this:

  return reset($vocabularies);

.. and user_load() does this:

  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
15.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

15.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

15.45 KB
PASSED: [[SimpleTest]]: [MySQL] 0 pass(es). View


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


Status: Fixed » Closed (fixed)

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