1

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

Field API has storage and instance settings. Looking at base fields, there is 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 on 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?

\Drupal\node\Entity\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') <---- this

\Drupal\Core\Field\Plugin\Field\FieldType\EntityReferenceItem contains the following code.

  public static function defaultStorageSettings() { <---- STORAGE, not field
    return [
      'target_type' => \Drupal::moduleHandler()->moduleExists('node') ? 'node' : 'user',
    ] + parent::defaultStorageSettings();
  }
  • 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. – 4k4 Nov 7 '17 at 13:13
  • eeeh, ok and... what are you trying to say? – user21641 Nov 7 '17 at 13:17
0

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;
  }
| improve this answer | |
  • So calling $fields['foo']->getFieldStorageDefinition()->setSetting('target_type', 'bar'); in baseFieldDefinitions() is incorrect? – user21641 Nov 7 '17 at 13:32
  • No it should work, though getFieldStorageDefinition() seems to be redundant, it's only there to implement the interface. – 4k4 Nov 7 '17 at 13:39

Your Answer

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