You are here

entity_test_update.module in Drupal 8

Provides an entity type for testing definition and schema updates.

File

core/modules/system/tests/modules/entity_test_update/entity_test_update.module
View source
<?php

/**
 * @file
 * Provides an entity type for testing definition and schema updates.
 */
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\entity_test_update\Entity\EntityTestUpdate;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityTypeInterface;

/**
 * Implements hook_entity_base_field_info().
 */
function entity_test_update_entity_base_field_info(EntityTypeInterface $entity_type) {

  // Add a base field that will be used to test that fields added through
  // hook_entity_base_field_info() are handled correctly during a schema
  // conversion (e.g. from non-revisionable to revisionable).
  if ($entity_type
    ->id() == 'entity_test_update') {
    $fields = [];
    $fields['test_entity_base_field_info'] = BaseFieldDefinition::create('string')
      ->setLabel(new TranslatableMarkup('Field added by hook_entity_base_field_info()'))
      ->setTranslatable(TRUE)
      ->setRevisionable(TRUE);
    return $fields;
  }
}

/**
 * Implements hook_entity_field_storage_info().
 */
function entity_test_update_entity_field_storage_info(EntityTypeInterface $entity_type) {
  if ($entity_type
    ->id() == 'entity_test_update') {
    return \Drupal::state()
      ->get('entity_test_update.additional_field_storage_definitions', []);
  }
}

/**
 * Implements hook_entity_type_alter().
 */
function entity_test_update_entity_type_alter(array &$entity_types) {

  // Allow entity_test_update tests to override the entity type definition.
  $entity_type = \Drupal::state()
    ->get('entity_test_update.entity_type', $entity_types['entity_test_update']);
  if ($entity_type !== 'null') {
    $entity_types['entity_test_update'] = $entity_type;
  }
  else {
    unset($entity_types['entity_test_update']);
  }
}

/**
 * Implements hook_ENTITY_TYPE_presave() for the 'view' entity type.
 */
function entity_test_update_view_presave(EntityInterface $entity) {
  if (\Drupal::state()
    ->get('entity_test_update.throw_view_exception') === $entity
    ->id()) {
    throw new \LogicException('The view could not be saved.');
  }
}

/**
 * Creates a given number of 'entity_test_update' entities.
 *
 * The 'drupal-8.0.0-rc1-filled.standard.entity_test_update.php.gz' db dump was
 * created with the following characteristics:
 * - Drupal 8.0.0-rc1 installed with the Standard profile;
 * - The 'entity_test_update' module enabled;
 * - 102 test entities created and saved.
 *
 * The 'drupal-8.0.0-rc1-filled.standard.entity_test_update_mul.php.gz' db dump
 * was created with the following characteristics:
 * - Drupal 8.0.0-rc1 installed with the Standard profile;
 * - Manually edited the annotation of the entity_test_update entity type in
 *   order to make it translatable;
 * - The entity_test_update and Language module enabled;
 * - Romanian language added;
 * - 50 test entities created, translated and saved;
 * - The Content Translation module enabled and configured for our test entity
 *   type. This was enabled after the first 50 entities were created in order
 *   to have NULL values for its translation metadata fields
 *   (e.g. content_translation_status);
 * - 52 more test entities (with the IDs 51 - 102) crated, translated and saved.
 *
 * The 'drupal-8.0.0-rc1-filled.standard.entity_test_update_mul_rev.php.gz' db
 * dump was created like the multilingual one described above, with one change:
 * The annotation of the entity_test_update entity type was also manually edited
 * in order to make it revisionable.
 *
 * @param int $start
 *   (optional) The entity ID to start from. Defaults to 1.
 * @param int $end
 *   (optional) The entity ID to end with. Defaults to 50.
 * @param bool $add_translation
 *   (optional) Whether to create a translation for each entity. If the number
 *   of entities is greater than 50, the default translation (en) of the 51st
 *   entity and the 52nd translation (ro) are disabled through the
 *   'content_moderation_status' field. Defaults to FALSE.
 *
 * @see drupal-8.0.0-rc1-filled.standard.entity_test_update.php.gz
 * @see drupal-8.0.0-rc1-filled.standard.entity_test_update_mul.php.gz
 */
function _entity_test_update_create_test_entities($start = 1, $end = 50, $add_translation = FALSE) {
  for ($i = $start; $i <= $end; $i++) {
    $entity = EntityTestUpdate::create([
      'id' => $i,
      'name' => $i,
      'test_single_property' => $i . ' - test single property',
      'test_multiple_properties' => [
        [
          'value1' => $i . ' - test multiple properties - value1',
          'value2' => $i . ' - test multiple properties - value2',
        ],
      ],
      'test_single_property_multiple_values' => [
        [
          'value' => $i . ' - test single property multiple values 0',
        ],
        [
          'value' => $i . ' - test single property multiple values 1',
        ],
      ],
      'test_multiple_properties_multiple_values' => [
        [
          'value1' => $i . ' - test multiple properties multiple values - value1 0',
          'value2' => $i . ' - test multiple properties multiple values - value2 0',
        ],
        [
          'value1' => $i . ' - test multiple properties multiple values - value1 1',
          'value2' => $i . ' - test multiple properties multiple values - value2 1',
        ],
      ],
      'test_non_rev_field' => $i . ' - test non-revisionable field',
      'test_non_mul_field' => $i . ' - test non-translatable field',
      'test_non_mulrev_field' => $i . ' - test non-translatable and non-revisionable field',
      'field_test_configurable_field' => [
        [
          'value1' => $i . ' - field test configurable field - value1 0',
          'value2' => $i . ' - field test configurable field - value2 0',
        ],
        [
          'value1' => $i . ' - field test configurable field - value1 1',
          'value2' => $i . ' - field test configurable field - value2 1',
        ],
      ],
      'test_entity_base_field_info' => $i . ' - test entity base field info',
    ]);
    if ($add_translation) {
      $entity
        ->addTranslation('ro', [
        'name' => $i . ' - ro',
        'test_single_property' => $i . ' - test single property - ro',
        'test_multiple_properties' => [
          [
            'value1' => $i . ' - test multiple properties - value1 - ro',
            'value2' => $i . ' - test multiple properties - value2 - ro',
          ],
        ],
        'test_single_property_multiple_values' => [
          [
            'value' => $i . ' - test single property multiple values 0 - ro',
          ],
          [
            'value' => $i . ' - test single property multiple values 1 - ro',
          ],
        ],
        'test_multiple_properties_multiple_values' => [
          [
            'value1' => $i . ' - test multiple properties multiple values - value1 0 - ro',
            'value2' => $i . ' - test multiple properties multiple values - value2 0 - ro',
          ],
          [
            'value1' => $i . ' - test multiple properties multiple values - value1 1 - ro',
            'value2' => $i . ' - test multiple properties multiple values - value2 1 - ro',
          ],
        ],
        'test_non_rev_field' => $i . ' - test non-revisionable field - ro',
        'field_test_configurable_field' => [
          [
            'value1' => $i . ' - field test configurable field - value1 0 - ro',
            'value2' => $i . ' - field test configurable field - value2 0 - ro',
          ],
          [
            'value1' => $i . ' - field test configurable field - value1 1 - ro',
            'value2' => $i . ' - field test configurable field - value2 1 - ro',
          ],
        ],
        'test_entity_base_field_info' => $i . ' - test entity base field info - ro',
      ]);
      if ($i == 101) {
        $entity
          ->getTranslation('en')
          ->set('content_translation_status', FALSE);
      }
      if ($i == 102) {
        $entity
          ->getTranslation('ro')
          ->set('content_translation_status', FALSE);
      }
    }
    $entity
      ->save();
  }
}