0

I am rewriting my config fields (Field API fields) into base fields and I am unsure about one thing: settings.

Base fields have the setSettings() method, which I presume is equivalent of field instance settings. Then, there is getFieldStorageDefinition()->setSettings(), which I presume is equivalent of storage settings.
When I look at the entity reference item and the user ID field used by the node entity, the user ID field calls setSetting('target_type', 'user'), which is storage setting and not instance setting. Do I get these two wrong? What is going on?

Node::baseFieldDefinitions() contains the following code.

$fields['uid'] = BaseFieldDefinition::create('entity_reference')
  ->setLabel(t('Authored by'))
  ->setDescription(t('The username of the content author.'))
  ->setRevisionable(TRUE)
  ->setSetting('target_type', 'user')

EntityReferenceItem::defaultStorageSettings() contains the following code.

return [
  'target_type' => \Drupal::moduleHandler()->moduleExists('node') ? 'node' : 'user',
] + parent::defaultStorageSettings();
0

1 Answer 1

1

The base field definition implements the FieldDefinitionInterface and the FieldStorageDefinitionInterface, so it can act like a form API field, though it's not managed by the field module.

So the base field definition contains the settings of both, which you can see how the default values are set in BaseFieldDefinition::create():

  public static function create($type) {
    ...
    $default_settings = $field_type_manager->getDefaultStorageSettings($type) + $field_type_manager->getDefaultFieldSettings($type);
    $field_definition->itemDefinition->setSettings($default_settings);
    return $field_definition;
  }
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.