field_test.entity.inc

Same filename in other branches
  1. 7.x modules/field/tests/field_test.entity.inc
  2. 8.9.x core/modules/field/tests/modules/field_test/field_test.entity.inc
  3. 10 core/modules/field/tests/modules/field_test/field_test.entity.inc
  4. 11.x core/modules/field/tests/modules/field_test/field_test.entity.inc

Defines an entity type.

File

core/modules/field/tests/modules/field_test/field_test.entity.inc

View source
<?php


/**
 * @file
 * Defines an entity type.
 */

/**
 * Implements hook_entity_type_alter().
 */
function field_test_entity_type_alter(array &$entity_types) {
    
    /** @var \Drupal\Core\Entity\EntityTypeInterface[] $entity_types */
    foreach (field_test_entity_info_translatable() as $entity_type => $translatable) {
        $entity_types[$entity_type]->set('translatable', $translatable);
    }
}

/**
 * Helper function to enable entity translations.
 */
function field_test_entity_info_translatable($entity_type_id = NULL, $translatable = NULL) {
    static $stored_value = [];
    if (isset($entity_type_id)) {
        $entity_definition_update_manager = \Drupal::entityDefinitionUpdateManager();
        $entity_type = $entity_definition_update_manager->getEntityType($entity_type_id);
        $stored_value[$entity_type_id] = $translatable;
        if ($translatable != $entity_type->isTranslatable()) {
            $entity_definition_update_manager->uninstallEntityType($entity_type);
            $entity_type->set('translatable', $translatable);
            $entity_definition_update_manager->installEntityType($entity_type);
        }
    }
    return $stored_value;
}

Functions

Title Deprecated Summary
field_test_entity_info_translatable Helper function to enable entity translations.
field_test_entity_type_alter Implements hook_entity_type_alter().

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.