-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathplug_config.module
119 lines (107 loc) · 4.24 KB
/
plug_config.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
<?php
/**
* @file
* Module implementation file.
*/
use Drupal\plug_config\Plugin\PlugConfigManager;
/**
* Implements hook_modules_installed()
*/
function plug_config_modules_installed($modules) {
$definitions = PlugConfigManager::scopedFactory($modules)->getDefinitions();
foreach ($modules as $module) {
array_walk($definitions, function($definition, $key) use ($module){
if ($definition['provider'] == $module && !db_table_exists($key)) {
db_create_table($key, call_user_func(array($definition['class'], 'schema')));
}
});
}
drupal_get_schema(NULL, TRUE);
}
/**
* Implements hook_modules_uninstalled().
*/
function plug_config_modules_uninstalled($modules) {
$definitions = PlugConfigManager::scopedFactory($modules)->getDefinitions();
foreach ($modules as $module) {
array_walk($definitions, function ($definition, $key) use ($module) {
if ($definition['provider'] == $module && db_table_exists($key)) {
db_drop_table($key);
}
});
}
}
/**
* Implements hook_entity_info().
*/
function plug_config_entity_info() {
return PlugConfigManager::get()->getDefinitions();
}
/**
* A wrapper around drupal_get_form() that helps building entity forms.
*
* This function may be used by entities to build their entity form. It has to
* be used instead of calling drupal_get_form().
* Entity forms built with this helper receive useful defaults suiting for
* editing a single entity, whereas the special cases of adding and cloning
* of entities are supported too.
*
* While this function is intended to be used to get entity forms for entities
* using the entity ui controller, it may be used for entity types not using
* the ui controller too.
*
* @param $entity_type
* The entity type for which to get the form.
* @param $entity
* The entity for which to return the form.
* If $op is 'add' the entity has to be either initialized before calling this
* function, or NULL may be passed. If NULL is passed, an entity will be
* initialized with empty values using entity_create(). Thus entities, for
* which this is problematic have to care to pass in an initialized entity.
*
* @param string $op
* (optional) One of 'edit', 'add' or 'clone'. Defaults to edit.
* @param array $form_state
* (optional) A pre-populated form state, e.g. to add in form include files.
* See entity_metadata_form_entity_ui().
*
* @return mixed
* The fully built and processed form, ready to be rendered.
*
* @throws EntityMalformedException
*
* @see EntityDefaultUIController::hook_forms().
* @see entity_ui_form_submit_build_entity().
*/
function plug_config_get_form($entity_type, $entity, $op = 'edit', $form_state = array()) {
$form_id = 'plug_config_entity_form';
if (!isset($entity) && $op == 'add') {
$entity = entity_create($entity_type, array());
}
// Do not use drupal_get_form(), but invoke drupal_build_form() ourselves so
// we can prepopulate the form state.
$form_state['wrapper_callback'] = 'entity_ui_main_form_defaults';
$form_state['entity_type'] = $entity_type;
form_load_include($form_state, 'inc', 'entity', 'includes/entity.ui');
// Handle cloning. We cannot do that in the wrapper callback as it is too late
// for changing arguments.
if ($op == 'clone') {
$entity = entity_ui_clone_entity($entity_type, $entity);
}
// We don't pass the entity type as first parameter, as the implementing
// module knows the type anyway. However, in order to allow for efficient
// hook_forms() implementations we append the entity type as last argument,
// which the module implementing the form constructor may safely ignore.
// @see entity_forms()
$form_state['build_info']['args'] = array($entity, $op, $entity_type);
return drupal_build_form($form_id, $form_state);
}
function plug_config_entity_form($form, &$form_state, $config_entity, $op = 'edit') {
/** @var \Drupal\plug_config\Plugin\Config\DefaultConfigEntityInterface $config_entity */
return $config_entity->form($form, $form_state, $op);
}
function plug_config_entity_form_submit($form, &$form_state) {
/** @var \Drupal\plug_config\Plugin\Config\DefaultConfigEntityInterface $config_entity */
$config_entity = $form_state[$form_state['entity_type']];
$config_entity->formSubmit($form, $form_state);
}