AI Content Suggestions cannot be used with custom Entities

Created on 13 November 2024, 5 months ago
Updated 23 January 2025, 2 months ago

Problem/Motivation

The current version of the AI Content Suggestions module can only be used with Nodes and Taxonomy terms because of the way the form alters are handled. This is so that it doesn't get added to entities where it does not provide any useful features (because the entities don't have any fields that are compatible), but it means users who use custom entities for the site cannot cannot use the module.

Proposed resolution

What I'd suggest is that we use hook_entity_type_alter to set a marker on all entities that have compatible fields (mostly text fields) so that a hook_form_alter can check if the current form implements the ContentEntityFormInterface and check for the flag on the entity before running the form alter service that enables the content plugins.

We could also add an admin form that allows users to deselect any entities discovered programmatically that they don't want the module used on, giving more fine-grained control.

Feature request
Status

Active

Version

1.0

Component

Other Submodules

Created by

🇬🇧United Kingdom MrDaleSmith

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

Merge Requests

Comments & Activities

  • Issue created by @MrDaleSmith
  • Pipeline finished with Failed
    2 months ago
    Total: 312s
    #403753
  • Pipeline finished with Success
    2 months ago
    Total: 242s
    #403765
  • 🇬🇧United Kingdom MrDaleSmith

    I decided we'd be best off not trying to automate this because the existing form alter includes string field types and the majority of content entities have at least one string field: filtering out all the ones we assume it won't make sense to use the AI on would add a lot of conditionals and mean we might block users from doing something they want to do.

    Instead I've added a settings form to let users decide for themselves, and an update hook that sets the settings to preserve the previous behaviour for anyone who was already using the module.

  • 🇧🇪Belgium wouters_f Leuven

    Tested this:
    Gave me this error:
    NOTICE: PHP message: Uncaught PHP Exception AssertionError: "Failed to assert that "ai_content_suggestions_plugins, ai_providers, entity_types, user.roles:authenticated" are valid cache contexts." at /var/www/html/web/core/lib/Drupal/Core/Cache/Cache.php line 31
    If i removed the line

    $form['#cache']['contexts'][] = 'entity_types';
    

    From the Settings form, it worked as expected.

  • 🇧🇪Belgium wouters_f Leuven

    (there is also a merge conflict which might prevent it from getting merged. might be best to resolve that too).

  • 🇬🇧United Kingdom MrDaleSmith

    That error comes if you don't clear caches after adding the new ca he context: can you confirm if you did that?

  • Pipeline finished with Success
    about 2 months ago
    Total: 266s
    #413236
Production build 0.71.5 2024