Field storage target_type isn't getting set for plugin handling entity_reference fields

Created on 26 March 2024, 8 months ago
Updated 19 September 2024, 2 months ago

Problem/Motivation

Custom plugin for entity_reference field type is following the ReverseEntityReference example:

public function getFieldDefinitionSettings(): array {
  return [
    'target_type' => 'media',
  ];
}

However on the Manage Fields screen the field still says "Reference type: Content", and on Manage Display none of the media-specific formatters show up. It seems that something is missing to get the field definition settings into the right spot so the rest of the field system can see them.

πŸ› Bug report
Status

Fixed

Version

4.0

Component

Code

Created by

πŸ‡ΊπŸ‡ΈUnited States karlshea Minneapolis πŸ‡ΊπŸ‡Έ

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

Merge Requests

Comments & Activities

  • Issue created by @karlshea
  • πŸ‡ΊπŸ‡ΈUnited States karlshea Minneapolis πŸ‡ΊπŸ‡Έ

    I wonder if returning target_type from getFieldDefinitionSettings() is a red herring? It looks like EntityReferenceItem from core is returning target_type from defaultStorageSettings(), maybe there needs to be a way of influencing that from within a computed field plugin?

  • πŸ‡ΊπŸ‡ΈUnited States karlshea Minneapolis πŸ‡ΊπŸ‡Έ

    Yes, looks like it, e.g. EntityReferenceItem::storageSettingsSummary.

    $storage_definition is \Drupal\computed_field\Field\FieldStorageDefinition, which in its create() is initializing the defaults for the field type, which is "node" target_type. It doesn't seems like there's any way to alter that either.

  • πŸ‡ΊπŸ‡ΈUnited States karlshea Minneapolis πŸ‡ΊπŸ‡Έ

    Looks like ComputedFieldSettingsTrait is also conflating storage settings vs field settings as well which I don't think is hurting anything but I'm also not sure it's helping anything.

    Drupal\computed_field\Field\FieldStorageDefinition might need a way of talking to the computed field plugin to get field storage settings? Or possibly getFieldStorageDefinition() in the ComputedField entity might do it? hook_entity_bundle_field_info_alter can't help because all you can do is set the settings on the storage definition which is just recreated each time it's accessed.

  • Pipeline finished with Failed
    8 months ago
    Total: 142s
    #129229
  • πŸ‡ΊπŸ‡ΈUnited States karlshea Minneapolis πŸ‡ΊπŸ‡Έ

    Looks like ComputedFieldSettingsTrait is also conflating storage settings vs field settings as well which I don't think is hurting anything but I'm also not sure it's helping anything.

    Nope, it needs those settings there too.

  • Pipeline finished with Success
    8 months ago
    #129239
  • Status changed to Needs review 8 months ago
  • πŸ‡ΊπŸ‡ΈUnited States karlshea Minneapolis πŸ‡ΊπŸ‡Έ

    Would definitely appreciate a look at this, seems to have fixed the entity_reference issue for me!

  • πŸ‡ΊπŸ‡ΈUnited States karlshea Minneapolis πŸ‡ΊπŸ‡Έ

    Looks like this interacts with the cardinality patch, some of the media formatters look at the field storage definition cardinality in their isApplicable method.

    When that patch is in this will also need to be added to ComputedField::getFieldStorageDefinition():

    // Set cardinality
    $definition->setCardinality($plugin->getFieldCardinality());
    
  • Pipeline finished with Success
    8 months ago
    Total: 202s
    #129918
  • πŸ‡ΊπŸ‡ΈUnited States karlshea Minneapolis πŸ‡ΊπŸ‡Έ

    Rebased MR!11 on top of the work in πŸ› Computed field should specify the cardinality of the field Fixed

  • Pipeline finished with Success
    8 months ago
    Total: 140s
    #129930
  • Pipeline finished with Success
    8 months ago
    #129935
  • Status changed to Needs work 6 months ago
  • πŸ‡¬πŸ‡§United Kingdom joachim

    I've committed πŸ› Computed field should specify the cardinality of the field Fixed so this needs rebasing again.

  • Pipeline finished with Success
    6 months ago
    Total: 141s
    #171819
  • Status changed to Needs review 6 months ago
  • πŸ‡ΊπŸ‡ΈUnited States karlshea Minneapolis πŸ‡ΊπŸ‡Έ

    Rebased!

  • πŸ‡¬πŸ‡§United Kingdom joachim

    Made a few tweaks.

    Could you check to see if it still fixes the problem for you?

  • Status changed to Fixed 6 months ago
  • πŸ‡¬πŸ‡§United Kingdom joachim

    Tested it locally and all works. Committed.

    Thanks for reporting and working on this!

    • joachim β†’ committed 2177ee1b on 4.0.x
      Issue #3436047 by KarlShea, joachim: Field storage doesn’t get settings...
  • πŸ‡¬πŸ‡§United Kingdom joachim

    I'm going to file a follow-up for splitting the settings method into field settings & storage settings.

  • πŸ‡ΊπŸ‡ΈUnited States karlshea Minneapolis πŸ‡ΊπŸ‡Έ

    Thank you! It all appears to be working for me as well.

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

  • πŸ‡ΊπŸ‡ΈUnited States JonMcL Brooklyn, NY

    If I am following along with this change, am I correct that this

    public function getFieldDefinitionSettings(): array {
      return [
        'target_type' => 'media',
        'handler' => 'default:media',
        'handler_settings' => [
          'target_bundles' => [
            'image',
          ],
        ],
      ];
    }
    

    needs to be changed to

    
    public function getStorageDefinitionSettings(): array {
      return [
        'target_type' => 'media',
      ];
    }
    
    public function getFieldDefinitionSettings(): array {
      return [
        'handler' => 'default:media',
        'handler_settings' => [
          'target_bundles' => [
            'image',
          ],
        ],
      ];
    }
    

    Am I understanding this change correctly?

  • πŸ‡ΊπŸ‡ΈUnited States karlshea Minneapolis πŸ‡ΊπŸ‡Έ

    @jonmcl yes, that's correct! My computed media field has exactly the same code.

Production build 0.71.5 2024