forked from evandonovan/remote_block
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathremote_block.module
More file actions
207 lines (192 loc) · 6.42 KB
/
remote_block.module
File metadata and controls
207 lines (192 loc) · 6.42 KB
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
<?php
// $Id$
/**
* Implements hook_help()
*/
function remote_block_help($path, $arg) {
$output = '';
switch($path) {
case 'admin/help#remote_block':
$output = '<p>' . t('Remote Block renders blocks on a regionless page so that they may be embedded via an iframe. You may use the embed code it generates to embed any given block on a site. Note that required CSS and JS will be included, so blocks with special styles and behaviors (such as Quicktabs blocks) will still function.') . '</p>';
$output .= '<p>' . t('Remote Block can also provide the same content via JSON for more advanced embeds. See the examples/json-demo.html file to see how it works.') . '</p>';
$output .= '<p>' . t('Finally, Remote Block can display bare HTML if the ?theme=none query string is appended to the URL. This is useful when using wget to grab raw HTML for use in external sites.') . '</p>';
break;
}
return $output;
}
/**
* Implements hook_menu().
*/
function remote_block_menu() {
$items = array();
$items['remote-block/%/%/%'] = array(
'page callback' => 'remote_block_embed',
'page arguments' => array(1, 2, 3),
'access arguments' => array('access content'),
'type' => MENU_CALLBACK,
);
return $items;
}
/**
* Implements hook_block().
* Provides a block that shows the embed code for the "My City" page.
*/
function remote_block_block($op = 'list', $delta = 0) {
$block = array();
switch ($op) {
case 'list':
$block[0]['info'] = t('My City Embed Code');
return $block;
case 'view':
switch ($delta) {
case 0:
$block['subject'] = t('Embed This Page');
$block['content'] = remote_block_embed_code();
break;
}
return $block;
}
}
/**
* Content for the embed code block.
*/
function remote_block_embed_code() {
return drupal_get_form('rb_embed_code_form');
}
/**
* Form definition for the embed code block.
*/
function rb_embed_code_form() {
$code = rb_get_city_embed_code();
$form = array();
$form['rb_embed_code'] = array(
'#type' => 'fieldset',
'#title' => t('Embed this page'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
);
$form['rb_embed_code']['code'] = array(
'#type' => 'textarea',
'#title' => t('Embed code'),
'#description' => t('Copy and paste this code into the <body> of your website\'s HTML'),
'#value' => $code,
'#maxlength' => FALSE,
);
return $form;
}
/**
* Generates the embed code itself.
*/
function rb_get_city_embed_code() {
if(arg(0) == "taxonomy" && arg(1) == "term" && is_numeric(arg(2))) {
$term = taxonomy_get_term(arg(2));
if(is_object($term)) {
$embed_code = <<<EOF
<!-- begin TechMission ChristianVolunteering embed code for $term->name -->
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript" src="http://www.urbanministry.org/sites/all/modules/custom/remote_block/js/jquery.ba-postmessage.min.js"></script>
<script type="text/javascript">
$(function(){
var if_height,
src = 'http://www.urbanministry.org/remote-block/html/quicktabs/17/$term->tid#' + encodeURIComponent( document.location.href ),
iframe = $( '<iframe " src="' + src + '" width="600" height="50" scrolling="no" frameborder="0"><\/iframe>' )
.appendTo( '#iframe' );
$.receiveMessage(function(e){
var h = Number( e.data.replace( /.*if_height=(\d+)(?:&|$)/, '$1' ) );
if ( !isNaN( h ) && h > 0 && h !== if_height ) {
iframe.height( if_height = h );
}
}, 'http://www.urbanministry.org' );
});
</script>
<div id="iframe"></div>
<!-- end embed code -->
EOF;
return $embed_code;
}
else {
return "No embed code available for the current term.";
}
}
else {
return "No embed code available.";
}
}
/**
* Menu callback for remote-block/%/%/% -
* invokes the block view hook in order to render a certain block.
*/
function remote_block_embed($format, $module, $delta) {
// Sets the variable to print without CSS and JS based on query string.
$theme = TRUE;
if(isset($_GET) && isset($_GET['theme']) && $_GET['theme'] == 'none') { $theme = FALSE; }
// Loads the block content, or returns error if failure.
if(isset($module) && module_exists($module) && isset($delta)) {
$block = module_invoke($module, 'block', 'view', $delta);
}
else {
print 'Invalid request.';
}
// Sets up the variables of $head, $css, $content, and $js.
$head = drupal_get_html_head();
$css = drupal_get_css();
// Includes Quicktabs CSS if necessary.
if($module == 'quicktabs') {
$css .=
'<style type="text/css">
.quicktabs-hide {
display: block;
position: absolute;
left: -10000px;
top: -10000px;
}
</style>';
}
// If $format is "html" then add the JS for <iframe> window.postMessage.
drupal_add_js(drupal_get_path('module', 'remote_block') . '/js/jquery.ba-postmessage.min.js');
drupal_add_js(drupal_get_path('module', 'remote_block') . '/js/remote_block_child.js');
// Adds JS at the bottom.
$js = drupal_get_js();
// Builds an entire HTML page for the HTML display format
$content = '<!DOCTYPE html><html><head>' . $head . $css . '</head><body>' . $block['content'] . $js . '</body></html>';
// Sets up variables for the JSON of the block, and JSON of block plus CSS/JS.
$json_block = array('page' => $block['content']);
// Sets up content array so page, css, and js can be added at specific points.
$json_content = array('content' => $block['content'], 'css' => $css, 'js' => $js);
if(isset($block) && is_array($block) && isset($block['content'])) {
if($theme == FALSE) {
if($format == 'html') {
print $block['content'];
}
elseif($format == 'json') {
if(isset($_GET['callback'])) {
$json_callback = $_GET['callback'];
}
else {
$json_callback = '';
}
echo $json_callback . '(' . json_encode($json_block) . ');';
}
else {
print 'Invalid format.';
}
}
else {
if($format == 'html') {
print $content;
}
elseif($format == 'json') {
if(isset($_GET['callback'])) {
$json_callback = $_GET['callback'];
}
else {
$json_callback = '';
}
echo $json_callback . '(' . json_encode($json_content) . ');';
}
else {
print 'Invalid format.';
}
}
}
}