Newer
Older
Joachim Noreiko
committed
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
<?php
/**
* @file data_entity.module
* TODO: Enter file description here.
*/
/**
* Get all data tables which are declared as entities.
*
* @todo This is just a wrapper around data_get_all_tables() for now;
* add an admin UI later.
*/
function data_entity_get_entity_tables() {
return data_get_all_tables();
}
/**
* Adds our default values to a table's meta information.
*/
function data_entity_meta_add_defaults(&$meta) {
foreach ($meta['fields'] as $field => $data) {
$meta['fields'][$field] += array(
'locked' => FALSE,
'required' => FALSE,
);
}
}
/**
* Get the name of a table entity's id.
*
* @todo: consider moving this to the DataTable class.
*/
function data_entity_get_id_field($table) {
// @todo: whopping assumption that there's a single primary key field.
$id_field = $table->table_schema['primary key'][0];
return $id_field;
}
/**
* Implements hook_entity_info().
*
* Declare every data table as an entity.
*
* @todo Add an admin UI to request tables for this rather than do all.
*/
function data_entity_entity_info() {
$tables = data_entity_get_entity_tables();
$info = array();
foreach ($tables as $table_name => $table) {
$id_field = data_entity_get_id_field($table);
$info['data_' . $table_name] = array(
'label' => $table->title,
'controller class' => 'DataEntityController',
'base table' => $table_name,
'entity keys' => array(
'id' => $id_field,
),
'bundles' => array(),
'view modes' => array(),
'uri callback' => 'data_entity_uri',
);
}
return $info;
}
/**
* Implements hook_menu().
*/
function data_entity_menu() {
$items['admin/content/data/entity/%data_ui_table/%data_entity_item'] = array(
'title' => 'Edit data item',
'load arguments' => array(4),
'page callback' => 'drupal_get_form',
'page arguments' => array('data_entity_entity_edit_form', 4, 5),
'file' => 'data_entity.pages.inc',
'access callback' => 'data_entity_table_access',
'access arguments' => array(4),
);
$items['admin/structure/data/edit/%data_ui_table/entity-form'] = array(
'title' => 'Configure entity form',
'description' => 'Administer data tables.',
'page callback' => 'drupal_get_form',
'page arguments' => array('data_entity_admin_entity_form', 4),
'file' => 'data_entity.admin.inc',
'access arguments' => array('administer data tables'),
'type' => MENU_LOCAL_TASK,
);
return $items;
}
/**
* Menu access callback.
*/
function data_entity_table_access($table) {
return user_access('edit data in table ' . $table->name);
}
/**
* Menu loader callback.
*
* Called 'data_entity_item_load' to avoid being data's hook_entity_load()!
*/
function data_entity_item_load($deid, $table_name) {
$entity_type = 'data_' . $table_name;
$data_entity = entity_load($entity_type, array($deid));
return $data_entity ? reset($data_entity) : FALSE;
}
/**
* Implements hook_permission().
*/
function data_entity_permission() {
$tables = data_entity_get_entity_tables();
$permissions = array();
foreach ($tables as $table_name => $table) {
$permissions['edit data in table ' . $table_name] = array(
'title' => t('Edit data in the %table_name table', array('%table_name' => $table->title)),
);
}
return $permissions;
}
/**
* Entity URI callback.
*/
function data_entity_uri($data_entity) {
$entity_type = $data_entity->entity_type;
list($id, ) = entity_extract_ids($entity_type, $data_entity);
return array(
'path' => 'admin/content/data/entity/' . $data_entity->data_table . '/' . $id,
);
}
/**
* Implements hook_theme().
*/
function data_entity_theme($existing, $type, $theme, $path) {
return array(
'data_entity_admin_entity_form' => array(
'render element' => 'form',
),
);
}
/**
* Implements hook_views_api().
*/
function data_entity_views_api() {
return array(
'api' => '3.0-alpha1',
'path' => drupal_get_path('module', 'data_entity') . '/views',
);
}