-
Notifications
You must be signed in to change notification settings - Fork 2
/
opengraphprotocol.module
executable file
·188 lines (180 loc) · 6 KB
/
opengraphprotocol.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
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
<?php
/**
* @file
* Enables Open Graph protocol output for Drupal sites.
*/
/**
* Implements hook_rdf_namespaces().
*/
function opengraphprotocol_rdf_namespaces() {
return array(
'og' => 'http://ogp.me/ns#',
'fb' => 'http://www.facebook.com/2008/fbml',
);
}
/**
* Implements hook_form_FORM_ID_alter().
*
* Adds a vertical tab in the edit content type form to specify which Open
* Graph protocol type should associated with each content type.
*/
function opengraphprotocol_form_node_type_form_alter(&$form, $form_state) {
// Object types defined at http://opengraphprotocol.org/#types
$og_types = array(
'Activities' => array(
'activity',
'sport',
),
'Businesses' => array(
'bar',
'company',
'cafe',
'hotel',
'restaurant',
),
'Groups' => array(
'cause',
'sports_league',
'sports_team',
),
'Organizations' => array(
'band',
'government',
'non_profit',
'school',
'university',
),
'People' => array(
'actor',
'athlete',
'author',
'director',
'musician',
'politician',
'public_figure',
),
'Places' => array(
'city',
'country',
'landmark',
'state_province',
),
'Products and Entertainment' => array(
'album',
'book',
'drink',
'food',
'game',
'movie',
'product',
'song',
'tv_show',
),
'Websites' => array(
'article',
// website and blog are designed to represent an entire site, an og:type
// tag with types website or blog should usually only appear on the root
// of a domain. see http://developers.facebook.com/docs/opengraph
//'blog',
//'website',
),
);
// Serializes the OGP types for Drupal's Form API drop down list.
foreach ($og_types as $name => $group) {
foreach ($group as $type) {
$og_types_options[$name][$type] = $type;
}
}
if (isset($form['type'])) {
$form['opengraphprotocol'] = array(
'#type' => 'fieldset',
'#title' => t('Open Graph protocol settings'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
'#group' => 'additional_settings',
);
$form['opengraphprotocol']['opengraphprotocol_type'] = array(
'#type' => 'select',
'#title' => t('Open Graph protocol type'),
'#description' => t('Choose the Open Graph protocol type you want to associate with this content type.'),
'#default_value' => variable_get('opengraphprotocol_type_' . $form['#node_type']->type, ''),
'#options' => $og_types_options,
);
$form['opengraphprotocol']['opengraphprotocol_fb_admins'] = array(
'#type' => 'textfield',
'#title' => t('Facebook Admins IDs'),
'#description' => t('A comma-separated list of Facebook user IDs that administer the pages of this content type.'),
'#default_value' => variable_get('opengraphprotocol_fb_admins_' . $form['#node_type']->type, ''),
);
}
}
/**
* Implements MODULE_preprocess_HOOK().
*
* Adds the various Open Graph protocol HTML elements during page rendering.
*/
function opengraphprotocol_preprocess_node(&$variables) {
$node = $variables['node'];
// Uses the mapping for og:type if it has been defined by the administrator.
$meta_tags[] = array(
'property' => 'og:type',
'content' => variable_get('opengraphprotocol_type_' . $node->type, $node->type),
);
$meta_tags[] = array(
'property' => 'og:title',
'content' => $node->title,
);
$meta_tags[] = array(
'property' => 'og:url',
'content' => url('node/' . $node->nid, array('absolute' => TRUE)),
);
$meta_tags[] = array(
'property' => 'og:site_name',
'content' => variable_get('site_name'),
);
$meta_tags[] = array(
'property' => 'fb:admins',
'content' => variable_get('opengraphprotocol_fb_admins_' . $node->type, ''),
);
// Exposes the value of any field known to og: (these are optional).
$og_properties = array('description', 'image', 'latitude', 'longitude', 'street-address', 'locality', 'region', 'postal-code', 'country-name', 'email', 'phone_number', 'fax_number');
foreach ($og_properties as $property) {
$field = 'field_' . $property;
if (isset($node->$field)) {
$field_info = field_info_field($field);
// We need a different logic depending on the type of data to be exposed.
switch ($field_info['module']) {
case 'number' :
if (isset($node->{$field}[LANGUAGE_NONE][0]['value'])) {
$meta_tags[] = array(
'property' => 'og:' . $property ,
'content' => $node->{$field}[LANGUAGE_NONE][0]['value'],
);
}
break;
case 'text' :
if (isset($node->{$field}[LANGUAGE_NONE][0]['safe_value'])) {
$meta_tags[] = array(
'property' => 'og:' . $property ,
'content' => $node->{$field}[LANGUAGE_NONE][0]['safe_value'],
);
}
break;
case 'image' :
if (isset($node->{$field}[LANGUAGE_NONE][0]['uri'])) {
$meta_tags[] = array(
'property' => 'og:' . $property ,
'content' => file_create_url($node->{$field}[LANGUAGE_NONE][0]['uri']),
);
}
break;
}
}
}
// Outputs all the various metadata from above as meta tags in the head tag.
foreach ($meta_tags as $id => $meta_tag) {
drupal_add_html_head(array('#tag' => 'meta', '#attributes' => $meta_tag), 'opengraphprotocol_' . $id);
}
// Places a Facebook "Like" above the node links.
$variables['content']['links']['node']['#prefix'] = '<div id="opengraphprotocol-like"><iframe src="http://www.facebook.com/plugins/like.php?href=' . urlencode(url('node/' . $node->nid, array('absolute' => TRUE))) . '&layout=standard&show_faces=true&width=450&action=like&font&colorscheme=light&height=80" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:350px; height:80px;" allowTransparency="true"></iframe></div>';
}