In RDF module we have the following function

/**
 * Implements hook_entity_load().
 */
function rdf_entity_load($entities, $type) {
  foreach ($entities as $entity) {
    // Extracts the bundle of the entity being loaded.
    list($id, $vid, $bundle) = entity_extract_ids($type, $entity);
    $entity->rdf_mapping = rdf_mapping_load($type, $bundle);
  }
}

We can simplify this now we have propper classed entity objects.

/**
 * Implements hook_entity_load().
 */
function rdf_entity_load($entities, $type) {
  foreach ($entities as $entity) {
    $entity->rdf_mapping = rdf_mapping_load($type, $entity->bundle());
  }
}

After removing all the entity_extract_ids() calls we can remove the function.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

aspilicious’s picture

Title: [META] Remove entity_extract_id() now we have propper classed entity objects » [META] Remove entity_extract_ids() now we have propper classed entity objects

Changing title

aspilicious’s picture

Postponed untill we have revision support

aspilicious’s picture

Status: Active » Postponed
sun’s picture

Issue tags: +Entity system
aspilicious’s picture

Title: [META] Remove entity_extract_ids() now we have propper classed entity objects » Remove entity_extract_ids() now we have propper classed entity objects
Status: Postponed » Needs review
FileSize
39.32 KB

1) File isn't a proper entity yet
2) Don' know if I handled vid correctly
3) null should be NULL

BUT I still wanted to make this patch to see what would brake :)

aspilicious’s picture

Status: Needs review » Needs work

Few comments for future reference

+++ b/core/modules/field/field.api.phpundefined
@@ -1762,10 +1760,7 @@ function hook_field_storage_load($entity_type, $entities, $age, $fields, $option
+  $vid = isset($entity->vid) ? $entity->vid : $entity->id();

We probably need a vid() funtion like we have with id() and bundle() that way we can reset it to a default we want (for each entity).

When we have a proper revision framework for every entity we just have to change the implementation of these vid() functions. Right?

+++ b/core/modules/field/modules/field_sql_storage/field_sql_storage.moduleundefined
@@ -653,16 +646,14 @@ function _field_sql_storage_query_field_conditions(EntityFieldQuery $query, Sele
+        ->condition('entity_id', $entity->id)

$entity->id()

15 days to next Drupal core point release.

fago’s picture

We probably need a vid() funtion like we have with id() and bundle() that way we can reset it to a default we want (for each entity).

Right. I remember we left it out revisions for the EntityInterface in the first place. I've created #1612014: Create an interface for revisionable entities for that.

aspilicious’s picture

Assigned: Unassigned » aspilicious

Give it to me!

aspilicious’s picture

Assigned: aspilicious » Unassigned
Status: Needs work » Needs review
FileSize
36.78 KB

Ok possible that I made a mistake somewhere and this should probably wait after the list into options module move

Status: Needs review » Needs work

The last submitted patch, 1374030-extract-ids-9.patch, failed testing.

aspilicious’s picture

Status: Needs work » Needs review
FileSize
36.73 KB

hmmm

Status: Needs review » Needs work

The last submitted patch, 1374030-extract-ids-11.patch, failed testing.

aspilicious’s picture

Status: Needs work » Needs review
FileSize
36.62 KB

Stupid php...

Status: Needs review » Needs work

The last submitted patch, 1374030-extract-ids-13.patch, failed testing.

aspilicious’s picture

Status: Needs work » Needs review
FileSize
37.02 KB

Another try! =D

Status: Needs review » Needs work

The last submitted patch, 1374030-extract-ids-15.patch, failed testing.

aspilicious’s picture

ARGGGHH kill those stdObject objects...

Fatal error: Call to undefined method stdClass::id() in /var/lib/drupaltestbot/sites/default/files/checkout/core/modules/field/field.multilingual.inc on line 298
FATAL Drupal\comment\Tests\CommentContentRebuildTest: test runner returned a non-zero error code (255).
Fatal error: Call to undefined method stdClass::bundle() in /var/lib/drupaltestbot/sites/default/files/checkout/core/modules/field/field.attach.inc on line 169
FATAL Drupal\entity\Tests\EntityFieldQueryTest: test runner returned a non-zero error code (255).
aspilicious’s picture

EntityFieldQuery stuff has this code...

      $entity = new stdClass();
      $entity->ftid = $i;
      $entity->fttype = 'test_entity_bundle';
      $entity->{$this->field_names[0]}[LANGUAGE_NOT_SPECIFIED][0]['value'] = $i - 5;
      drupal_write_record('test_entity_bundle', $entity);

We need that stubentity...

Berdir’s picture

This is being discussed in #1551140: Remove stub entities, replace entity_create_stub_entity(), there are two working (although not finished/complete) approaches in there and a long-term proposal for the EFQ part. We need to decide how to continue there.

Berdir’s picture

Status: Needs work » Needs review
Issue tags: -Entity system

#15: 1374030-extract-ids-15.patch queued for re-testing.

Status: Needs review » Needs work
Issue tags: +Entity system

The last submitted patch, 1374030-extract-ids-15.patch, failed testing.

aspilicious’s picture

Status: Needs work » Needs review
FileSize
31.31 KB

Lets see how bad this patch is

Status: Needs review » Needs work
Issue tags: -Entity system

The last submitted patch, 1374030-extract-ids-22.patch, failed testing.

Issue tags: +Entity system

The last submitted patch, 1374030-extract-ids-22.patch, failed testing.

aspilicious’s picture

Status: Needs work » Needs review
FileSize
31.31 KB

second try

Status: Needs review » Needs work

The last submitted patch, 1374030-extract-ids-24.patch, failed testing.

mikeryan’s picture

Title: Remove entity_extract_ids() now we have propper classed entity objects » Remove entity_extract_ids() now we have proper classed entity objects
Status: Needs work » Needs review

Tired of looking at that title;).

aspilicious’s picture

Ok someone else can take this for now. Wont look at this for the next day.

tim.plunkett’s picture

FileSize
923 bytes
30.14 KB

Fatal error: Can't use method return value in write context in core/modules/field/field.api.php on line 1919

I just copied the actual field_sql_storage_field_storage_delete_revision() into hook_field_storage_delete_revision().

Status: Needs review » Needs work

The last submitted patch, drupal-1374030-29.patch, failed testing.

Berdir’s picture

Status: Needs work » Needs review
FileSize
10.9 KB
38.43 KB
function entity_form_field_validate($entity_type, $form, &$form_state) {
  // All field attach API functions act on an entity object, but during form
  // validation, we don't have one. $form_state contains the entity as it was
  // prior to processing the current form submission, and we must not update it
  // until we have fully validated the submitted input. Therefore, for
  // validation, act on a pseudo entity created out of the form values.
  $pseudo_entity = (object) $form_state['values'];
  field_attach_form_validate($entity_type, $pseudo_entity, $form, $form_state);
}

Rly? U serious?

Worked around that for now, fixed lots of exceptions because of undefined $id's and added type hints for $entity in field.attach.inc, to easier see more of these weird calls if there are any. Book tests, which previously exploded, pass now, let's see about everything else...

Status: Needs review » Needs work

The last submitted patch, remove-entity-extract-ids-1374030-31.patch, failed testing.

Berdir’s picture

Assigned: Unassigned » Berdir

Working on this, we'll have to fix the test_entity entity before we can do this but I'm making progress...

Berdir’s picture

Status: Needs work » Needs review
FileSize
52.35 KB

Go testbot, go!

This currently also contains a lot of code that is related to fixing test_entity. I'll extract it into the other issue later, just want to see how far we get with this.

Status: Needs review » Needs work

The last submitted patch, remove-entity-extract-ids-1374030-34.patch, failed testing.

Berdir’s picture

Status: Needs work » Needs review
FileSize
56.54 KB

Next try.

Status: Needs review » Needs work

The last submitted patch, remove-entity-extract-ids-1374030-36.patch, failed testing.

Berdir’s picture

Assigned: Berdir » Unassigned
Status: Needs work » Needs review
FileSize
60.59 KB

Ok, here we go, this should finally pass.

At this point, the conversion of the whole test_entity stuff and the removal of entity_extract_ids() is kinda mixed up because the former is required for this to pass but then required additional test and code changes on top of what's required here so I hope we can get this in together.

I think this is ready for reviews now.

Berdir’s picture

Priority: Normal » Major

I do believe this should be major. #1616930: Fix the field_test entity type is and is now kinda blocked on it and I think we all *really* want entity_extract_ids() to be a thing of the past.

Berdir’s picture

#1723892: Support for revisions for entity save and delete operations would allow us to remove the custom storage controller code here...

xjm’s picture

Issue tags: +API change

Yes please.

sun’s picture

Status: Needs review » Needs work
Issue tags: -API change +API clean-up

Finally! Excellent clean-up.

+++ b/core/modules/field/field.attach.inc
@@ -658,9 +657,13 @@ function field_attach_load($entity_type, $entities, $age = FIELD_LOAD_CURRENT, $
+      if (!($entity instanceof EntityInterface)) {
+        debug(debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS));
+        debug($entity);
+      }

Left-over debugging code?

This patch looks RTBC to me after removing that glitch.

plach’s picture

Great clean up!

+++ b/core/modules/entity/entity.module
@@ -409,12 +371,11 @@ function entity_prepare_view($entity_type, $entities) {
   $info = entity_get_info($entity_type);
-  list($id, $vid, $bundle) = entity_extract_ids($entity_type, $entity);
 
   // A bundle-specific callback takes precedence over the generic one for the
   // entity type.
-  if (isset($info['bundles'][$bundle]['uri callback'])) {
-    $uri_callback = $info['bundles'][$bundle]['uri callback'];
+  if (isset($info['bundles'][$entity->bundle()]['uri callback'])) {
+    $uri_callback = $info['bundles'][$entity->bundle()]['uri callback'];
   }

+++ b/core/modules/field/field.api.php
@@ -584,15 +584,14 @@ function hook_field_storage_update_field($field, $prior_field, $has_data) {
+    $item['file_field_id'] = $entity->id();
+    file_field_delete_file($item, $field, $entity_type, $entity->id());

@@ -2631,17 +2628,15 @@ function hook_field_storage_purge_field_instance($instance) {
+    ->condition('entity_id', $entity->id())
     ->execute();
   db_delete($revision_name)
     ->condition('entity_type', $entity_type)
-    ->condition('entity_id', $id)
+    ->condition('entity_id', $entity->id())

+++ b/core/modules/field/modules/field_sql_storage/field_sql_storage.module
@@ -480,17 +480,15 @@ function field_sql_storage_field_storage_delete($entity_type, $entity, $fields)
     ->condition('entity_type', $entity_type)
-    ->condition('entity_id', $id)
+    ->condition('entity_id', $entity->id())
     ->execute();
   db_delete($revision_name)
     ->condition('entity_type', $entity_type)
-    ->condition('entity_id', $id)
+    ->condition('entity_id', $entity->id())

+++ b/core/modules/file/file.field.inc
@@ -231,13 +229,11 @@ function file_field_insert($entity_type, $entity, $field, $instance, $langcode,
+      file_usage_add(file_load($item['fid']), 'file', $entity_type, $entity->id());

@@ -273,11 +269,9 @@ function file_field_update($entity_type, $entity, $field, $instance, $langcode,
+    file_usage_delete(file_load($item['fid']), 'file', $entity_type, $entity->id(), 0);

+++ b/core/modules/rdf/rdf.module
@@ -395,8 +395,7 @@ function rdf_entity_info_alter(&$entity_info) {
+    $entity->rdf_mapping = rdf_mapping_load($type, $entity->bundle());

Minor thing: could we use a variable to avoid calling the methods multiple times?
(I wouldn't even ask if there wasn't @sun's remark above to fix :)

Berdir’s picture

Status: Needs work » Needs review
FileSize
60.44 KB
766 bytes
+    $item['file_field_id'] = $entity->id();
+    file_field_delete_file($item, $field, $entity_type, $entity->id());

Interesting, looks like I forgot to fix the hook documentation when I removed that function. (not related to this issue). Need to open an issue about that.

Removed the debug code. I added and removed that like 3 times and forgot it multiple times ;)

@plach: I'm not sure. I personally spent quite some time doing performance tests on Entity::id() and we decided to hardcode it, so it's fast and can be called multiple times. So there isn't a performance reason to do so and if it's just for two times, it's IMHO ok to call it directly. But I'm fine with changing it if others agree.

Updated patch attached. Manual interdiff looks bigger, but I verified that's just because code that I'm changing has been moved around.

Berdir’s picture

Issue tags: +API change

Re-adding lost tag.

plach’s picture

Status: Needs review » Reviewed & tested by the community

No problem let's go ahead with this :)

sun’s picture

Thanks!

Also, I agree with @Berdir - it would be strange to "cache" the property values in local variables, for the sole purpose of avoiding the method calls. Those calls should be lightning fast, so I don't see an issue with that.

plach’s picture

Actually, it wasn't a performance concern, just an esthetic one: in many places we have $id or $bundle holding the values returned by the related methods. I was arguing that having a consistent approach would be nice.

Dries’s picture

Status: Reviewed & tested by the community » Fixed

Nice clean-up. I still think id() should be getID() but we can worry about that later. Committed to 8.x. Thanks.

Berdir’s picture

Title: Remove entity_extract_ids() now we have proper classed entity objects » Change notification for:remove entity_extract_ids() now we have proper classed entity objects
Priority: Major » Critical
Status: Fixed » Active

Wow, that was fast. Re-opening for change notice.

Berdir’s picture

Status: Active » Needs review
aspilicious’s picture

I would link to "Entity" base class if thats possible on api.d.o. There they can find all the available methods. The current example doesn't say how to convert $vid or $bundle.

Berdir’s picture

Linked those methods. I think if you see how id() is called, it's kinda obvious. But I really wanted to show that $entity_type no longer needs to be passed around and how function definitions should be updated, even though that's not directly related.

And yes, all these field methods still receive $entity_type, not sure if we want to do that to the field functions as well or wait for the field API rewrite based on plugins.

tim.plunkett’s picture

Title: Change notification for:remove entity_extract_ids() now we have proper classed entity objects » Remove entity_extract_ids() now that we have proper classed entity objects
Priority: Critical » Major
Status: Needs review » Fixed

Looks good to me.

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