0

I've created a new custom basefield for user entity type like this:

function hd_is_integration_entity_base_field_info_alter(&$fields, \Drupal\Core\Entity\EntityTypeInterface $entity_type) {
  if ($entity_type->id() === 'user') {
    $fields['is_uid'] = BaseFieldDefinition::create('text')
      ->setName('is_uid')
      ->setLabel(t('IS User ID'))
      ->setDescription(t('ID of this user in HD IS.'))
      ->setSettings([
        'max_length' => 50,
        'text_processing' => 0,
      ])
      ->setDefaultValue('')
      ->setDisplayOptions('view', [
        'label' => 'above',
        'type' => 'string',
        'weight' => 0,
      ])
      ->setDisplayOptions('form', [
        'type' => 'string_textfield',
        'weight' => 0,
      ])
      ->setDisplayConfigurable('form', TRUE)
      ->setDisplayConfigurable('view', TRUE);
  }
}

I've also updated the entity schema with update hook:

function hd_is_integration_update_8001(&$sandbox) {
  $field_storage_definition = BaseFieldDefinition::create('string')
    ->setName('is_uid')
    ->setLabel(t('IS User ID'))
    ->setDescription(t('ID of this user in HD IS.'))
    ->setSettings([
      'max_length' => 50,
      'text_processing' => 0,
    ])
    ->setDefaultValue('')
    ->setDisplayOptions('view', [
      'label' => 'above',
      'type' => 'string',
      'weight' => 0,
    ])
    ->setDisplayOptions('form', [
      'type' => 'string_textfield',
      'weight' => 0,
    ])
    ->setDisplayConfigurable('form', TRUE)
    ->setDisplayConfigurable('view', TRUE);

  \Drupal::entityDefinitionUpdateManager()
    ->installFieldStorageDefinition('is_uid', 'user', 'user', $field_storage_definition);
}

The field is displaying fine in the user edit form, but any value added in it isn't saved. What am I missing, please?

1
  • 1
    As I see from your description you've missed ->setTargetEntityTypeId('user') – Yuriy Stenin Mar 8 at 16:08
1

In the end, I mofified my approach using https://www.drupal.org/project/one_time_password as an example. The difference is probably in the installation of the field definition - hook_install() works just fine.

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.