You are here

field_encrypt_test.module in Field Encryption 3.0.x

Contains module hooks for field_encrypt_test.

File

tests/modules/field_encrypt_test/field_encrypt_test.module
View source
<?php

/**
 * @file
 * Contains module hooks for field_encrypt_test.
 */
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\node\NodeInterface;

/**
 * Implements hook_entity_update().
 */
function field_encrypt_test_entity_update(EntityInterface $entity) {
  if ($entity instanceof NodeInterface) {
    \Drupal::messenger()
      ->addMessage(t('Field encrypt test hook_entity_update: @label', [
      '@label' => $entity
        ->label(),
    ]));
  }
}

/**
 * Implements hook_ENTITY_TYPE_update().
 */
function field_encrypt_test_node_update(EntityInterface $entity) {
  \Drupal::messenger()
    ->addMessage(t('Field encrypt test hook_ENTITY_TYPE_update: @label', [
    '@label' => $entity
      ->label(),
  ]));
}

/**
 * Implements hook_entity_insert().
 */
function field_encrypt_test_entity_insert(EntityInterface $entity) {
  if ($entity instanceof NodeInterface) {
    \Drupal::messenger()
      ->addMessage(t('Field encrypt test hook_entity_insert: @label', [
      '@label' => $entity
        ->label(),
    ]));
  }
}

/**
 * Implements hook_ENTITY_TYPE_insert().
 */
function field_encrypt_test_node_insert(EntityInterface $entity) {
  \Drupal::messenger()
    ->addMessage(t('Field encrypt test hook_ENTITY_TYPE_insert: @label', [
    '@label' => $entity
      ->label(),
  ]));
}

/**
 * Implements hook_entity_delete().
 */
function field_encrypt_test_entity_delete(EntityInterface $entity) {
  if ($entity instanceof NodeInterface) {
    \Drupal::messenger()
      ->addMessage(t('Field encrypt test hook_entity_delete: @label', [
      '@label' => $entity
        ->label(),
    ]));
  }
}

/**
 * Implements hook_ENTITY_TYPE_delete().
 */
function field_encrypt_test_node_delete(EntityInterface $entity) {
  \Drupal::messenger()
    ->addMessage(t('Field encrypt test hook_ENTITY_TYPE_delete: @label', [
    '@label' => $entity
      ->label(),
  ]));
}

/**
 * Implements hook_entity_base_field_info().
 */
function field_encrypt_test_entity_base_field_info(EntityTypeInterface $entity_type) {
  $fields = [];
  if ($entity_type
    ->id() === 'node' && \Drupal::state()
    ->get('field_encrypt.create_base_field', FALSE)) {
    $fields['field_encrypt_test_base_field'] = BaseFieldDefinition::create('string')
      ->setLabel('Field Encrypt test base field');
  }
  return $fields;
}

/**
 * Implements hook_entity_base_field_info_alter().
 */
function field_encrypt_test_entity_base_field_info_alter(&$fields, EntityTypeInterface $entity_type) {
  if ($entity_type
    ->id() === 'node' && \Drupal::state()
    ->get('field_encrypt_test.hook_field_encrypt_unencrypted_storage_value_alter', FALSE)) {

    /** @var BaseFieldDefinition[] $fields */
    $fields['title']
      ->setSetting('field_encrypt.placeholders', [
      'value' => '๐Ÿ’',
    ]);
  }
}

/**
 * Implements hook_field_encrypt_unencrypted_storage_value_alter().
 */
function field_encrypt_test_field_encrypt_unencrypted_storage_value_alter(&$unencrypted_storage_value, array $context) {
  if (\Drupal::state()
    ->get('field_encrypt_test.hook_field_encrypt_unencrypted_storage_value_alter', FALSE)) {
    $entity = $context['entity'];
    $field = $context['field'];
    $property = $context['property'];
    \Drupal::messenger()
      ->addMessage("Value alter hook: Entity title: {$entity->label()}");
    \Drupal::messenger()
      ->addMessage("Value alter hook: Field name: {$field->getName()}");
    \Drupal::messenger()
      ->addMessage("Value alter hook: Property: {$property}");
    $unencrypted_storage_value = '๐Ÿ’';
  }
}

/**
 * Implements hook_field_encrypt_allow_encryption().
 */
function field_encrypt_test_field_encrypt_allow_encryption(ContentEntityInterface $entity) {
  if (\Drupal::state()
    ->get('field_encrypt_test.hook_field_encrypt_allow_encryption', FALSE)) {
    \Drupal::messenger()
      ->addMessage("Allow encryption hook: Entity title: {$entity->label()}");

    // Only encrypt fields on unpublished nodes.
    if ($entity
      ->getEntityTypeId() === 'node') {
      if ($entity
        ->isPublished()) {
        return FALSE;
      }
    }
  }
}