Limit source entities in settings to ones that have fields

Created on 7 February 2025, 15 days ago

Problem/Motivation

The current settings form shows all entity types in the source entities section. However, entity_usage can only track entities with fields. This is confusing to new users and causes problems in things like the batch update code when trying to get field defs and the like. Common entities without fields are view, webform, et.al.

Only entities that have fields should be shown as source entities. FYI - all entities can be targets.

Steps to reproduce

Go to source type settings, see view entity type shown. Select it, then run a batch update and it will fail with an exception because the view entity does not have base fields.

Proposed resolution

Filter the source options by checking if the entity type class has the getFields method (most reliable test).

Remaining tasks

Patch attached.

User interface changes

Changes settings form

API changes

None

Data model changes

None

Feature request
Status

Active

Version

2.0

Component

Code

Created by

🇺🇸United States cgmonroe

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

  • Issue created by @cgmonroe
  • 🇺🇸United States cgmonroe

    Here's the patch

  • 🇬🇧United Kingdom alexpott 🇪🇺🌍
    +++ b/src/Form/EntityUsageSettingsForm.php
    @@ -113,8 +114,13 @@ class EntityUsageSettingsForm extends ConfigFormBase {
    +      // If the entity type doesn't have fields it can't be a source, e.g. views
    +      if (method_exists($entity_type->getOriginalClass(), 'getFields')) {
    +        $source_options[$entity_type->id()] = $entity_type->getLabel();
    

    I think we should be checking FieldableEntityInterface so something like:

          if ($entity_type->entityClassImplements(FieldableEntityInterface::class)) {
            $source_options[$entity_type->id()] = $entity_type->getLabel();
          }
    

    You will need to add a use statement too.

    FWIW I think this is a bug.

  • 🇬🇧United Kingdom alexpott 🇪🇺🌍

    I think theoretically you could implement any entity tracking plugin that does not use fields BUT the entity track plugin definition has field_types so it is going to be quite hard.

    But this potentially would break such a use-case. Maybe the way around this is to look at all the entity track plugins and check to see if the extend from \Drupal\entity_usage\EntityUsageTrackBase - if they all do then there we should not include non-fiedldable types. This would improve the UI for 99.5% of users.

  • 🇮🇳India sayan_k_dutta

    Working on it.

  • Pipeline finished with Failed
    9 days ago
    Total: 291s
    #422948
  • 🇮🇳India sayan_k_dutta

    Checking using FieldableEntityInterface seems to filter out the entity types without any fields from the source entities list. Created the MR !116 according to it.
    I have not tried using the EntityUsageTrackBase as I am not clear of the idea of how to use it to check for entities without fields.

Production build 0.71.5 2024