When running cron on my site, I got the following error:

The website encountered an unexpected error. Please try again later.
Error: Call to a member function getTargetEntityTypeId() on array in field_purge_batch() (line 82 of core/modules/field/field.purge.inc).
field_purge_batch(50) (Line: 167)
field_cron()

This is due to the fact that in field_purge_batch $deleted_fields_repository->getFieldDefinitions($field_storage_unique_id) is producing an array of array and not an array of objects that woul fit with the last line in the below extract $field->getTargetEntityTypeId();

function field_purge_batch($batch_size, $field_storage_unique_id = NULL) {
  /** @var \Drupal\Core\Field\DeletedFieldsRepositoryInterface $deleted_fields_repository */
  $deleted_fields_repository = \Drupal::service('entity_field.deleted_fields_repository');
  $fields = $deleted_fields_repository->getFieldDefinitions($field_storage_unique_id);
  $info = \Drupal::entityManager()->getDefinitions();
  foreach ($fields as $field) {
    $entity_type = $field->getTargetEntityTypeId();
....
  }
}

Same problem with getFieldStorageDefinitionsoccurs few lines after

foreach ($deleted_fields_repository->getFieldStorageDefinitions() as $field_storage) {
    if ($field_storage_unique_id && $field_storage->getUniqueStorageIdentifier() != $field_storage_unique_id) {
      // If a specific UUID is provided, only purge the corresponding field.
      continue;
    }
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

DuneBL created an issue. See original summary.

dillix’s picture

I also have this issue after upgrade to 8.5.0-dev

dillix’s picture

Priority: Normal » Major
dillix’s picture

Priority: Major » Critical
Berdir’s picture

And what exactly are those array that you're seeing there? Can you do an var_dump() of that above the failing line? what other modules do you have installed?

Berdir’s picture

Status: Active » Postponed (maintainer needs more info)
dillix’s picture

Status: Postponed (maintainer needs more info) » Active

I started to develop fresh site from Drupal 8.2->8.3->8.4 and all worked fine. But when I upgraded Drupal to 8.5.0-dev cron stopped work. My site have 2 languages: Russian (Default) and English

Here is list of my modules:

        "drupal/address": "~1.0",
        "drupal/admin_toolbar": "~1.16",
        "drupal/bootstrap": "~3.0",
        "drupal/bootstrap_layouts": "3.x-dev",
        "drupal/captcha": "1.x-dev",
        "drupal/colorbox": "~1.2",
        "drupal/commerce": "~2.0",
        "drupal/console": "~1.0",
        "drupal/core": "~8.5",
        "drupal/devel": "1.x-dev",
        "drupal/diff": "1.x-dev",
        "drupal/entity": "1.x-dev",
        "drupal/entity_reference_revisions": "1.x-dev",
        "drupal/fz152": "1.x-dev",
        "drupal/honeypot": "~1.23",
        "drupal/layout_plugin": "1.x-dev",
        "drupal/migrate_plus": "4.x-dev",
        "drupal/migrate_tools": "4.x-dev",
        "drupal/migrate_upgrade": "3.x-dev",
        "drupal/page_manager": "1.x-dev",
        "drupal/panels": "4.x-dev",
        "drupal/paragraphs": "^1.2",
        "drupal/pathauto": "1.x-dev",
        "drupal/recaptcha": "~2.2",
        "drupal/restui": "~1.13",
        "drupal/views_bootstrap": "3.x-dev",
        "drupal/xbbcode": "3.x-dev",
        "drush/drush": "9.0.0-beta4",

Also I have custom entity modules created with drupal console.

And here is var_dump($fields):

array(3) {
  ["113a8e00-92ee-4700-ac62-a613ec5605c9"]=>
  array(19) {
    ["uuid"]=>
    string(36) "113a8e00-92ee-4700-ac62-a613ec5605c9"
    ["langcode"]=>
    string(2) "ru"
    ["status"]=>
    bool(true)
    ["dependencies"]=>
    array(1) {
      ["config"]=>
      array(2) {
        [0]=>
        string(42) "commerce_order.commerce_order_type.default"
        [1]=>
        string(39) "field.storage.commerce_order.line_items"
      }
    }
    ["_core"]=>
    array(1) {
      ["default_config_hash"]=>
      string(43) "OblWxZGIMZi1iUZ9BwHx8PSYAMtVfwxfc3c0Gnq1qSs"
    }
    ["id"]=>
    string(33) "commerce_order.default.line_items"
    ["field_name"]=>
    string(10) "line_items"
    ["entity_type"]=>
    string(14) "commerce_order"
    ["bundle"]=>
    string(7) "default"
    ["label"]=>
    string(10) "Line items"
    ["description"]=>
    string(0) ""
    ["required"]=>
    bool(true)
    ["translatable"]=>
    bool(false)
    ["default_value"]=>
    array(0) {
    }
    ["default_value_callback"]=>
    string(0) ""
    ["settings"]=>
    array(2) {
      ["handler"]=>
      string(26) "default:commerce_line_item"
      ["handler_settings"]=>
      array(0) {
      }
    }
    ["field_type"]=>
    string(16) "entity_reference"
    ["deleted"]=>
    bool(true)
    ["field_storage_uuid"]=>
    string(36) "bc6c53c5-7f2f-4b55-b6d2-d030f1398bc7"
  }
  ["4e34c889-b0ee-4bdf-b01f-f2efff7218ff"]=>
  array(19) {
    ["uuid"]=>
    string(36) "4e34c889-b0ee-4bdf-b01f-f2efff7218ff"
    ["langcode"]=>
    string(2) "ru"
    ["status"]=>
    bool(true)
    ["dependencies"]=>
    array(2) {
      ["config"]=>
      array(2) {
        [0]=>
        string(29) "field.storage.profile.address"
        [1]=>
        string(20) "profile.type.billing"
      }
      ["module"]=>
      array(1) {
        [0]=>
        string(7) "address"
      }
    }
    ["_core"]=>
    array(1) {
      ["default_config_hash"]=>
      string(43) "lN6kY8o1V0jVmtcqn-3mK3Qa1yW_xqGqORS9LmwpXeI"
    }
    ["id"]=>
    string(23) "profile.billing.address"
    ["field_name"]=>
    string(7) "address"
    ["entity_type"]=>
    string(7) "profile"
    ["bundle"]=>
    string(7) "billing"
    ["label"]=>
    string(7) "Address"
    ["description"]=>
    string(0) ""
    ["required"]=>
    bool(true)
    ["translatable"]=>
    bool(false)
    ["default_value"]=>
    array(0) {
    }
    ["default_value_callback"]=>
    string(0) ""
    ["settings"]=>
    array(3) {
      ["available_countries"]=>
      array(0) {
      }
      ["fields"]=>
      array(11) {
        ["administrativeArea"]=>
        string(18) "administrativeArea"
        ["locality"]=>
        string(8) "locality"
        ["dependentLocality"]=>
        string(17) "dependentLocality"
        ["postalCode"]=>
        string(10) "postalCode"
        ["sortingCode"]=>
        string(11) "sortingCode"
        ["addressLine1"]=>
        string(12) "addressLine1"
        ["addressLine2"]=>
        string(12) "addressLine2"
        ["organization"]=>
        string(12) "organization"
        ["givenName"]=>
        string(9) "givenName"
        ["additionalName"]=>
        string(14) "additionalName"
        ["familyName"]=>
        string(10) "familyName"
      }
      ["langcode_override"]=>
      string(0) ""
    }
    ["field_type"]=>
    string(7) "address"
    ["deleted"]=>
    bool(true)
    ["field_storage_uuid"]=>
    string(36) "1043b110-a133-4b5f-9d40-3b875fbcdeb1"
  }
  ["79e8eaa5-1bbe-4e65-836d-ba9306753434"]=>
  array(18) {
    ["uuid"]=>
    string(36) "79e8eaa5-1bbe-4e65-836d-ba9306753434"
    ["langcode"]=>
    string(2) "ru"
    ["status"]=>
    bool(true)
    ["dependencies"]=>
    array(2) {
      ["config"]=>
      array(2) {
        [0]=>
        string(46) "commerce_product.commerce_product_type.default"
        [1]=>
        string(52) "field.storage.commerce_product.field_soft_group_type"
      }
      ["module"]=>
      array(1) {
        [0]=>
        string(7) "options"
      }
    }
    ["id"]=>
    string(46) "commerce_product.default.field_soft_group_type"
    ["field_name"]=>
    string(21) "field_soft_group_type"
    ["entity_type"]=>
    string(16) "commerce_product"
    ["bundle"]=>
    string(7) "default"
    ["label"]=>
    string(15) "soft_group_type"
    ["description"]=>
    string(0) ""
    ["required"]=>
    bool(false)
    ["translatable"]=>
    bool(false)
    ["default_value"]=>
    array(0) {
    }
    ["default_value_callback"]=>
    string(0) ""
    ["settings"]=>
    array(0) {
    }
    ["field_type"]=>
    string(11) "list_string"
    ["deleted"]=>
    bool(true)
    ["field_storage_uuid"]=>
    string(36) "b2d4e8b5-1324-4989-93e6-01133540e608"
  }
}

I think that that I've got problems with cron after this issue was resolved: #2282119: Make the Entity Field API handle field purging

Berdir’s picture

Yes, that issue is probably related and there seems to be a problem there. We need to either add an update function to convert the existing state entries or convert them into objects when loading them.

But first, please be aware that 8.5.x is not even in alpha yet and 8.5.x -> 8.5.x updates are *not* yet officially supported and it is possible that going from alpha1 to alpha2 or so will *not* work. You should not yet switch to 8.5.x, it is too early. If you just did a test to make sure you're prepared for 8.5.x once it comes out, great.

As a workaround, you can run field_cron() until all deleted fields have been cleaned up. Does that actually work? If not, due to a missing tables or so, then that's where your real problem is.

dillix’s picture

No, it didn't help. field_cron() fails to run with same error. Is there a way to manually do it? And what should I do if I already moved prod site to 8.5.x-dev? Don't upgrade current 8.5.x-dev till 8.5.x-alpha-2?

Berdir’s picture

Well, I mean running field_cron() on 8.4.x.

I honestly don't know what you should do, the safest option would be to restore a backup and go back to stable 8.4. With any other option, you're basically on your own to figure things out and at least wait until those bugs are fixed. There are also still some other known regressions in 8.5.x.

dillix’s picture

Berdir, I can't move back to backup because there is new users & forum topics on our site. Can I downgrade site to 8.4 with composer? And will this issue be fixed there or there is another related issue?

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.0-alpha1 will be released the week of January 17, 2018, which means new developments and disruptive changes should now be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

dillix’s picture

Version: 8.6.x-dev » 8.5.x-dev
cilefen’s picture

Title: field_purge_batch expect array of objects and get array of array » field_purge_batch expects an array of objects but instead gets an array of arrays
xjm’s picture

Issue tags: +8.5.0 release notes
amateescu’s picture

Status: Active » Needs review
FileSize
1.61 KB

This patch should fix it.

@dillix, can you make a backup copy of your site (files + database), apply this patch, run the updates and then run cron?

Status: Needs review » Needs work

The last submitted patch, 16: 2931436.patch, failed testing. View results

amateescu’s picture

Status: Needs work » Needs review
FileSize
1.6 KB
1.26 KB

Oops, copy/pasta error :)

plach’s picture

Status: Needs review » Needs work
Issue tags: +Needs tests

Nice, we also need a test for this.

amateescu’s picture

Status: Needs work » Needs review
Issue tags: -Needs tests
FileSize
15.26 KB
16.87 KB

There we go.

Berdir’s picture

Status: Needs review » Reviewed & tested by the community

Looks great, nice work.

In regards to the release notes tag, this is only relevant if we don't commit it in time I guess.

plach’s picture

Version: 8.5.x-dev » 8.6.x-dev

This needs to go in 8.6.x first.

The last submitted patch, 20: 2931436-20-test-only.patch, failed testing. View results

dillix’s picture

Great news! Yes now cron works on D8.5.0-a1.

  • Gábor Hojtsy committed ec9ea10 on 8.6.x
    Issue #2931436 by amateescu, dillix, Berdir, DuneBL, plach:...

  • Gábor Hojtsy committed 1d52eab on 8.5.x
    Issue #2931436 by amateescu, dillix, Berdir, DuneBL, plach:...
Gábor Hojtsy’s picture

Version: 8.6.x-dev » 8.5.x-dev
Status: Reviewed & tested by the community » Fixed

Yay, this was important to get fixed on time! Thanks all! Leaving release notes tag in case we believe we want to mention it still.

tannerg’s picture

woot!

Status: Fixed » Closed (fixed)

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

Musa.thomas’s picture

wrong comment

0Sarah0Al’s picture

I am having the exact same error after I updated to 8.5.1 from 8.4.5

Now, I am stuck. can not run update.php

I got this, when I tried to run update.php:

An AJAX HTTP error occurred.
HTTP Result Code: 200
Debugging information follows.
Path: /drupal/update.php/start?id=311&op=do_nojs&op=do
StatusText: OK
ResponseText: {"status":true,"percentage":"4","message":"Completed 1 of 23.","label":"Updating webform"}Error: Call to a member function getTargetEntityTypeId() on array in field_purge_batch() (line 82 of /media/sf_sandbox/drupal/core/modules/field/field.purge.inc).

amateescu’s picture

@Sarahphp1, one easy way out of this problem is to run cron a few times before doing the upgrade, which should remove the deleted fields from the database.

You can also do a database query to find out if there are still any fields to purge with this query:

SELECT * FROM `key_value` WHERE `collection` = 'state' AND (`name` LIKE 'field.field.deleted' OR `name` LIKE 'field.storage.deleted')

When there are no remaining fields that need to be purged, the value column returned by that query should be: a:0:{}.

0Sarah0Al’s picture

Thanks for your reply @amateescu

I can't access the UI because of the error. I tried "drush cron" and then I got the same error ..

I am gonna try using code, and will let you know!

Thanks again ,,

amateescu’s picture

@Sarahphp1, if you are using drush, then there's also a quick fix by running:

drush eval "module_load_install('field'); field_update_8500();"

After this you should be able to run update.php normally :)

0Sarah0Al’s picture

@amateescu

Thank you so so very much ,,
You are a life saver...

That drush command fixed it ,,

amateescu’s picture

No problem, I'm glad I could help :)

bajah1701’s picture

Is there a way to fix this issue if I don't have access to the UI or Drush? I checked the database and the value column for name="field.storage.deleted" has stuff inside but the other one has a:{0}, which is what needs to be there.