forked from kraftwagen/kw-itemnames
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkw_itemnames_entity.kw_itemnames.inc
76 lines (59 loc) · 2.26 KB
/
kw_itemnames_entity.kw_itemnames.inc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
<?php
/**
* Implements hook_kw_itemnames_type_info().
*/
function kw_itemnames_entity_kw_itemnames_type_info() {
$result = array();
foreach(entity_get_info() as $entity_type => $entity_info) {
if (isset($entity_info['kw_itemnames_entity_disabled']) && empty($entity_info['kw_itemnames_entity_disabled'])) {
continue;
}
$result[$entity_type] = array(
'item load callback' => 'kw_itemnames_entity_item_load',
'item load arguments' => array($entity_type, '%item_id'),
'item create callback' => 'kw_itemnames_entity_item_create',
'item create arguments' => array($entity_type, '%defaults', '%required'),
'item update callback' => 'kw_itemnames_entity_item_update',
'item update arguments' => array($entity_type, '%item', '%required'),
'item delete callback' => 'kw_itemnames_entity_item_delete',
'item delete arguments' => array($entity_type, '%item_id'),
'item extract id callback' => 'kw_itemnames_entity_item_extract_id',
'item extract id arguments' => array($entity_type, '%item'),
);
}
return $result;
}
function kw_itemnames_entity_item_load($entity_type, $entity_id) {
$entities = entity_load($entity_type, array($entity_id));
return !empty($entities) ? reset($entities) : FALSE;
}
function kw_itemnames_entity_item_create($entity_type, $defaults, $required) {
$entity_array = $required + $defaults;
$info = entity_get_info($entity_type);
$start_array = array();
foreach ($info['entity keys'] as $type => $key) {
if (isset($entity_array[$key])) {
$start_array[$key] = $entity_array[$key];
}
}
$entity = entity_create($entity_type, $start_array);
foreach ($entity_array as $key => $value) {
$entity->{$key} = $value;
}
entity_save($entity_type, $entity);
return $entity;
}
function kw_itemnames_entity_item_update($entity_type, $entity, $required) {
foreach ($required as $key => $value) {
$entity->{$key} = $value;
}
entity_save($entity_type, $entity);
return $entity;
}
function kw_itemnames_entity_item_delete($entity_type, $entity_id) {
entity_delete($entity_type, $entity_id);
}
function kw_itemnames_entity_item_extract_id($entity_type, $entity) {
list($entity_id,,) = entity_extract_ids($entity_type, $entity);
return $entity_id;
}