-
Notifications
You must be signed in to change notification settings - Fork 1
/
share_light.install
208 lines (192 loc) · 6.26 KB
/
share_light.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
<?php
use \Drupal\share_light\Loader;
use \Drupal\share_light\Block;
/**
* Implements hook_schema().
*/
function share_light_schema() {
$int = array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0);
$short_text = array('type' => 'varchar', 'not null' => FALSE, 'length' => 255);
$long_text = array('type' => 'text', 'size' => 'big', 'not null' => FALSE);
$schema['share_light_email_settings'] = array(
'fields' => array(
'nid' => $int,
'page_title' => $short_text,
'page_instructions' => $long_text,
'page_instructions_format' => $short_text,
'page_redirect' => $short_text,
'message_edit' => $int,
'message_subject' => $short_text,
'message_message' => $long_text,
'message_message_format' => $short_text,
'message_footer' => $long_text,
'message_footer_format' => $short_text,
),
'primary key' => array('nid'),
);
return $schema;
}
/**
* Implements hook_field_schema().
*/
function share_light_field_schema($field) {
if ($field['type'] == 'share_light') {
$columns['toggle'] = array(
'type' => 'int',
'not null' => FALSE,
'default' => 0,
);
$columns['options'] = array(
'type' => 'blob',
'not null' => FALSE,
'serialize' => TRUE,
);
return array(
'columns' => $columns,
);
}
}
function share_light_install() {
$default = array(
'toggle' => variable_get('share_light_default_toggle', 1),
'options' => Block::defaults(),
);
$default['options']['subject'] = '';
// create new field
_field_info_collate_types(TRUE);
$field = \Drupal\little_helpers\Field\Field::fromType('share_light', 'share_light');
$field->settings = array();
$field->save();
variable_set('share_light_channels_enabled', _share_light_default_channels());
_share_light_add_protocols();
}
function share_light_uninstall() {
variable_del('share_light_default_toggle');
variable_del('share_light_channels_enabled');
variable_del('share_light_tracking_enabled');
$email_fields = array(
'page_title',
'page_instructions',
'page_instructions_format',
'page_redirect',
'message_edit',
'message_subject',
'message_message',
'message_message_format',
'message_footer',
'message_footer_format',
);
foreach ($email_fields as $f) {
variable_del('share_light_email_' . $f);
}
}
function share_light_update_last_removed() {
return 7;
}
/**
* Default enabled channels.
*
* Everything but pinterest is enabled by default.
*/
function _share_light_default_channels() {
$channels = array();
foreach (array_keys(Loader::instance()->channelOptions()) as $name) {
$channels[$name] = $name;
}
$channels['pinterest'] = 0;
return $channels;
}
/**
* Add proprietary share url protocols to allowed protocols.
*/
function _share_light_add_protocols() {
$share_protocols = [
'fb-messenger',
'whatsapp',
];
// Defaults copied from drupal_strip_dangerous_protocols().
$defaults = [
'ftp',
'http',
'https',
'irc',
'mailto',
'news',
'nntp',
'rtsp',
'sftp',
'ssh',
'tel',
'telnet',
'webcal',
];
$allowed_protocols = variable_get('filter_allowed_protocols', $defaults);
$allowed_protocols = array_unique(array_merge($allowed_protocols, $share_protocols));
variable_set('filter_allowed_protocols', $allowed_protocols);
}
/**
* Allow proprietary share url protocols in links.
*/
function share_light_update_7201() {
_share_light_add_protocols();
}
/**
* Add text_format columns to share_light_email_settings.
*/
function share_light_update_7101() {
$short_text = array('type' => 'varchar', 'not null' => FALSE, 'length' => 255);
foreach (array('page_instructions_format', 'message_message_format', 'message_footer_format') as $f) {
db_add_field('share_light_email_settings', $f, $short_text);
}
}
/**
* Update channel option data structure.
*/
function share_light_update_8() {
$fields = field_read_fields(array('type' => 'share_light'));
foreach ($fields as $field) {
$instances = field_read_instances(array('field_name' => $field['field_name']), array('include_inactive' => TRUE));
foreach ($instances as $instance) {
foreach ($instance['default_value'] as $delta => $item) {
$instance['default_value'][$delta]['options'] = _share_light_update_8_options_map($item['options']);
}
// Avoid calling hook_field_update_instance since i18n will fail badly.
_field_write_instance($instance, TRUE);
}
$rows = db_query("SELECT entity_type, entity_id, delta, share_light_options FROM {field_data_{$field['field_name']}}");
foreach ($rows as $row) {
$options = serialize(_share_light_update_8_options_map(unserialize($row->share_light_options)));
db_update('field_data_' . $field['field_name'])
->fields(array('share_light_options' => $options))
->condition('entity_type', $row->entity_type)
->condition('entity_id', $row->entity_id)
->condition('delta', $row->delta)
->execute();
}
$rows = db_query("SELECT entity_type, entity_id, revision_id, delta, share_light_options FROM {field_revision_{$field['field_name']}}");
foreach ($rows as $row) {
$options = serialize(_share_light_update_8_options_map(unserialize($row->share_light_options)));
db_update('field_revision_' . $field['field_name'])
->fields(array('share_light_options' => $options))
->condition('entity_type', $row->entity_type)
->condition('entity_id', $row->entity_id)
->condition('revision_id', $row->revision_id)
->condition('delta', $row->delta)
->execute();
}
}
// Since we use _field_write_instance() directly
// we need to clear the caches manually.
field_cache_clear();
}
function _share_light_update_8_options_map($options) {
$options['link']['path'] = $options['share_url'];
unset($options['share_url']);
foreach(array('email', 'facebook', 'pinterest', 'twitter') as $channel) {
$options['channels'][$channel]['toggle'] = $options['advanced']["channel_{$channel}_toggle"];
}
$options['channels']['pinterest']['text'] = $options['advanced']['channel_pinterest_text'];
$options['channels']['twitter']['text'] = $options['advanced']['channel_twitter_text'];
unset($options['advanced']);
return $options;
}