Problem/Motivation

The entity_field_value_definitions tool fails when retrieving field definitions for certain entity types—specifically media and comment—even when a valid bundle is provided.
For example, when executing the tool with:
entity_type_id: media and bundle: image

the tool returns an error:
Tool Plugin executed unsuccessfully: Error retrieving field definitions: Missing bundle for entity type media

This occurs even though the bundle exists and is valid. The issue appears to be in how the tool creates a stub entity to get default field values:

<?php
// Stub version of entity to get default field values.
$entity = $this->entityTypeManager->getStorage($entity_type_id)->create(['type' => $bundle]);
?>

Media entities use bundle as the property name for their bundle, not type. Different entity types use different property names for their bundle key, and hardcoding 'type' causes failures for entity types that use a different bundle key.

Steps to reproduce

Install Tool API module with tool_content submodule
Test entity_field_value_definitions tool using tools explorer
Execute the tool with parameters:

  1. entity_type_id: media
  2. bundle: image

Observe the error: "Missing bundle for entity type media"
Note: The tool works correctly for nodes and taxonomy terms but fails for media & comment entities

Proposed resolution

Use the entity type's bundle key dynamically instead of hardcoding 'type':
Current code

<?php
// Stub version of entity to get default field values.
$entity = $this->entityTypeManager->getStorage($entity_type_id)->create(['type' => $bundle]);
?>

Proposed fix:

<?php
// Stub version of entity to get default field values.
$entity_type = $this->entityTypeManager->getDefinition($entity_type_id);
$bundle_key = $entity_type->getKey('bundle');
$entity = $this->entityTypeManager->getStorage($entity_type_id)->create([
  $bundle_key => $bundle,
]);
?>

User interface changes

none

API changes

none

Data model changes

none

Issue fork tool-3563835

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

prabha1997 created an issue. See original summary.

prabha1997’s picture

Issue summary: View changes
prabha1997’s picture

Status: Active » Needs work
prabha1997’s picture

Issue summary: View changes
prabha1997’s picture

Title: EntityFieldValueDefinitions tool fails with "Missing bundle" error for valid media bundles » EntityFieldValueDefinitions tool fails with "Missing bundle" error for media and comment entities
Issue summary: View changes
prabha1997’s picture

Assigned: Unassigned » prabha1997

prabha1997’s picture

Status: Needs work » Needs review
ajits’s picture

Project: Tool API » Tool Belt
Status: Needs review » Needs work

The sub-modules have been moved to "Tool Belt". Moving this issue as well.
@prabha1997 - thank you for your MR. Can you please create it against the new project?