Test module for the entity API providing an entity type for testing.
<?php
/**
* @file
* Test module for the entity API providing an entity type for testing.
*/
use Drupal\entity_test\Plugin\Core\Entity\EntityTest;
/**
* Implements hook_entity_info_alter().
*/
function entity_test_entity_info_alter(&$info) {
// Optionally specify a translation handler for testing translations.
if (state()
->get('entity_test.translation')) {
$info['entity_test']['translation']['entity_test'] = TRUE;
}
// Optionally unset the access controller to test the fallback.
if (state()
->get('entity_test.default_access_controller')) {
unset($info['entity_test']['access_controller_class']);
}
}
/**
* 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();
$items['entity-test/add'] = array(
'title' => 'Add an entity_test',
'page callback' => 'entity_test_add',
'access arguments' => array(
'administer entity_test content',
),
'type' => MENU_NORMAL_ITEM,
);
$items['entity-test/manage/%entity_test'] = array(
'title' => 'Edit test entity',
'page callback' => 'entity_test_edit',
'page arguments' => array(
2,
),
'access arguments' => array(
'administer entity_test content',
),
'type' => MENU_NORMAL_ITEM,
);
$items['entity-test/manage/%entity_test/edit'] = array(
'title' => 'Edit',
'type' => MENU_DEFAULT_LOCAL_TASK,
);
return $items;
}
/**
* Menu callback: displays the 'Add new entity_test' form.
*
* @return array
* The processed form for a new entity_test.
*
* @see entity_test_menu()
*/
function entity_test_add() {
drupal_set_title(t('Create an entity_test'));
$entity = entity_create('entity_test', array());
return entity_get_form($entity);
}
/**
* Menu callback: displays the 'Edit existing entity_test' form.
*
* @param array $entity
* The entity to be edited.
*
* @return array
* The processed form for the edited entity_test.
*
* @see entity_test_menu()
*/
function entity_test_edit(EntityTest $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 multiple test entities based on certain conditions.
*
* @param array $ids
* (optional) An array of entity IDs. If omitted, all entities are loaded.
* @param bool $reset
* A boolean indicating that the internal cache should be reset.
*
* @return array
* An array of test entity objects, indexed by ID.
*/
function entity_test_load_multiple(array $ids = NULL, $reset = FALSE) {
return entity_load_multiple('entity_test', $ids, $reset);
}
/**
* Deletes multiple test entities.
*
* @param $ids
* An array of test entity IDs.
*/
function entity_test_delete_multiple(array $ids) {
entity_delete_multiple('entity_test', $ids);
}
/**
* 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);
variable_set('entity_form_langcode', $langcode);
}
Name | Description |
---|---|
entity_test_add | Menu callback: displays the 'Add new entity_test' form. |
entity_test_delete_multiple | Deletes multiple test entities. |
entity_test_edit | Menu callback: displays the 'Edit existing entity_test' form. |
entity_test_entity_info_alter | Implements hook_entity_info_alter(). |
entity_test_form_node_form_alter | Implements hook_form_BASE_FORM_ID_alter(). |
entity_test_load | Loads a test entity. |
entity_test_load_multiple | Loads multiple test entities based on certain conditions. |
entity_test_menu | Implements hook_menu(). |
entity_test_permission | Implements hook_permission(). |