Metatag module is not worked properly with custom entity

Created on 27 September 2023, about 1 year ago
Updated 30 March 2024, 8 months ago

Problem/motivation

Steps to reproduce

Create custom entity , install metatag module & add metatag field using ui or below code

$fields['field_metatag'] = BaseFieldDefinition::create('metatag')
->setLabel(t('Metatag'))
->setDescription(t('The metatag field for your custom entity.'))
->setDisplayOptions('view', [
'label' => 'hidden',
'type' => 'metatag_empty_formatter',
'weight' => 6,
])
->setDisplayOptions('form', [
'type' => 'metatag_default',
'weight' => 6,
])
->setDisplayConfigurable('form', TRUE)
->setTranslatable(TRUE)
->setDisplayConfigurable('view', TRUE);

After that go to metatag configuration and add custom entity name in metatag config & after that create custom entity node then go to database and check metatag field data , you will found no data in metatag field or metatag column , if you manually add metadata during custom enity node create then you will found data entry in meta tag field in database but metatag will not be shown on page

🐛 Bug report
Status

Active

Version

2.0

Component

Code

Created by

🇮🇳India SAHIL ROHILLA01

Live updates comments and jobs are added and updated live.
  • Needs tests

    The change is currently missing an automated test that fails when run with the original code, and succeeds when the bug has been fixed.

Sign in to follow issues

Comments & Activities

  • Issue created by @SAHIL ROHILLA01
  • 🇺🇸United States DamienMcKenna NH, USA

    Just to confirm it - you have a fieldable entity type, you added a Metatag field to it, when editing or creating an entity you filled in that field but you don't see any values stored in the database for that field? And you also had a default configuration created?

    Did you change the default values when you filled in the entity values? Metatag only stores values that were different from the default values, so if you didn't change anything then there wouldn't be anything stored in the database, by design.

    You might try using metatag_generate_entity_all_tags($entity); to see what the module wants to generate for it.

  • 🇮🇳India SAHIL ROHILLA01

    @DameinMckenna , yes i had set a default value in meta config but after that no data has been stored in db and after that i have changed default value during node create or update then data was stored in field_metatag column in db and i had written below code for metatag field and i had also tried metatag field using ui but i had found metatag module was not working proper for custom entity

    $fields['field_metatag'] = BaseFieldDefinition::create('metatag')
    ->setLabel(t('Metatag'))
    ->setDescription(t('The metatag field for your custom entity.'))
    ->setDisplayOptions('view', [
    'label' => 'hidden',
    'type' => 'metatag_empty_formatter',
    'weight' => 6,
    ])
    ->setDisplayOptions('form', [
    'type' => 'metatag_default',
    'weight' => 6,
    ])
    ->setDisplayConfigurable('form', TRUE)
    ->setTranslatable(TRUE)
    ->setDisplayConfigurable('view', TRUE);

  • 🇺🇸United States DamienMcKenna NH, USA

    I haven't tried out doing it that way, so it's possible there might be something missing somewhere. I honestly don't know off-hand, sorry.

    I think it'd be worth starting to fix this by building a simple entity type and testing it

  • 🇮🇳India SAHIL ROHILLA01

    @DamienMcKenna , this issue is only coming in drupal console custom entity (drupal generate:entity:content)
    & not in drush custom entity (drush generate module-content-entity), thanks

  • 🇺🇸United States DamienMcKenna NH, USA

    Can you please compare the entity annotations to see what is different about them?

Production build 0.71.5 2024