-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsl7_news.install
executable file
·217 lines (201 loc) · 6.31 KB
/
sl7_news.install
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
<?php
/**
* @author Semyon Dragunov <sam.dragunov@gmail.com>
* https://github.com/SemyonDragunov
*/
/**
* Implements hook_install().
*/
function sl7_news_install() {
$t = get_t();
_sl7_news_create_taxonomy();
_sl7_news_create_node_type();
// Добавляем права.
if ($role_manager = user_role_load_by_name('manager')) {
$editor_permissions = array(
'create sl7_news content' => TRUE,
'edit own sl7_news content' => TRUE,
'delete own sl7_news content' => TRUE,
);
user_role_change_permissions($role_manager->rid, $editor_permissions);
}
// Set path breadcrumbs settings.
variable_set('path_breadcrumbs_home_link_title', 'Главная');
// Set pathauto pattern.
variable_set('pathauto_node_sl7_news_pattern', 'news/[node:title]-[node:nid]');
variable_set('pathauto_taxonomy_term_sl7_news_rubric_pattern', 'rubric/[term:name]');
}
function _sl7_news_create_taxonomy() {
$vocab = (object) array(
'name' => 'Рубрики новостей',
'description' => 'Категории новостей.',
'machine_name' => 'sl7_news_rubric',
);
if (!taxonomy_vocabulary_machine_name_load($vocab->machine_name)) {
taxonomy_vocabulary_save($vocab);
}
}
function _sl7_news_create_node_type() {
node_types_rebuild();
$types = node_type_get_types();
node_add_body_field($types['sl7_news'], 'Статья');
variable_set("comment_{$types['sl7_news']->type}", 1);
variable_set("node_preview_{$types['sl7_news']->type}", 0);
variable_set("node_options_{$types['sl7_news']->type}", array('status'));
variable_set("node_submitted_{$types['sl7_news']->type}", 0);
$fields = array(
array(
'field_name' => 'sl7_news_image',
'type' => 'image',
'cardinality' => 1,
'module' => 'image',
'locked' => TRUE,
'settings' => array(
'uri_scheme' => 'public',
'default_image' => 0,
),
'foreign keys' => array(
'fid' => array(
'columns' => array(
'fid' => 'fid',
),
'table' => 'file_managed',
),
),
'indexes' => array(
'fid' => array(
0 => 'fid',
),
),
),
array(
'field_name' => 'sl7_news_rubric',
'type' => 'taxonomy_term_reference',
'cardinality' => FIELD_CARDINALITY_UNLIMITED,
'locked' => TRUE,
'settings' => array(
'allowed_values' => array(
array(
'vocabulary' => 'sl7_news_rubric',
'parent' => 0
),
),
),
),
);
foreach ($fields as $field) {
$exists = db_query("SELECT id FROM {field_config} WHERE field_name=:field_name", array(':field_name' => $field['field_name']))->fetchField();
if (!$exists) {
field_create_field($field);
}
}
$instances = array(
array(
'field_name' => 'sl7_news_image',
'entity_type' => 'node',
'bundle' => 'sl7_news',
'required' => TRUE,
'label' => 'Изображение',
'settings' => array(
'max_filesize' => 1024*1024*2,
'min_resolution' => '500x400',
'file_extensions' => 'png gif jpg jpeg',
'file_directory' => 'sl7_news',
),
'widget' => array(
'type' => 'image_image',
'module' => 'image',
),
'display' => array(
'default' => array(
'label' => 'hidden',
'type' => 'image',
'settings' => array(
'image_style' => 'sl7_news_teaser'
),
'weight' => -2,
),
'teaser' => array(
'label' => 'hidden',
'type' => 'image',
'settings' => array(
'image_style' => 'sl7_news_teaser'
),
),
),
),
array(
'field_name' => 'sl7_news_rubric',
'description' => 'Если хотите разделить новости структурно, то укажите рубрику. <a href="/admin/structure/taxonomy/sl7_news_rubric">Управление рубриками.</a>',
'entity_type' => 'node',
'label' => 'Рубрика',
'bundle' => 'sl7_news',
'widget' => array(
'type' => 'options_select',
),
'display' => array(
'default' => array(
'label' => 'hidden',
'type' => 'taxonomy_term_reference_link',
'module' => 'taxonomy',
'weight' => -1,
),
'teaser' => array(
'label' => 'hidden',
'type' => 'taxonomy_term_reference_link',
'module' => 'taxonomy',
)
),
),
);
foreach ($instances as $instance) {
$exists = db_query("SELECT id FROM {field_config_instance} WHERE field_name=:field_name AND entity_type=:entity_type AND bundle=:bundle", array(':field_name' => $instance['field_name'], ':entity_type' => $instance['entity_type'], ':bundle' => $instance['bundle']))->fetchField();
if (!$exists) {
field_create_instance($instance);
}
}
}
/**
* Implements hook_uninstall().
*/
function sl7_news_uninstall() {
// Удаление нод sl7_news.
$node_type = 'sl7_news';
$sql = 'SELECT nid FROM {node} n WHERE n.type = :type';
$result = db_query($sql, array(':type' => $node_type));
$nids = array();
foreach ($result as $row) {
$nids[] = $row->nid;
}
node_delete_multiple($nids);
node_type_delete($node_type);
// Удаление полей нод sl7_news.
field_attach_delete_bundle('node', 'sl7_news');
db_delete('field_config_instance')->condition('entity_type', 'node')->condition('bundle', 'sl7_news')->execute();
db_delete('field_config_instance')
->condition('field_name', 'comment_body')
->condition('bundle', 'comment_node_sl7_news')
->execute();
db_delete('field_config')->condition('field_name', 'sl7_news_%', 'LIKE')->execute();
// Удаление таксономии.
$vocab = taxonomy_vocabulary_machine_name_load('sl7_news_rubric');
taxonomy_vocabulary_delete($vocab->vid);
// Удаление views.
$views = array(
'sl7_news',
);
foreach ($views as $name) {
if ($view = views_get_view($name)) {
$view->delete();
}
}
// Удаление переменных.
$result = db_select('variable', 'v')
->fields('v', array('name'))
->condition('name', 'sl7_news_%', 'LIKE')
->execute();
foreach ($result as $row) {
variable_del($row->name);
}
field_purge_batch(1000);
}