By Travis


2019-03-12 15:15:43 8 Comments

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;
  }

0 comments

Related Questions

Sponsored Content

1 Answered Questions

[SOLVED] How can I create Change Password block?

  • 2019-01-21 07:20:06
  • kpda806G
  • 47 View
  • 0 Score
  • 1 Answer
  • Tags:   views 8 users blocks

0 Answered Questions

How to handle validation (email along with custom user field) user registration via REST API?

  • 2018-12-16 17:03:22
  • Parvateesam
  • 60 View
  • 1 Score
  • 0 Answer
  • Tags:   8 users

0 Answered Questions

Apply constraint conditionally

  • 2018-12-11 15:25:39
  • Mag
  • 52 View
  • 2 Score
  • 0 Answer
  • Tags:   8 forms

1 Answered Questions

1 Answered Questions

[SOLVED] Webform field default value + Profile field value

  • 2018-06-17 00:04:48
  • arnaldop
  • 142 View
  • 1 Score
  • 1 Answer
  • Tags:   8 users webforms

1 Answered Questions

[SOLVED] Show "Add more items" for fields of limited cardinality?

  • 2016-12-12 18:48:42
  • Kevin
  • 296 View
  • 6 Score
  • 1 Answer
  • Tags:   8 entities

3 Answered Questions

[SOLVED] How to give custom function in D8 custom field widget class?

  • 2016-08-16 10:09:40
  • Jitha M Saroj
  • 694 View
  • 0 Score
  • 3 Answer
  • Tags:   8

1 Answered Questions

[SOLVED] Access Individual Fields - Contact Form - Swiftmailer

  • 2016-09-08 10:14:44
  • Daniel Dewhurst
  • 267 View
  • 1 Score
  • 1 Answer
  • Tags:   8 emails

1 Answered Questions

[SOLVED] How to use set() on a field within a field collection?

  • 2016-08-03 20:06:16
  • saramm1
  • 71 View
  • 0 Score
  • 1 Answer
  • Tags:   8 entities

1 Answered Questions

[SOLVED] Trigger table update when changes are made inside of baseFieldDefinitions

  • 2016-01-01 18:50:15
  • DenLilleMand
  • 143 View
  • 0 Score
  • 1 Answer
  • Tags:   entities 8

Sponsored Content