I found a problem with processEntityIds() which returns an error code if the entity is deleted or no longer exists.

TypeError: Argument 1 passed to Drupal\Core\Entity\EntityRepository::getTranslationFromContext() must implement interface Drupal\Core\Entity\EntityInterface, null given, called in /var/www/html/web/modules/contrib/entity_browser/src/Element/EntityBrowserElement.php on line 259 in Drupal\Core\Entity\EntityRepository->getTranslationFromContext() (line 99 of core/lib/Drupal/Core/Entity/EntityRepository.php).

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

Anwoon created an issue. See original summary.

anwoon’s picture

Issue summary: View changes
anwoon’s picture

Version: 8.x-1.x-dev » 8.x-2.2
StatusFileSize
new730 bytes
anwoon’s picture

StatusFileSize
new728 bytes
judapriest’s picture

Assigned: anwoon » Unassigned
Status: Active » Needs review

Got the same error

Step to reproduce :

  • I add a media
  • My media is used in a custom field that reference media (ex : block in homepage)
  • Everything is fine on Homepage and the form with the custom field
  • I deleted the media from /admin/content/media
  • I got an error on the homepage and on the form with the custom field

Pacth seems to work fine.

leontin’s picture

StatusFileSize
new933 bytes

Your patch didn't work for me.
Because you return an empty array, instead a Entity, I got another error:

TypeError: Argument 1 passed to Drupal\entity_browser\Plugin\Field\FieldWidget\EntityReferenceBrowserWidget::Drupal\entity_browser\Plugin\Field\FieldWidget\{closure}() must implement interface Drupal\Core\Entity\EntityInterface, array given in Drupal\entity_browser\Plugin\Field\FieldWidget\EntityReferenceBrowserWidget->Drupal\entity_browser\Plugin\Field\FieldWidget\{closure}() (line 401 of modules/contrib/entity_browser/src/Plugin/Field/FieldWidget/EntityReferenceBrowserWidget.php).

I propose another patch.

avpaderno’s picture

Version: 8.x-2.2 » 8.x-2.x-dev

avpaderno’s picture

Status: Needs review » Needs work
soutams’s picture

We are facing the same issue.
Is there any solution/patch available?
I'm using Entity Browser 2.9 version.

We are getting below error message

TypeError: Drupal\Core\Entity\EntityRepository::getTranslationFromContext(): Argument #1 ($entity) must be of type Drupal\Core\Entity\EntityInterface, null given, called in /docroot/modules/contrib/entity_browser/src/Element/EntityBrowserElement.php on line 259 in Drupal\Core\Entity\EntityRepository->getTranslationFromContext() (line 93 of /docroot/core/lib/Drupal/Core/Entity/EntityRepository.php)

mediabounds’s picture

Status: Needs work » Needs review
StatusFileSize
new956 bytes

We're also encountering this issue essentially with same the steps as in #5 (although for us it as with nodes).

I'm attaching a slightly different version of the patch in #6.

When processing the entity IDs, if it encounters one that doesn't exist, it just skips over it. This seems (to me) to be consistent with how Drupal core works on entity reference fields.

soutams’s picture

@mediabounds,

https://www.drupal.org/files/issues/2023-10-20/3085476-12.patch after applying this patch the mentioned issue got resolved, but still not able to submit the node. It is giving "media:null" error. Exact error "The referenced entity (media: null) does not exist."

I'm trying to embed youtube video in an entity reference field. But facing all these issues.

I'm using Entity Browser 2.9 version.

nicocin made their first commit to this issue’s fork.

nicocin’s picture

StatusFileSize
new1.16 KB

Patch for 2.15.

benstallings’s picture

Status: Needs review » Needs work

Claude Code says (emphasis added):

What it does: Two changes bundled together:

1. Adds a null check after load(). If an entity no longer exists (deleted between selection and processing), the current code passes null to getTranslationFromContext(), which throws a fatal error. The patch skips missing entities with if ($entity). This is a good fix for a real crash scenario.
2. Removes getTranslationFromContext(). The current code returns translated entities; the patch returns untranslated ones. This is a behavioral regression — multilingual sites depend on this to get entities in the current language context. Removing it silently breaks translation support.

Other issues:

- Indentation bug. The $entities = []; line is indented with 2 spaces instead of the surrounding 4. Would fail phpcs.
- list() vs [] style. The patch changes [$a, $b] = ... to list($a, $b) = .... The short syntax is the modern PHP convention and what the codebase already uses. This is a step backward.
- Commit message is "aa". Not useful.

What it should be: Keep the null check, keep the translation call, fix the indentation:

  foreach ($ids as $item) {
      [$entity_type, $entity_id] = explode(':', $item);
      $entity = \Drupal::entityTypeManager()->getStorage($entity_type)->load($entity_id);
      if ($entity) {
          $entities[] = \Drupal::service('entity.repository')->getTranslationFromContext($entity);
      }
  }

Summary: The null safety fix is needed, but the patch removes translation support as a side effect. Needs rework to preserve getTranslationFromContext().

benstallings’s picture

Status: Needs work » Needs review
anybody’s picture

Status: Needs review » Needs work

benstallings’s picture

1. Translation support restored — getTranslationFromContext() is back, now wrapped in the null check so deleted entities are skipped instead of crashing the call.
2. Indentation fixed — the rewritten block uses the surrounding 2-space Drupal indentation.
3. Modern array destructuring — kept [$entity_type, $entity_id] = explode(':', $item) rather than reverting to list().
4. Commit message — reworded to follow the standard Issue #NNNNN by user: format.

One behavior change worth calling out explicitly: switching from array_map to a foreach with $entities[] means missing entities are silently dropped and the result is reindexed sequentially, where the previous code would have returned NULL entries (and then crashed on them). I checked the callers in EntityReferenceBrowserWidget::getEntities() / ::getCurrentUserInputEntities() and entity_browser_entity_form_*_submit() — all of them consume the result as a flat list and don't rely on the keys, so dropping missing entries should be safe. Flagging it here in case anyone (or anybody) wants to confirm before RTBC.

benstallings’s picture

Status: Needs work » Needs review
avpaderno’s picture

Issue summary: View changes

berdir made their first commit to this issue’s fork.

berdir’s picture

Status: Needs review » Fixed

Merging.

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.

berdir’s picture

Title: Bug with processEntityIds() » Bug with processEntityIds() when entity can not be loaded

  • berdir committed 56181088 on 8.x-2.x authored by benstallings
    fix: #3085476 Bug with processEntityIds() when entity can not be loaded...

Status: Fixed » Closed (fixed)

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