Test module for the entity API providing several entity types for testing.
<?php
/**
* @file
* Test module for the entity API providing several entity types for testing.
*/
use Drupal\Core\Entity\EntityInterface;
use Drupal\entity\Plugin\Core\Entity\EntityFormDisplay;
/**
* Filter that limits test entity list to revisable ones.
*/
const ENTITY_TEST_TYPES_REVISABLE = 1;
/**
* Filter that limits test entity list to multilingual ones.
*/
const ENTITY_TEST_TYPES_MULTILINGUAL = 2;
/**
* Returns a list of test entity types.
*
* The returned entity types are one for each available entity storage type:
* - The plain entity_test type supports neither revisions nor multilingual
* properties.
* - The entity_test_mul type supports multilingual properties.
* - The entity_test_rev type supports revisions.
* - The entity_test_mulrev type supports both revisions and multilingual
* properties.
*
* @param int $filter
* Either ENTITY_TEST_TYPES_REVISABLE to only return revisable entity types or
* ENTITY_TEST_TYPES_MULTILINGUAL to only return multilingual ones. Defaults
* to NULL, which returns all.
*
* @return array
* List with entity_types.
*/
function entity_test_entity_types($filter = NULL) {
$types = array();
if ($filter == NULL) {
$types[] = 'entity_test';
}
if ($filter != ENTITY_TEST_TYPES_REVISABLE) {
$types[] = 'entity_test_mul';
}
if ($filter != ENTITY_TEST_TYPES_MULTILINGUAL) {
$types[] = 'entity_test_rev';
}
$types[] = 'entity_test_mulrev';
return drupal_map_assoc($types);
}
/**
* Implements hook_entity_info_alter().
*/
function entity_test_entity_info_alter(&$info) {
// Optionally specify a translation handler for testing translations.
if (Drupal::state()
->get('entity_test.translation')) {
foreach (entity_test_entity_types() as $entity_type) {
$info[$entity_type]['translation'][$entity_type] = TRUE;
}
}
}
/**
* 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'.
*/
function entity_test_create_bundle($bundle, $text = NULL, $entity_type = 'entity_test') {
$bundles = Drupal::state()
->get($entity_type . '.bundles') ?: array(
'entity_test' => array(
'label' => 'Entity Test Bundle',
),
);
$bundles += array(
$bundle => array(
'label' => $text ? $text : $bundle,
),
);
Drupal::state()
->set($entity_type . '.bundles', $bundles);
entity_invoke_bundle_hook('create', $entity_type, $bundle);
}
/**
* Renames a bundle for entity_test entities.
*
* @param string $bundle_old
* The machine-readable name of the bundle to rename.
* @param string $bundle_new
* The new machine-readable name of the bundle
* @param string $entity_type
* (optional) The entity type for which the bundle is renamed. Defaults to
* 'entity_test'.
*/
function entity_test_rename_bundle($bundle_old, $bundle_new, $entity_type = 'entity_test') {
$bundles = Drupal::state()
->get($entity_type . '.bundles') ?: array(
'entity_test' => array(
'label' => 'Entity Test Bundle',
),
);
$bundles[$bundle_new] = $bundles[$bundle_old];
unset($bundles[$bundle_old]);
Drupal::state()
->set($entity_type . '.bundles', $bundles);
entity_invoke_bundle_hook('rename', $entity_type, $bundle_old, $bundle_new);
}
/**
* 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'.
*/
function entity_test_delete_bundle($bundle, $entity_type = 'entity_test') {
$bundles = Drupal::state()
->get($entity_type . '.bundles') ?: array(
'entity_test' => array(
'label' => 'Entity Test Bundle',
),
);
unset($bundles[$bundle]);
Drupal::state()
->set($entity_type . '.bundles', $bundles);
entity_invoke_bundle_hook('delete', $entity_type, $bundle);
}
/**
* Implements hook_entity_bundle_info_alter().
*/
function entity_test_entity_bundle_info_alter(&$bundles) {
$entity_info = entity_get_info();
foreach ($bundles as $entity_type => $info) {
if ($entity_info[$entity_type]['module'] == 'entity_test') {
$bundles[$entity_type] = Drupal::state()
->get($entity_type . '.bundles') ?: array(
$entity_type => array(
'label' => 'Entity Test Bundle',
),
);
}
}
}
/**
* Implements hook_entity_view_mode_info_alter().
*/
function entity_test_entity_view_mode_info_alter(&$view_modes) {
$entity_info = entity_get_info();
foreach ($entity_info as $entity_type => $info) {
if ($entity_info[$entity_type]['module'] == 'entity_test') {
$view_modes[$entity_type] = array(
'full' => array(
'label' => t('Full object'),
'status' => TRUE,
),
'teaser' => array(
'label' => t('Teaser'),
'status' => TRUE,
),
);
}
}
}
/**
* Implements hook_field_extra_fields().
*/
function entity_test_field_extra_fields() {
$extra['entity_test']['entity_test'] = array(
'display' => array(
// Note: those extra fields do not currently display anything, they are
// just used in \Drupal\entity\Tests\EntityDisplayTest to test the
// behavior of entity display objects,
'display_extra_field' => array(
'label' => t('Display extra field'),
'description' => t('An extra field on the display side.'),
'weight' => 5,
'visible' => TRUE,
),
'display_extra_field_hidden' => array(
'label' => t('Display extra field (hidden)'),
'description' => t('An extra field on the display side, hidden by default.'),
'visible' => FALSE,
),
),
);
return $extra;
}
/**
* Implements hook_permission().
*/
function entity_test_permission() {
$permissions = array(
'administer entity_test content' => array(
'title' => t('Administer entity_test content'),
'description' => t('Manage entity_test content'),
),
'view test entity' => array(
'title' => t('View test entities'),
),
'view test entity translations' => array(
'title' => t('View translations of test entities'),
),
);
return $permissions;
}
/**
* Implements hook_menu().
*/
function entity_test_menu() {
$items = array();
foreach (entity_test_entity_types() as $entity_type) {
$items[$entity_type . '/add'] = array(
'title' => 'Add an @type',
'title arguments' => array(
'@type' => $entity_type,
),
'page callback' => 'entity_test_add',
'page arguments' => array(
$entity_type,
),
'access arguments' => array(
'administer entity_test content',
),
'type' => MENU_NORMAL_ITEM,
);
$items[$entity_type . '/manage/%' . $entity_type] = array(
'title' => 'Edit @type',
'title arguments' => array(
'@type' => $entity_type,
),
'page callback' => 'entity_test_edit',
'page arguments' => array(
2,
),
'access arguments' => array(
'administer entity_test content',
),
'type' => MENU_NORMAL_ITEM,
);
$items[$entity_type . '/manage/%' . $entity_type . '/edit'] = array(
'title' => 'Edit',
'type' => MENU_DEFAULT_LOCAL_TASK,
);
}
return $items;
}
/**
* Implements hook_form_BASE_FORM_ID_alter().
*/
function entity_test_form_node_form_alter(&$form, &$form_state, $form_id) {
$langcode = $form_state['controller']
->getFormLangcode($form_state);
Drupal::state()
->set('entity_test.form_langcode', $langcode);
}
/**
* Menu callback: displays the 'Add new entity_test' form.
*
* @param string $entity_type
* Name of the entity type for which a create form should be displayed.
*
* @return array
* The processed form for a new entity_test.
*
* @see entity_test_menu()
*/
function entity_test_add($entity_type) {
drupal_set_title(t('Create an @type', array(
'@type' => $entity_type,
)));
$entity = entity_create($entity_type, array());
return entity_get_form($entity);
}
/**
* Menu callback: displays the 'Edit existing entity_test' form.
*
* @param \Drupal\Core\Entity\EntityInterface $entity
* The entity to be edited.
*
* @return array
* The processed form for the edited entity.
*
* @see entity_test_menu()
*/
function entity_test_edit(EntityInterface $entity) {
drupal_set_title($entity
->label(), PASS_THROUGH);
return entity_get_form($entity);
}
/**
* Loads a test entity.
*
* @param int $id
* A test entity ID.
* @param bool $reset
* A boolean indicating that the internal cache should be reset.
*
* @return \Drupal\entity_test\Plugin\Core\Entity\EntityTest
* The loaded entity object, or FALSE if the entity cannot be loaded.
*/
function entity_test_load($id, $reset = FALSE) {
return entity_load('entity_test', $id, $reset);
}
/**
* Loads a test entity.
*
* @param int $id
* A test entity ID.
* @param bool $reset
* A boolean indicating that the internal cache should be reset.
*
* @return \Drupal\entity_test\Plugin\Core\Entity\EntityTestRev
* The loaded entity object, or FALSE if the entity cannot be loaded.
*/
function entity_test_rev_load($id, $reset = FALSE) {
return entity_load('entity_test_rev', $id, $reset);
}
/**
* Loads a test entity.
*
* @param int $id
* A test entity ID.
* @param bool $reset
* A boolean indicating that the internal cache should be reset.
*
* @return \Drupal\entity_test\Plugin\Core\Entity\EntityTestMul
* The loaded entity object, or FALSE if the entity cannot be loaded.
*/
function entity_test_mul_load($id, $reset = FALSE) {
return entity_load('entity_test_mul', $id, $reset);
}
/**
* Loads a test entity.
*
* @param int $id
* A test entity ID.
* @param bool $reset
* A boolean indicating that the internal cache should be reset.
*
* @return \Drupal\entity_test\Plugin\Core\Entity\EntityTestMulRev
* The loaded entity object, or FALSE if the entity cannot be loaded.
*/
function entity_test_mulrev_load($id, $reset = FALSE) {
return entity_load('entity_test_mulrev', $id, $reset);
}
/**
* Implements hook_ENTITY_TYPE_insert().
*/
function entity_test_entity_test_insert($entity) {
if ($entity->name->value == 'fail_insert') {
throw new Exception("Test exception rollback.");
}
}
/**
* Entity label callback.
*
* @param $entity_type
* The entity type.
* @param $entity
* The entity object.
* @param $langcocde
* (optional) The langcode.
*
* @return
* The label of the entity prefixed with "label callback".
*/
function entity_test_label_callback($entity_type, $entity, $langcode = NULL) {
return 'label callback ' . $entity->name->value;
}
/**
* Implements hook_entity_field_access().
*
* @see \Drupal\system\Tests\Entity\FieldAccessTest::testFieldAccess()
*/
function entity_test_entity_field_access($operation, $field, $account) {
if ($field
->getName() == 'field_test_text') {
if ($field->value == 'no access value') {
return FALSE;
}
elseif ($operation == 'delete' && $field->value == 'no delete access value') {
return FALSE;
}
}
}
/**
* Implements hook_entity_field_access_alter().
*
* @see \Drupal\system\Tests\Entity\FieldAccessTest::testFieldAccess()
*/
function entity_test_entity_field_access_alter(array &$grants, array $context) {
$field = $context['field'];
if ($field
->getName() == 'field_test_text' && $field->value == 'access alter value') {
$grants[':default'] = FALSE;
}
}
/**
* Implements hook_entity_form_display_alter().
*/
function entity_test_entity_form_display_alter(EntityFormDisplay $form_display, $context) {
// Make the field_test_text field 42 characters for entity_test_mul.
if ($context['entity_type'] == 'entity_test') {
if ($component_options = $form_display
->getComponent('field_test_text')) {
$component_options['settings']['size'] = 42;
$form_display
->setComponent('field_test_text', $component_options);
}
}
}
/**
* Implements hook_entity_presave()
*/
function entity_test_entity_presave(EntityInterface $entity) {
if (isset($GLOBALS['entity_test_throw_exception'])) {
throw new Exception('Entity presave exception', 1);
}
}
/**
* Implements hook_entity_predelete()
*/
function entity_test_entity_predelete(EntityInterface $entity) {
if (isset($GLOBALS['entity_test_throw_exception'])) {
throw new Exception('Entity predelete exception', 2);
}
}
/**
* Implements hook_entity_operation_alter().
*/
function entity_test_entity_operation_alter(array &$operations, EntityInterface $entity) {
$uri = $entity
->uri();
$operations['test_operation'] = array(
'title' => format_string('Test Operation: @label', array(
'@label' => $entity
->label(),
)),
'href' => $uri['path'] . '/test_operation',
'weight' => 50,
);
}
Name | Description |
---|---|
ENTITY_TEST_TYPES_MULTILINGUAL | Filter that limits test entity list to multilingual ones. |
ENTITY_TEST_TYPES_REVISABLE | Filter that limits test entity list to revisable ones. |