/
waterbed_rooms.module
157 lines (132 loc) · 4.38 KB
/
waterbed_rooms.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
<?php
/**
* @file
* Code for the waterbed_rooms feature.
*/
include_once 'waterbed_rooms.features.inc';
/**
* Implements hook_query_alter().
*/
function waterbed_rooms_query_alter(QueryAlterableInterface $query) {
$view_name = 'waterbed';
if ($query->hasTag('views_' . $view_name)) {
$query->groupBy('nid');
}
}
/**
* Implements hook_field_formatter_info().
*/
function waterbed_rooms_field_formatter_info() {
return array(
'image_services' => array(
'label' => t('Image for Services'),
'field types' => array('image'),
),
);
}
/**
* Implements hook_field_formatter_settings_form().
*/
function waterbed_rooms_field_formatter_settings_form($field, $instance, $view_mode, $form, &$form_state) {
$display = $instance['display'][$view_mode];
$settings = $display['settings'];
$image_styles = image_style_options(FALSE);
$element['image_style'] = array(
'#title' => t('Image style'),
'#type' => 'select',
'#default_value' => $settings['image_style'],
'#empty_option' => t('None (original image)'),
'#options' => $image_styles,
);
return $element;
}
/**
* Implements hook_field_formatter_view().
*/
function waterbed_rooms_field_formatter_view($entity_type, $entity, $field, $instance, $lang_code, $items, $display) {
$elements = array();
foreach ($items as $delta => $item) {
// Get full image URL based on provided image style.
if (in_array($field['type'], array('file','image')) && !empty($display['settings']['image_style']) && $image_style = image_style_load($display['settings']['image_style'])) {
$uri = image_style_url($display['settings']['image_style'], $item['uri']);
// Get file size of image preset file (if it has already been created,
// otherwise just create it first and then get file size).
$path = image_style_path($display['settings']['image_style'], $item['uri']);
$real_path = drupal_realpath($path);
if (file_exists($real_path) || image_style_create_derivative($image_style, $item['uri'], $path)) {
$item['filesize'] = filesize($real_path);
}
}
else {
$uri = file_create_url($item['uri']);
}
$elements[$delta] = array(
'#markup' => $uri
);
}
return $elements;
}
/**
* Implements hook_services_resources().
*/
function waterbed_rooms_services_resources() {
// Re implemented services for node to include absolute URL for images
$node_resource = array(
'node_waterbed' => array(
'operations' => array(
'retrieve' => array(
'help' => 'Retrieve a node with images with absolute path',
'file' => array('type' => 'inc', 'module' => 'services', 'name' => 'resources/node_resource'),
'callback' => '_waterbed_rooms_node_resource_retrieve',
'args' => array(
array(
'name' => 'nid',
'optional' => FALSE,
'source' => array('path' => 0),
'type' => 'int',
'description' => 'The nid of the node to retrieve',
),
),
'access callback' => '_node_resource_access',
'access arguments' => array('view'),
'access arguments append' => TRUE,
)
),
),
);
return $node_resource;
}
/**
* Returns the results of a node_load() for the specified node.
*
* This returned node may optionally take content_permissions settings into
* account, based on a configuration setting.
*
* @param $nid
* NID of the node we want to return.
* @return
* Node object or FALSE if not found.
*
* @see node_load()
*/
function _waterbed_rooms_node_resource_retrieve($nid) {
$node = node_load($nid);
$owner = user_load($node->uid);
if ($node) {
$uri = entity_uri('node', $node);
$node->path = url($uri['path'], array('absolute' => TRUE));
// Unset uri as it has complete entity and this
// cause never ending recursion in rendering.
unset($node->uri);
// Check if node has pictures field
if(property_exists($node, 'field_pictures')) {
foreach ($node->field_pictures[LANGUAGE_NONE] as $key => $picture) {
$node->field_pictures[LANGUAGE_NONE][$key]['uri'] = file_create_url($picture['uri']);
}
}
// Set owner information.
$node->owner_picture = image_style_url('thumbnail', $owner->picture->uri);
$node->owner_member_since = date('M Y', $owner->created);
}
return $node;
}