entity_test.module

Same filename in other branches
  1. 9 core/modules/system/tests/modules/entity_test/entity_test.module
  2. 8.9.x core/modules/system/tests/modules/entity_test/entity_test.module
  3. 10 core/modules/system/tests/modules/entity_test/entity_test.module

File

core/modules/system/tests/modules/entity_test/entity_test.module

View source
<?php


/**
 * @file
 * Test module for the entity API providing several entity types for testing.
 */
declare (strict_types=1);
use Drupal\Core\Entity\FieldableEntityInterface;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\entity_test\EntityTestHelper;

/**
 * Creates a new bundle for entity_test entities.
 *
 * @param string $bundle
 *   The machine-readable name of the bundle.
 * @param string $text
 *   (optional) The human-readable name of the bundle. If none is provided, the
 *   machine name will be used.
 * @param string $entity_type
 *   (optional) The entity type for which the bundle is created. Defaults to
 *   'entity_test'.
 *
 * @deprecated in drupal:11.2.0 and is removed from drupal:12.0.0. Use
 *   \Drupal\entity_test\EntityTestHelper::createBundle() instead.
 *
 * @see https://www.drupal.org/node/3497049
 */
function entity_test_create_bundle($bundle, $text = NULL, $entity_type = 'entity_test') {
    @trigger_error(__FUNCTION__ . '() is deprecated in drupal:11.2.0 and is removed from drupal:12.0.0. Use \\Drupal\\entity_test\\EntityTestHelper::createBundle() instead. See https://www.drupal.org/node/3497049', E_USER_DEPRECATED);
    EntityTestHelper::createBundle($bundle, $text, $entity_type);
}

/**
 * Deletes a bundle for entity_test entities.
 *
 * @param string $bundle
 *   The machine-readable name of the bundle to delete.
 * @param string $entity_type
 *   (optional) The entity type for which the bundle is deleted. Defaults to
 *   'entity_test'.
 *
 * @deprecated in drupal:11.2.0 and is removed from drupal:12.0.0. Use
 *    \Drupal\entity_test\EntityTestHelper::deleteBundle() instead.
 *
 * @see https://www.drupal.org/node/3497049
 */
function entity_test_delete_bundle($bundle, $entity_type = 'entity_test') {
    @trigger_error(__FUNCTION__ . '() is deprecated in drupal:11.2.0 and is removed from drupal:12.0.0. Use \\Drupal\\entity_test\\EntityTestHelper::deleteBundle() instead. See https://www.drupal.org/node/3497049', E_USER_DEPRECATED);
    EntityTestHelper::deleteBundle($bundle, $entity_type);
}

/**
 * Validation handler for the entity_test entity form.
 */
function entity_test_form_entity_test_form_validate(array &$form, FormStateInterface $form_state) {
    $form['#entity_test_form_validate'] = TRUE;
}

/**
 * Validation handler for the entity_test entity form.
 */
function entity_test_form_entity_test_form_validate_check(array &$form, FormStateInterface $form_state) {
    if (!empty($form['#entity_test_form_validate'])) {
        \Drupal::state()->set('entity_test.form.validate.result', TRUE);
    }
}

/**
 * Field default value callback.
 *
 * @param \Drupal\Core\Entity\FieldableEntityInterface $entity
 *   The entity being created.
 * @param \Drupal\Core\Field\FieldDefinitionInterface $definition
 *   The field definition.
 *
 * @return array
 *   An array of default values, in the same format as the $default_value
 *   property.
 *
 * @see \Drupal\field\Entity\FieldConfig::$default_value
 */
function entity_test_field_default_value(FieldableEntityInterface $entity, FieldDefinitionInterface $definition) {
    // Include the field name and entity language in the generated values to check
    // that they are correctly passed.
    $string = $definition->getName() . '_' . $entity->language()
        ->getId();
    // Return a "default value" with multiple items.
    return [
        [
            'shape' => "shape:0:{$string}",
            'color' => "color:0:{$string}",
        ],
        [
            'shape' => "shape:1:{$string}",
            'color' => "color:1:{$string}",
        ],
    ];
}

/**
 * Helper function to be used to record hook invocations.
 *
 * @param string $hook
 *   The hook name.
 * @param mixed $data
 *   Arbitrary data associated with the hook invocation.
 */
function _entity_test_record_hooks($hook, $data) {
    $state = \Drupal::state();
    $key = 'entity_test.hooks';
    $hooks = $state->get($key);
    $hooks[$hook] = $data;
    $state->set($key, $hooks);
}

Functions

Title Deprecated Summary
entity_test_create_bundle

in drupal:11.2.0 and is removed from drupal:12.0.0. Use \Drupal\entity_test\EntityTestHelper::createBundle() instead.

Creates a new bundle for entity_test entities.
entity_test_delete_bundle

in drupal:11.2.0 and is removed from drupal:12.0.0. Use \Drupal\entity_test\EntityTestHelper::deleteBundle() instead.

Deletes a bundle for entity_test entities.
entity_test_field_default_value Field default value callback.
entity_test_form_entity_test_form_validate Validation handler for the entity_test entity form.
entity_test_form_entity_test_form_validate_check Validation handler for the entity_test entity form.
_entity_test_record_hooks Helper function to be used to record hook invocations.

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