Newer
Older
<?php
/**
* @file
* Test module for the entity API providing an entity type for testing.
*/
Angie Byron
committed
use Drupal\entity_test\Plugin\Core\Entity\EntityTest;
/**
Angie Byron
committed
* Implements hook_entity_info_alter().
*/
Angie Byron
committed
function entity_test_entity_info_alter(&$info) {
// Optionally specify a translation handler for testing translations.
if (state()->get('entity_test.translation')) {
Angie Byron
committed
$info['entity_test']['translation']['entity_test'] = 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;
}
Angie Byron
committed
/**
* 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'),
),
Angie Byron
committed
'view test entity' => array(
'title' => t('View test entities'),
),
'view test entity translations' => array(
'title' => t('View translations of test entities'),
),
Angie Byron
committed
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
);
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.
Angie Byron
committed
*
* @return array
* The processed form for a new entity_test.
*
* @see entity_test_menu()
Angie Byron
committed
*/
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.
Angie Byron
committed
*
* @param array $entity
* The entity to be edited.
*
* @return array
* The processed form for the edited entity_test.
*
* @see entity_test_menu()
Angie Byron
committed
*/
Angie Byron
committed
function entity_test_edit(EntityTest $entity) {
drupal_set_title($entity->label(), PASS_THROUGH);
Angie Byron
committed
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.
*
Angie Byron
committed
* @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);
}