View source
<?php
function entity_test_entity_info() {
$return = array(
'entity_test' => array(
'label' => t('Test Entity'),
'plural label' => t('Test Entities'),
'description' => t('An entity type used by the entity API tests.'),
'entity class' => 'EntityClass',
'controller class' => 'EntityAPIController',
'base table' => 'entity_test',
'fieldable' => TRUE,
'entity keys' => array(
'id' => 'pid',
'bundle' => 'name',
),
'label callback' => 'entity_class_label',
'uri callback' => 'entity_class_uri',
'bundles' => array(),
'bundle keys' => array(
'bundle' => 'name',
),
'module' => 'entity_test',
),
'entity_test_type' => array(
'label' => t('Test entity type'),
'entity class' => 'Entity',
'controller class' => 'EntityAPIControllerExportable',
'base table' => 'entity_test_type',
'fieldable' => FALSE,
'bundle of' => 'entity_test',
'exportable' => TRUE,
'entity keys' => array(
'id' => 'id',
'name' => 'name',
),
'module' => 'entity_test',
),
'entity_test2' => array(
'label' => t('Test Entity (revision support)'),
'entity class' => 'EntityClassRevision',
'controller class' => 'EntityAPIController',
'base table' => 'entity_test2',
'revision table' => 'entity_test2_revision',
'fieldable' => TRUE,
'entity keys' => array(
'id' => 'pid',
'revision' => 'revision_id',
),
'label callback' => 'entity_class_label',
'uri callback' => 'entity_class_uri',
'bundles' => array(),
'bundle keys' => array(
'bundle' => 'name',
),
),
);
$types = db_select('entity_test_type', 'et')
->fields('et')
->execute()
->fetchAllAssoc('name');
foreach ($types as $name => $type) {
$return['entity_test']['bundles'][$name] = array(
'label' => $type->label,
);
}
if (module_exists('entitycache')) {
$return['entity_test']['field cache'] = FALSE;
$return['entity_test']['entity cache'] = TRUE;
}
return $return;
}
function entity_test_get_types($name = NULL) {
$types = entity_load_multiple_by_name('entity_test_type', isset($name) ? array(
$name,
) : FALSE);
return isset($name) ? reset($types) : $types;
}
function entity_test_load_multiple($pids = array(), $conditions = array(), $reset = FALSE) {
return entity_load('entity_test', $pids, $conditions, $reset);
}
function entity_test_delete_multiple(array $pids) {
entity_get_controller('entity_test')
->delete($pids);
}
class EntityClass extends Entity {
public function __construct(array $values = array(), $entityType = NULL) {
parent::__construct($values, 'entity_test');
}
public function buildContent($view_mode = 'full', $langcode = NULL) {
$content['user'] = array(
'#markup' => "User: " . format_username(user_load($this->uid)),
);
return entity_get_controller($this->entityType)
->buildContent($this, $view_mode, $langcode, $content);
}
protected function defaultLabel() {
$type = entity_test_get_types($this->name);
return $type->label;
}
protected function defaultURI() {
return array(
'path' => 'custom/' . $this
->identifier(),
);
}
}
class EntityClassRevision extends EntityClass {
public function __construct(array $values = array(), $entityType = NULL) {
Entity::__construct($values, 'entity_test2');
}
}
function entity_test_entity_insert($entity, $entity_type) {
if ($entity_type == 'entity_test_type') {
$_SESSION['entity_hook_test']['entity_insert'][] = entity_id($entity_type, $entity);
}
}
function entity_test_entity_update($entity, $entity_type) {
$_SESSION['entity_hook_test']['entity_update'][] = entity_id($entity_type, $entity);
}
function entity_test_entity_delete($entity, $entity_type) {
if ($entity_type == 'entity_test_type') {
$_SESSION['entity_hook_test']['entity_delete'][] = entity_id($entity_type, $entity);
}
}
function entity_test_entity_test_type_insert($entity) {
$_SESSION['entity_hook_test']['entity_test_type_insert'][] = $entity
->identifier();
}
function entity_test_entity_test_type_update($entity) {
$_SESSION['entity_hook_test']['entity_test_type_update'][] = $entity
->identifier();
if (!empty($entity->original) && $entity->original->label == 'test_changes') {
if ($entity->original->label != $entity->label) {
$entity->label .= '_update';
}
}
}
function entity_test_entity_test_type_delete($entity) {
$_SESSION['entity_hook_test']['entity_test_type_delete'][] = $entity
->identifier();
}
function entity_test_entity_test_type_presave($entity) {
if (!empty($entity->original) && $entity->original->label == 'test_changes') {
if ($entity->original->label != $entity->label) {
$entity->label .= '_presave';
}
}
}
function entity_test_entity_property_info_alter(&$info) {
$info['node']['properties']['reference'] = array(
'label' => t('Test reference'),
'description' => t('A generic entity reference.'),
'getter callback' => 'entity_test_entity_getter',
'setter callback' => 'entity_test_entity_setter',
'type' => 'entity',
);
}
function entity_test_entity_getter($node) {
if (empty($node->entity)) {
$node->entity = array(
'type' => 'user',
'id' => $node->uid,
);
}
if ($node->entity['type'] === 'user' && empty($node->entity['id'])) {
return entity_metadata_wrapper('user', drupal_anonymous_user());
}
else {
return entity_metadata_wrapper($node->entity['type'], $node->entity['id']);
}
}
function entity_test_entity_setter($node, $property_name, $wrapper) {
$node->entity = array(
'type' => $wrapper
->type(),
'id' => $wrapper
->getIdentifier(),
);
}