forked from evandonovan/remote_block
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathremote_block.module
More file actions
159 lines (140 loc) · 4.41 KB
/
remote_block.module
File metadata and controls
159 lines (140 loc) · 4.41 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
<?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;
}
/**
* Generates the embed code itself.
*/
function remote_block_get_embed_code($bid) {
if (!remote_block_embed_allowed($bid) {
return '';
}
$data = remote_block_get_embed_data($bid);
$embed_code = <<<EOF
<!-- begin embed code -->
<div id="iframe">
<iframe src="$data->src" width="$data->width" height="$data->height" scrolling="no" frameborder="0"></iframe>
</div>
<!-- end embed code -->
EOF;
return $embed_code;
}
/**
* Check if this block is allowed to be embedded.
*/
function remote_block_embed_allowed($bid) {
// @TODO: Stub.
return TRUE;
}
/**
* Get width and height specified for this widget.
*/
function remote_block_get_embed_data($bid) {
// @TODO: Stub.
$data = array(
'width' => 100,
'height' => 100,
'src' => "http://www.example.com/remote-block/%/%/%",
);
return $data;
}
/**
* 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;
}
/**
* 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>';
}
// 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.';
}
}
}
}