-
Notifications
You must be signed in to change notification settings - Fork 2
/
printable.module
185 lines (171 loc) · 5.79 KB
/
printable.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
<?php
/**
* @file
* Provides printer friendly content entities.
*/
use Drupal\Component\Utility\String;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
use Drupal\Core\Template\Attribute;
use Drupal\entity\Entity\EntityDisplay;
use Symfony\Cmf\Component\Routing\RouteObjectInterface;
// Register autoloading of vendor libraries.
$autoload = __DIR__ . '/vendor/autoload.php';
if (file_exists($autoload)) {
require_once $autoload;
}
/**
* Implements hook_permission().
*/
function printable_permission() {
return array(
'view printer friendly versions' => array(
'title' => t('View Printer Friendly Versions'),
),
'administer printable' => array(
'title' => t('Administer Printable'),
),
);
}
/**
* Implements hook_theme().
*/
function printable_theme() {
$module_path = drupal_get_path('module', 'printable');
return array(
'printable_navigation' => array(
'variables' => array('printable_link' => NULL),
),
'printable' => array(
'template' => 'printable',
'pattern' => 'printable__',
'variables' => array(
'header' => NULL,
'content' => NULL,
'footer' => NULL,
),
'path' => $module_path . '/templates',
),
'printable_header' => array(
'template' => 'printable-header',
'pattern' => 'printable_header__',
'variables' => array(
'logo_url' => NULL,
),
'path' => $module_path . '/templates',
),
'printable_footer' => array(
'template' => 'printable-footer',
'pattern' => 'printable_footer__',
'variables' => array(
'footer_content' => NULL,
),
'path' => $module_path . '/templates',
),
);
}
/**
* Preprocess variables for list of printable printer friendly page.
*
* @param array $variables
* An associative array containing:
* - elements: Array of participant names.
* Array keys: #base_url, #title, #html_attributes, #send_script.
*/
function template_preprocess_printable(&$variables) {
global $base_url;
$config = \Drupal::config('printable.settings');
$variables['base_url'] = $base_url . '/' . drupal_get_path('module', 'printable');
$request = \Drupal::request();
if ($route = $request->attributes->get(RouteObjectInterface::ROUTE_OBJECT)) {
$title = \Drupal::service('title_resolver')->getTitle($request, $route);
}
$variables['title'] = $title;
$language_interface = \Drupal::languageManager()->getCurrentLanguage();
$variables['html_attributes'] = new Attribute(array(
'lang' => $language_interface->getId(),
'dir' => $language_interface->getDirection(),
));
if ($config->get('send_to_printer')) {
$variables['send_script'] = $variables['base_url'] . '/js/script.js';
if ($config->get('close_window')) {
$variables['close_script'] = $variables['base_url'] . '/js/close.js';
}
}
}
/**
* Preprocess variables for list of printable printer friendly page.
*
* @param array $variables
* An associative array containing
* - elements: Array of participant names.
* Array keys: #source_url, #footer_links, #footer_content.
*/
function template_preprocess_printable_footer(&$variables) {
global $base_url;
// Create source url over here.
$source_url = $base_url . \Drupal::service('path.current')->getPath();
$pos = strpos($source_url, "printable");
$pos_node = strpos($source_url, '/', $pos + 11);
$source_url = substr($source_url, 0, $pos) . substr($source_url, $pos_node + 1);
$variables['source_url'] = $source_url;
if (\Drupal::service('config.factory')->get('printable.settings')->get('list_attribute')) {
$links = $variables['footer_content'];
$split_links = explode(',', $links);
$new_array = array_unique($split_links);
$variables['footer_links'] = $new_array;
}
}
/**
* Implements hook_entity_view_mode_alter().
*/
function printable_entity_view_mode_info_alter(&$view_modes) {
$printable_manager = \Drupal::service('printable.entity_manager');
foreach ($printable_manager->getPrintableEntities() as $entity_type => $entity_definition) {
// Add an additional view mode to this entity.
$view_modes[$entity_type]['printable'] = array(
'label' => t('Printable'),
'custom_settings' => FALSE,
'cache' => TRUE,
);
}
}
/**
* Implements hook_entity_view().
*/
function printable_entity_view(array &$build, EntityInterface $entity, EntityViewDisplayInterface $display, $view_mode, $langcode) {
$config = \Drupal::config('printable.settings');
\Drupal::service('renderer')->addCacheableDependency($build, $config);
$printable_manager = \Drupal::service('printable.entity_manager');
$link_builder = \Drupal::service('printable.link_builder');
if ($printable_manager->isPrintableEntity($entity)) {
$print_setting = $config->get('printable_print_link_locations');
$pdf_setting = $config->get('printable_pdf_link_locations');
// Get the name of entity over here.
$entity_name = $printable_manager->getEntityName($entity);
$printable_navigation = array(
'#theme' => 'links__entity__printable',
'#links' => $link_builder->buildLinks($entity),
'#attributes' => array(
'class' => array('pre_links'),
),
);
if (!in_array($entity_name, $print_setting)) {
unset($printable_navigation['#links']['print']);
}
if (!in_array($entity_name, $pdf_setting)) {
unset($printable_navigation['#links']['pdf']);
}
// Add the build links to the entity being rendered.
$build['printable_navigation'] = array(
'#markup' => '<strong class="node_view">' . drupal_render($printable_navigation) . '</strong>',
'#attached' => array(
'library' => 'printable/entity-links',
),
'#weight' => 100,
'#cache' => [
'tags' => $entity->getEntityType()->getListCacheTags(),
],
);
}
}