-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplug_field.module
202 lines (183 loc) · 6.37 KB
/
plug_field.module
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
<?php
/**
* @file
* Module implementation file.
*/
use \Drupal\plug_field\FieldDefinition;
use \Drupal\plug_field\PlugFieldTypeManager;
/**
* Implements hook_system_info_alter().
*
* Ensures that modules providing field plugins in use can't be disabled.
*/
function plug_field_system_info_alter(&$info, $file, $type) {
$definitions = PlugFieldTypeManager::get()->getDefinitions();
foreach ($definitions as $definition) {
if ($type == 'module' && $definition['provider'] == $file->name) {
$fields = field_read_fields(array('type' => $definition['id']), array('include_deleted' => TRUE));
if ($fields) {
$info['required'] = TRUE;
// Provide an explanation message (only mention pending deletions if
// there remains no actual, non-deleted fields).
$non_deleted = FALSE;
foreach ($fields as $field) {
if (empty($field['deleted'])) {
$non_deleted = TRUE;
break;
}
}
if ($non_deleted) {
if (module_exists('field_ui')) {
$explanation = t('Field type(s) in use - see <a href="@fields-page">Field list</a>', array('@fields-page' => url('admin/reports/fields')));
}
else {
$explanation = t('Fields type(s) in use');
}
}
else {
$explanation = t('Fields pending deletion');
}
$info['explanation'] = $explanation;
}
}
}
}
/**
* Implements hook_menu_alter().
*
* Creates a new menu callback for field list page to show the plugin provider
* instead of "Plug Field".
*/
function plug_field_menu_alter(&$items) {
if (isset($items['admin/reports/fields'])) {
$items['admin/reports/fields']['page callback'] = 'plug_field_field_ui_fields_list';
$items['admin/reports/fields']['file'] = 'plug_field.admin.inc';
$items['admin/reports/fields']['file path'] = drupal_get_path('module', 'plug_field');
}
}
/**
* Implements hook_field_info().
*/
function plug_field_field_info() {
return PlugFieldTypeManager::get()->getDefinitions();
}
/**
* Implements hook_field_load().
*/
function plug_field_field_load($entity_type, $entities, $field, $instances, $langcode, &$items, $age) {
if ($plugin = PlugFieldTypeManager::get()->createInstance($field['type'], array(
'fieldDefinition' => new FieldDefinition($field),
))) {
$plugin->load($entity_type, $entities, $instances, $langcode, $items, $age);
}
}
/**
* Implements hook_field_prepare_view().
*/
function plug_field_field_prepare_view($entity_type, $entities, $field, $instances, $langcode, &$items) {
if ($plugin = PlugFieldTypeManager::get()->createInstance($field['type'], array(
'fieldDefinition' => new FieldDefinition($field),
))) {
$plugin->prepareView($entity_type, $entities, $instances, $langcode, $items);
}
}
/**
* Implements hook_field_validate().
*/
function plug_field_field_validate($entity_type, $entity, $field, $instance, $langcode, $items, &$errors) {
if ($plugin = PlugFieldTypeManager::get()->createInstance($field['type'], array(
'fieldDefinition' => new FieldDefinition($field),
))) {
$plugin->validate($entity_type, $entity, $instance, $langcode, $items, $errors);
}
}
/**
* Implements hook_field_presave().
*/
function plug_field_field_presave($entity_type, $entity, $field, $instance, $langcode, &$items) {
if ($plugin = PlugFieldTypeManager::get()->createInstance($field['type'], array(
'fieldDefinition' => new FieldDefinition($field),
))) {
$plugin->preSave($entity_type, $entity, $instance, $langcode, $items);
}
}
/**
* Implements hook_field_insert().
*/
function plug_field_field_insert($entity_type, $entity, $field, $instance, $langcode, &$items) {
if ($plugin = PlugFieldTypeManager::get()->createInstance($field['type'], array(
'fieldDefinition' => new FieldDefinition($field),
))) {
$plugin->insert($entity_type, $entity, $instance, $langcode, $items);
}
}
/**
* Implements hook_field_update().
*/
function plug_field_field_update($entity_type, $entity, $field, $instance, $langcode, &$items) {
if ($plugin = PlugFieldTypeManager::get()->createInstance($field['type'], array(
'fieldDefinition' => new FieldDefinition($field),
))) {
$plugin->update($entity_type, $entity, $instance, $langcode, $items);
}
}
/**
* Implements hook_field_delete().
*/
function plug_field_field_delete($entity_type, $entity, $field, $instance, $langcode, $items) {
if ($plugin = PlugFieldTypeManager::get()->createInstance($field['type'], array(
'fieldDefinition' => new FieldDefinition($field),
))) {
$plugin->delete($entity_type, $entity, $instance, $langcode, $items);
}
}
/**
* Implements hook_field_delete_revision().
*/
function plug_field_field_delete_revision($entity_type, $entity, $field, $instance, $langcode, &$items) {
if ($plugin = PlugFieldTypeManager::get()->createInstance($field['type'], array(
'fieldDefinition' => new FieldDefinition($field),
))) {
$plugin->deleteRevision($entity_type, $entity, $instance, $langcode, $items);
}
}
/**
* Implements hook_field_prepare_translation().
*/
function plug_field_field_prepare_translation($entity_type, $entity, $field, $instance, $langcode, &$items, $source_entity, $source_langcode) {
if ($plugin = PlugFieldTypeManager::get()->createInstance($field['type'], array(
'fieldDefinition' => new FieldDefinition($field),
))) {
$plugin->prepareTranslation($entity_type, $entity, $instance, $langcode, $items, $source_entity, $source_langcode);
}
}
/**
* Implements hook_field_is_empty().
*/
function plug_field_field_is_empty($item, $field) {
if ($plugin = PlugFieldTypeManager::get()->createInstance($field['type'], array(
'fieldDefinition' => new FieldDefinition($field),
))) {
return $plugin->isEmpty($item);
}
}
/**
* Implements hook_field_settings_form().
*/
function plug_field_field_settings_form($field, $instance, $has_data) {
if ($plugin = PlugFieldTypeManager::get()->createInstance($field['type'], array(
'fieldDefinition' => new FieldDefinition($field),
))) {
return $plugin->settingsForm($instance, $has_data);
}
}
/**
* Implements hook_field_instance_settings_form().
*/
function plug_field_field_instance_settings_form($field, $instance) {
if ($plugin = PlugFieldTypeManager::get()->createInstance($field['type'], array(
'fieldDefinition' => new FieldDefinition($field),
))) {
return $plugin->instanceSettingsForm($instance);
}
}