field_test.entity.inc 1.12 KB
Newer Older
1 2 3 4 5 6 7 8
<?php

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

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

18 19 20
/**
 * Helper function to enable entity translations.
 */
21
function field_test_entity_info_translatable($entity_type_id = NULL, $translatable = NULL) {
22
  $stored_value = &drupal_static(__FUNCTION__, []);
23
  if (isset($entity_type_id)) {
24 25
    $entity_definition_update_manager = \Drupal::entityDefinitionUpdateManager();
    $entity_type = $entity_definition_update_manager->getEntityType($entity_type_id);
26
    $stored_value[$entity_type_id] = $translatable;
27 28 29 30
    if ($translatable != $entity_type->isTranslatable()) {
      $entity_definition_update_manager->uninstallEntityType($entity_type);
      $entity_type->set('translatable', $translatable);
      $entity_definition_update_manager->installEntityType($entity_type);
31
    }
32 33 34
  }
  return $stored_value;
}