2

I would like to store a password in Drupal using a BaseFieldDefinition (since that's what the other fields on this entity are using) for use with authenticating via a third-party service, using OAuth 2.0. Currently, I'm storing the password using BaseFieldDefinition::create('password'). I would like to use this value at one point to authenticate using API requests, however, I am unable to get its value like a normal field.

How can I implement a password on an entity, using BaseFieldDefinitions? The password is not for use within Drupal, so I need to be able to access the value.

One thought that I had was storing it as a string field, not loading a default value on the form, and using the encryption module to manually encrypt the data before storing it and decrypt it when I need it, but this is very cumbersome and I'd rather not add the extra dependency.

Edit: Here's the code showing how I construct the field:

  public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
    $fields = [];
    /* Code constructing other fields... */
    $fields['password'] = BaseFieldDefinition::create('password')
      ->setLabel(t('Password'))
      ->setDescription(t('Password for authenticating via vendor\'s API.'));
    return $fields;
  }

And here's where I attempt to retrieve the value in the same entity:

  public function getPassword() {
    return $this->get('password')->value;
  }
  • 1
    Can you show code on how you're tryng to retrive its value? – No Sssweat Mar 12 at 19:34

Your Answer

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

Browse other questions tagged or ask your own question.