-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsubd.module
149 lines (136 loc) · 3.91 KB
/
subd.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
<?php
/**
* Implementationation of hook_menu().
*/
function subd_menu() {
$items = array();
$items['subd-router'] = array(
'page callback' => 'subd_router_page',
'access arguments' => array('access content'),
'type' => MENU_CALLBACK,
);
return $items;
}
function subd_router_page() {
// If a node is found, swap in that node's content for the main page content.
// This is not madness. THIS IS DRUPAL 7!
if ($node = subd_get_current_node()) {
drupal_set_breadcrumb(array());
menu_set_active_item('node/' . $node->nid);
return node_page_view($node);
}
else {
// If there are no instances of the field defined, also bail out!
$field = field_info_field('subd_subdomain');
if (empty($field)) {
return FALSE;
}
// Get a list of all subdomain'd nodes.
$conditions = array(array('type', 'node'), array('value', 'NULL', '!='));
$results = field_attach_query($field['id'], $conditions);
if (!empty($results['node'])) {
$base_uri = _subd_get_base_domain();
foreach($results['node'] as $eid => $entity) {
$node = node_load($eid);
$subdomain = $node->subd_subdomain['und'][0]['value'];
$items[] = l($node->title, "http://$subdomain.$base_uri");
return theme('item_list', array('items' => $items));
}
}
}
}
function subd_get_current_node() {
// Sweet fuck, this is wrong and bad and wrong and more bad.
// But, you know. How to extract the subdomain? Oh, Drupal. This works well
// enough for a domain structure that is known and predictable.
$uri = $_SERVER['HTTP_HOST'];
$parsed = explode('.', $uri);
if (count($parsed) > 2) {
$subdomain = array_shift($parsed);
}
else {
return FALSE;
}
// If there are no instances of the field defined, also bail out!
$field = field_info_field('subd_subdomain');
if (empty($field)) {
return FALSE;
}
// See if any nodes match the subdomain.
$conditions = array(array('type', 'node'), array('value', $subdomain, '='));
$results = field_attach_query($field['id'], $conditions);
if (!empty($results['node'])) {
foreach($results['node'] as $eid => $entity) {
return node_load($eid);
}
}
return FALSE;
}
/**
* Add a 'Subdomain' field to a given content type
*
* @param $type
* A node type object.
* @param $label
* The label for the body instance.
*/
function subd_add_subdomain_field($type, $label = 'Subdomain') {
// Add or remove the body field, as needed.
$field = field_info_field('subd_subdomain');
$instance = field_info_instance('node', 'subd_subdomain', $type);
if (empty($field)) {
$field = array(
'field_name' => 'subd_subdomain',
'type' => 'text',
'entity_types' => array('node'),
'translatable' => FALSE,
);
$field = field_create_field($field);
}
if (empty($instance)) {
$instance = array(
'field_name' => 'subd_subdomain',
'entity_type' => 'node',
'bundle' => $type,
'label' => $label,
'widget_type' => 'text',
'settings' => array('required' => TRUE),
// Define default formatters for the teaser and full view.
'display' => array(
'full' => array(
'label' => 'hidden',
'type' => 'hidden',
),
'teaser' => array(
'label' => 'hidden',
'type' => 'hidden',
),
'rss' => array(
'label' => 'hidden',
'type' => 'hidden',
),
'search_index' => array(
'label' => 'hidden',
'type' => 'default',
),
'search_results' => array(
'label' => 'hidden',
'type' => 'hidden',
),
),
);
field_create_instance($instance);
}
}
function subd_preprocess_page(&$vars) {
$vars['front_page'] = 'http://' . _subd_get_base_domain();
}
function _subd_get_base_domain() {
$base_uri = $_SERVER['HTTP_HOST'];
$parsed = explode('.', $base_uri);
if (count($parsed) > 2) {
array_shift($parsed);
$base_uri = join('.', $parsed);
}
return $base_uri;
}