You are here

function _field_encrypt_entity_hooks in Field Encryption 3.0.x

Generates PHP code to define necessary entity hooks.

Parameters

string $entity_type_id: (optional) The entity type to generate hooks for. If omitted code will be generated for all entity types with encrypted fields.

Return value

string The PHP code.

3 calls to _field_encrypt_entity_hooks()
DynamicEntityHooksTest::testUnexpectedEntityTypeId in tests/src/Kernel/DynamicEntityHooksTest.php
Tests _field_encrypt_entity_hooks().
field_encrypt_requirements in ./field_encrypt.install
Implements hook_requirements().
_field_encrypt_define_entity_hooks in ./field_encrypt.module
Creates entity hooks for entity types with encrypted fields.

File

./field_encrypt.module, line 362
Contains module hooks for field_encrypt.

Code

function _field_encrypt_entity_hooks($entity_type_id = NULL) {
  $functions = '';
  $entity_types = $entity_type_id ? [
    $entity_type_id,
  ] : \Drupal::state()
    ->get('field_encrypt.entity_types', []);
  foreach ($entity_types as $entity_type_id) {

    // We're going to use eval() to create functions. This check ensures no
    // interesting have got into the entity type ID that would result in running
    // unexpected PHP code. Under normal operation this is not possible but we
    // assume the worst.
    if (preg_match('/[^A-Za-z0-9_]+/', $entity_type_id)) {
      throw new RuntimeException(sprintf('"%s" entity type contains unexpected characters', $entity_type_id));
    }
    if (!function_exists("field_encrypt_{$entity_type_id}_insert")) {
      $functions .= <<<EOF
/**
 * Implements hook_ENTITY_TYPE_insert().
 */
function field_encrypt_{<span class="php-variable">$entity_type_id</span>}_insert(Drupal\\Core\\Entity\\EntityInterface \$entity) {
  \\Drupal::service('field_encrypt.process_entities')->decryptEntity(\$entity);
}
/**
 * Implements hook_ENTITY_TYPE_update().
 */
function field_encrypt_{<span class="php-variable">$entity_type_id</span>}_update(Drupal\\Core\\Entity\\EntityInterface \$entity) {
  \\Drupal::service('field_encrypt.process_entities')->decryptEntity(\$entity);
}
EOF;
    }
  }
  return $functions;
}