Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
This module has some tricky unintended inconsistent behavior with entity_metadata_wrapper.
Conveniently when you load a entity with an encrypted field, it will give you the unencrypted
value. Likewise, when you set and save a value on that field programmatically you don't need to encrypt it first.
$my_entity = entity_load($entity_id);
$my_entity_wrapper = entity_metadata_wrapper('some_entity', $my_entity);
$value = $my_entity_wrapper->field_encrypted->value();
//yay $value is decrypted for us!
$my_entity_wrapper->field_encrypted->set("Some unencrypted string");
//new value automatically encrypted
However, if you save the wrapper at some point, the values go back to their encrypted stat:
$my_entity = entity_load($entity_id);
$my_entity_wrapper = entity_metadata_wrapper('some_entity', $my_entity);
$my_entity_wrapper->field_encrypted->set("New value");
$my_entity_wrapper->save();
//Oh noes!! We saved. The value below now is encrypted :-(
print $my_entity_wrapper->field_encrypted->value();
Comments
Comment #0.0
bennybobw CreditAttribution: bennybobw commentedFixed a little comment typo in the example
Comment #1
gregglesCan you state which part of this is a bug?
To be honest, I'm not familiar with the area of code you're talking about. Do you know which part of the encryption module is responsible for this behavior? Are you using encryptfapi module?
Comment #2
gregglesOver 2 weeks without a response. Marking as a fixed support request.
Comment #3
bennybobw CreditAttribution: bennybobw commentedMy bad. I just realized this is in the encrypted_text module, not in the encryption module itself.
Comment #3.0
bennybobw CreditAttribution: bennybobw commentedfixed another typo so it will make more sense