[warning] array_flip(): Can only flip STRING and INTEGER values, when saving a non-revisionable custom content entity

Created on 6 June 2022, over 2 years ago
Updated 19 June 2024, 5 months ago

Problem/Motivation

I have a quite simple, fieldable custom content entity type: It only has a base table (no separate data-table, no revision-tables, and non-translatable). It is declared in a class's annotation.

When I update any entity of this type programmatically I get this warning:

> [warning] array_flip(): Can only flip STRING and INTEGER values! EntityChangesDetectionTrait.php:34

I checked the documentation, and compared also my c.e.t. definition with other c.e.t. definitions but I couldn't spot anything relevant. Maybe I'm missing something, but it seems that in this method: \Drupal\Core\Entity\EntityChangesDetectionTrait::getFieldsToSkipFromTranslationChangesCheck

    $fields = [
      $entity_type->getKey('revision'), // This is FALSE for my entity type
      $entity_type->getKey('revision_translation_affected'), // This is FALSE for my entity type
    ];

    $fields = array_merge($fields, array_values($entity_type->getRevisionMetadataKeys())); // The first 2 elements have FALSE value

    foreach (array_diff_key($entity->getFieldDefinitions(), array_flip($fields)) as $field_name => $field_definition) { // The array_flip is throwing the warning

Steps to reproduce

  1. Create a custom content entity type with just a base table (no data-table, no revisions, no translations)
  2. Create an entity of this type
  3. Load the entity programmatically, change any field value, and save it.

Proposed resolution

The $fields array in the above snippet should be filtered to remove any FALSE values, before using it as input in the array_flip.

Remaining tasks

-

User interface changes

-

API changes

-

Data model changes

-

Release notes snippet

-

πŸ› Bug report
Status

Needs work

Version

11.0 πŸ”₯

Component
EntityΒ  β†’

Last updated about 6 hours ago

Created by

πŸ‡³πŸ‡΄Norway efpapado

Live updates comments and jobs are added and updated live.
  • Needs tests

    The change is currently missing an automated test that fails when run with the original code, and succeeds when the bug has been fixed.

Sign in to follow issues

Merge Requests

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • πŸ‡ΊπŸ‡ΈUnited States smustgrave

    Since there hasn't been a follow up to #9 going to close for now.

    If still a bug please reopen addressing #9

    Thanks all!

  • Status changed to Needs review 5 months ago
  • πŸ‡³πŸ‡΄Norway efpapado

    Reopening the issue since the problem is still here on D10.2.3.

    This is the relevant part of the stacktrace:

    EntityChangesDetectionTrait.php:34, Drupal\Core\Entity\ContentEntityBase->traitGetFieldsToSkipFromTranslationChangesCheck()
    ContentEntityBase.php:1434, Drupal\Core\Entity\ContentEntityBase->getFieldsToSkipFromTranslationChangesCheck()
    ContentEntityBase.php:1475, Drupal\Core\Entity\ContentEntityBase->hasTranslationChanges()
    ChangedItem.php:49, Drupal\Core\Field\Plugin\Field\FieldType\ChangedItem->preSave()
    FieldItemList.php:233, Drupal\Core\Field\FieldItemList->delegateMethod()
    FieldItemList.php:191, Drupal\Core\Field\FieldItemList->preSave()
    ContentEntityStorageBase.php:938, Drupal\Core\Entity\ContentEntityStorageBase->invokeFieldMethod()
    ContentEntityStorageBase.php:888, Drupal\Core\Entity\ContentEntityStorageBase->invokeHook()
    EntityStorageBase.php:529, Drupal\Core\Entity\EntityStorageBase->doPreSave()
    ContentEntityStorageBase.php:753, Drupal\Core\Entity\ContentEntityStorageBase->doPreSave()
    EntityStorageBase.php:483, Drupal\Core\Entity\EntityStorageBase->save()
    SqlContentEntityStorage.php:806, Drupal\Core\Entity\Sql\SqlContentEntityStorage->save()
    EntityBase.php:352, Drupal\Core\Entity\EntityBase->save()
    ...

    @larowlan

    Looking at \Drupal\Core\Entity\ContentEntityStorageBase::populateAffectedRevisionTranslations which I think would be the calling parent here, can you check if it gets inside the if statement

        if ($this->entityType->isTranslatable() && $this->entityType->isRevisionable()) {
    

    I suspect it should not, so you might need to check those two methods.

    Up until the array_flip that throws the warning, the \Drupal\Core\Entity\ContentEntityStorageBase::populateAffectedRevisionTranslations is not called at all.

    I created a merge request.

  • πŸ‡³πŸ‡΄Norway efpapado

    Hiding the patch file, as a MR has now been used, and changing the version to 11.x

  • Pipeline finished with Success
    5 months ago
    Total: 564s
    #197893
  • Status changed to Needs work 5 months ago
  • πŸ‡ΊπŸ‡ΈUnited States smustgrave

    This may count as not needing tests but think should least include a comment for the change.

  • Status changed to Needs review 5 months ago
  • πŸ‡³πŸ‡΄Norway efpapado

    Pushed new commit with a comment as requested.

  • Pipeline finished with Success
    5 months ago
    Total: 561s
    #202805
  • Status changed to RTBC 5 months ago
  • πŸ‡ΊπŸ‡ΈUnited States smustgrave

    Going to lean on this being small enough to not need test coverage so will mark.

  • Status changed to Needs work 5 months ago
  • πŸ‡¦πŸ‡ΊAustralia larowlan πŸ‡¦πŸ‡ΊπŸ.au GMT+10

    I feel this one is valid enough to add tests for.

    \Drupal\entity_test\Entity\EntityTest in core only has a base table, no data table etc.

Production build 0.71.5 2024