Score:0

How to store a safevalue of a field in addition to the original?

kz flag

I have a custom field type that I create with a custom module. It is a plain text field where the user type in a string. I want to retain two copies of this field:

  1. The exact string value as typed by the user.
  2. A safe value of the same string.

This is how I create the field:

class MyItem extends FieldItemBase {

  /**
   * {@inheritdoc}
   */
  public static function schema(FieldStorageDefinitionInterface $field_definition) {
    return [
      'columns' => [
        'value' => [
          'type' => 'text',
          'size' => 'tiny',
          'not null' => FALSE,
        ],
        'safevalue' => [
          'type' => 'text',
          'size' => 'tiny',
          'not null' => FALSE,
        ],
      ],
    ];
  }
…

}

I assume I should generate a safevalue of the string in hook_node_presave() and somehow get into the entity there (but corrections to these assumptions are welcome).

I have no problem getting the string value as typed by the user and to compute a safe value:

$value = $entity->field_myfield->getString();
$safevalue = makeSafe($value);

But I do not know how to get the $safevalue stored in the database.

Edit

I see that this has two nearly identical answers. Both work fine if the cardinality of the field is one, so I've upvoted both. Since I cannot accept both, I've accepted the one from apaderno for the extra detail about propertyDefinitions().

I am still not able to get this to work with a multivalue field, but I think that probably need to be a separate question.

Score:2
us flag

If safeValue has been defined as field property in propertyDefinitions(), it can be accessed as $entity->field_myfield->safeValue, either to read it or write it.

In Drupal core, an example of field that stores a processed copy of the field value is the TextItem class, whose propertyDefinitions() method contains the following code.

$properties['value'] = DataDefinition::create('string')
  ->setLabel(t('Text'))
  ->setRequired(TRUE);
$properties['format'] = DataDefinition::create('filter_format')->setLabel(t('Text format'));
$properties['processed'] = DataDefinition::create('string')
  ->setLabel(t('Processed text'))
  ->setDescription(t('The text with the text format applied.'))
  ->setComputed(TRUE)
  ->setClass('\\Drupal\\text\\TextProcessed')
  ->setSetting('text source', 'value')
  ->setInternal(FALSE);

That class defines the property as computed. That's why its schema() method doesn't include that property.

Score:2
cn flag

For saving:

$value = $entity->field_myfield->getString();
$entity->field_myfield->safevalue = makeSafe($value);

For loading:

$safe_value = $entity->field_myfield->safevalue;
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.