entity_load's second parameter is an array. But if I want to load only one entity, I often forget to pass an array. I saw this practice before in core, so I think that it can be a nice DX improvement.

Comments

tstoeckler’s picture

Priority: Critical » Normal

Definitely not critical.

damien tournoud’s picture

Na. -1 for inconsistent APIs (I know we have some others like that lying around). The return value is always an array, so supporting either a scalar or an array as the input argument make it even more inconsistent.

By the way, this should have type-hinting, I'm not sure why this one hasn't.

tamasd’s picture

StatusFileSize
new536 bytes

You are right, type hinting is a better solution.

berdir’s picture

Status: Active » Needs review

This actually already works because we have an empty($ids) check.

Adding the type hint breaks the tests, see also #839986: Enforce array arguments (Say good bye to Fatal error: Cannot use object of type stdClass as array errors), which I actually removed the type hint for entity_load() because it's technically an api change.

Edit: simply changing to needs review to confirm that this fails.

Status: Needs review » Needs work

The last submitted patch, entity_load_2.patch, failed testing.

NealB-1’s picture

Version: 7.x-dev » 8.x-dev
Component: base system » entity system
Priority: Normal » Major
Status: Needs work » Needs review
StatusFileSize
new593 bytes

This version accepts a single id and should not cause any downstream ill effects.

I marked this as major, because the sooner the change is made, the greater the good it can do in terms of simplifying all the other code that invokes this function.

xjm’s picture

Priority: Major » Normal
Status: Needs review » Needs work
+++ b/core/modules/entity/entity.moduleundefined
@@ -232,6 +232,10 @@ function entity_load($entity_type, $ids = FALSE, $conditions = array(), $reset =
+  if(is_numeric($ids)) {

There needs to be a space between if and the parentheses. Reference: http://drupal.org/coding-standards#controlstruct

Additionally, if we're changing the allowed values of a parameter, we need to update the function documentation to indicate that.

Finally, this is not major. I'm actually not sure this change should be made at all.

berdir’s picture

This is the wrong approach anyway. entity_load() needs to be renamed to entity_load_multiple(), to be consistent with all the other load functions. Sure, this will break everything, but we're allowed to do that in this stage.

xjm’s picture

tstoeckler’s picture

Status: Needs work » Closed (duplicate)

Yes, either this is won't fix for #2 or duplicate for #9. I'm going for duplicate.

NealB-1’s picture

Thanks for the pointer to http://drupal.org/node/1160566