Newer
Older
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
<?php
/**
* Holds hooks and general functionality.
*/
/**
* Implements hook_help().
*/
function domain_restriction_formatter_help($path, $arg) {
switch ($path) {
case 'admin/help#domain_restriction_formatter':
$output = file_get_contents(drupal_get_path('module', 'domain_restriction_formatter') . '/README.txt');
return module_exists('markdown') ? module_invoke('markdown', 'filter', 'process', 0, -1, $output) : '<pre>' . $output . '</pre>';
}
}
/**
* Implements hook_field_formatter_info_alter().
*/
function domain_restriction_formatter_field_formatter_info_alter(&$info) {
foreach ($info as &$formatter) {
$formatter['settings']['domain_restriction_formatter'] = '';
}
}
/**
* Implements hook_field_formatter_settings_summary_alter().
*/
function domain_restriction_formatter_field_formatter_settings_summary_alter(&$summary, $context) {
$display = $context['instance']['display'][$context['view_mode']];
$settings = $display['settings'];
$restrictions = array();
if (!empty($summary)) {
$summary .= '<br />';
}
// Check if there are domain restrictions.
if (!empty($settings['domain_restriction_formatter'])) {
$restrictions = array_filter($settings['domain_restriction_formatter']);
}
if (!empty($restrictions)) {
$summary .= t('<strong>There are domain restrictions in place.</strong>');
}
else {
$summary .= t('No domain restrictions.');
}
}
/**
* Implements hook_field_formatter_settings_form_alter().
*/
function domain_restriction_formatter_field_formatter_settings_form_alter(&$settings_form, $context) {
$display = $context['instance']['display'][$context['view_mode']];
$settings = $display['settings'];
// Render checkboxes, so the user can decide on which domain(s) the field has to be show.
$domains = domain_domains();
$options = array();
foreach ($domains as $key => $domain) {
$options[$key] = $domain['subdomain'];
}
$settings_form['domain_restriction_formatter'] = array(
'#type' => 'checkboxes',
'#title' => 'Domains',
'#options' => $options,
'#default_value' => $settings['domain_restriction_formatter'],
'#description' => t('Limit the display of this field to the selected domains. If none are selected the field will be visible on all domains.'),
);
}
/**
* Implements hook_preprocess_field().
*
* Unsets items if domain restrictions are in place.
*/
function domain_restriction_formatter_preprocess_field(&$variables, $hook) {
$entity_type = $variables['element']['#entity_type'];
$field_name = $variables['element']['#field_name'];
$bundle = $variables['element']['#bundle'];
$view_mode = $variables['element']['#view_mode'];
// Get the formatter info of the field.
$formatter_info = field_formatter_settings_get_instance_display_settings($entity_type,
$field_name, $bundle, $view_mode);
if (!empty($formatter_info['domain_restriction_formatter'])) {
$current_domain = domain_get_domain();
$restrictions = array_filter($formatter_info['domain_restriction_formatter']);
// If there are any domain restrictions, apply them.
if (!empty($restrictions) && empty($formatter_info['domain_restriction_formatter'][$current_domain['domain_id']])) {
$variables['items'] = array();
$variables['label_hidden'] = TRUE;
}
}
}