1

The documentation says:

Bundle fields either have to override an existing base field, or need to provide a field storage definition via hook_entity_field_storage_info() unless they are computed.

How can I do that?

I tried something like this:

/**
 * Implements hook_entity_field_storage_info().
 */
function km_mod_entity_field_storage_info(\Drupal\Core\Entity\EntityTypeInterface $entity_type) {
  if ($entity_type->id() == 'node') {
    $definitions['field_metatags'] = \Drupal\Core\Field\BaseFieldDefinition::create('metatag')
      ->setName('field_metatags')
      ->setLabel(t('Metatags'))
      ->setTranslatable(TRUE)
      ->setCardinality(1)
      ->setTargetEntityTypeId($entity_type->id());
    return $definitions;
  }
}


/**
 * Implements hook_entity_bundle_field_info().
 */
function km_mod_entity_bundle_field_info(\Drupal\Core\Entity\EntityTypeInterface $entity_type, $bundle, array $base_field_definitions) {

  if ($entity_type->id() === 'node') {
    $fields = [];

    $fields['field_metatags'] = BaseFieldDefinition::create('metatag')
      ->setLabel(t('Metatags'))
      ->setDisplayOptions('view', array(
        'label' => 'hidden',
        'type' => 'hidden',
        'weight' => 0,
      ))
      ->setDisplayOptions('form', array(
        'type' => 'metatag',
        'weight' => 50,
      ));

    return $fields;
  }
}

It does not work as intended though. The field does not appear during Form display, and when trying to save:

Drupal\Core\Entity\EntityStorageException: Table mapping contains invalid field field_metatags. in Drupal\Core\Entity\Sql\SqlContentEntityStorage->save()

I've checked the field storage config yaml (by creating the field manually) and it doesn't really look any difference to how I try to configure it:

uuid: 4c6ecbd5-49b3-4305-8f47-518f76a917f7
langcode: sv
status: true
dependencies:
  module:
    - metatag
    - node
id: node.field_yaay
field_name: field_yaay
entity_type: node
type: metatag
settings: {  }
module: metatag
locked: false
cardinality: 1
translatable: true
indexes: {  }
persist_with_no_fields: false
custom_storage: false

I have run drush entity-updates as well, with no luck.

So what am I doing wrong?

1

I ran into the same issue. It looks like there might be a problem with Core.

This patch attached to the issue "Hook entity_field_storage_info is broken" fixed things for me.

  • Please QA-review the edit I applied to your answer, feel free to further improve it, or just rollback if you don't like it at all. BTW: the downvote is not mine ... – Pierre.Vriens Nov 15 '17 at 9:15
0

For fix this error i created a class FieldStorageDefinition

use Drupal\Core\Field\BaseFieldDefinition;

class FieldStorageDefinition extends BaseFieldDefinition {

  /**
   * {@inheritdoc}
   */
  public function isBaseField() {
    return FALSE;
  }

}

for created the fields use the class FieldStorageDefinition not BaseFieldDefinition

$fields['field_metatags'] = FieldStorageDefinition::create('metatag')

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.