-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtims.module
166 lines (147 loc) · 6.32 KB
/
tims.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
<?php
/**
* Implements hook_help().
*/
function tims_help($path, $arg) {
switch ($path) {
case 'admin/help#tims':
$output = '<h3>' . t('About') . '</h3>';
$output .= '<p>' . t('The TIMS module allows users with the <em>Administer site configuration</em> permission to create content templates within the browser using the Twig templating language.') . '</p>';
$output .= '<h3>' . t('Naming your templates') . '</h3>';
$output .= '<p>' . t('Naming convention is based on what is contained in the theme_get_suggestions array. For example, if you would like to template a node of the article content type, instead of creating a php file named <em>node--article.tpl.php</em> you will create a template in this module\'s interface called <em>node__article</em>. See <a href="@link">Working with template suggestions</a> for more about working with template suggestions and their naming convention.', array('@link' => url('https://drupal.org/node/223440'))) . '</p>';
$output .= '<h3>' . t('Twig syntax') . '</h3>';
$output .= '<p>' . t('For a guide to the Twig syntax, refer to the <a href="@link">documention for theming Drupal 8</a> which uses Twig for theming as part of core.', array('@link' => url('https://drupal.org/node/1906384'))) . '</p>';
$output .= '<h3>' . t('Differences between Drupal 8 theming and this module') . '</h3>';
$output .= '<dl>';
$output .= '<dt>' . t('Printing a field') . '</dt>';
$output .= '<dd>' . t('Drupal 8 Twig syntax needs to modified when printing a field. The variable needs to wrapped in the render function and the raw filter must be applied.') . '</dd>';
$output .= '<dd>' . t('Render the body field:');
$output .= '<br />' . t('Drupal 7 TIMS Module: ') . '{{ render(content.body)|raw }}';
$output .= '<br />' . t('Drupal8: ') . '{{ content.body }}' . '</dd>';
$output .= '<dd>' . t('Note that printing metadata such as the title or nid works as expected: ') . '{{ title }}' . '</dd>';
$output .= '</dl>';
return $output;
}
}
/**
* Implements hook_init().
*/
function tims_init() {
// Don't apply TWIG templates to admin pages.
if (path_is_admin(current_path())) {
return;
}
// We need to register our stuff after everything else, this is a bit of a hack to do so.
theme_get_registry();
$theme_registry_object = theme_get_registry(FALSE);
$tmp = &drupal_static('theme_get_registry');
$hooks = $tmp[1];
foreach (variable_get('tims_templates', array()) as $hook => $template) {
$info = array();
$theme_hook_original = $hook;
// Webform fix. With a hook like webform_form_4, $info['render element'] needs to be populated.
// Other types of hooks like node make better use of theme_hook_suggestions and
// merge $info from the main hook (node) and sub-hook (node__4) in theme().
if (!isset($hooks[$hook]) && substr($hook, 0, 7) == 'webform') {
// Iteratively strip everything after the last '_' delimiter, until an
// implementation is found. Webform uses a single '_' pattern.
while ($pos = strrpos($hook, '_')) {
$hook = substr($hook, 0, $pos);
if (isset($hooks[$hook])) {
break;
}
}
}
if (isset($hooks[$hook])) {
$safeKeys = array('type', 'render element', 'preprocess functions', 'process functions', 'variables');
$info = array_intersect_key($hooks[$hook], array_flip($safeKeys));
}
$info['function'] = 'tims_render';
$info['theme path'] = drupal_get_path('module', 'tims') . '/theme';
$info['process functions'][] = 'tims_set_template';
$theme_registry_object[$theme_hook_original] = $hooks[$hook] = $info;
}
}
/**
* @param $variables
* @param $hook
*
* Implementation of template_process.
* Used to determine which TWIG template to use when rendering the element.
*/
function tims_set_template(&$variables, $hook) {
$variables['#tims_template'] = $hook;
}
/**
* Renders a template using Twig.
*/
function tims_render($variables) {
require_once DRUPAL_ROOT . '/sites/all/libraries/Twig/lib/Twig/Autoloader.php';
Twig_Autoloader::register();
require_once drupal_get_path('module', 'tims') . '/tims_extension.php';
$templates = variable_get('tims_templates', array());
$template = NULL;
if (isset($variables['#tims_template'])) {
$hook = $variables['#tims_template'];
if (isset($templates[$hook])) {
$template = $templates[$hook];
}
}
foreach (array_reverse($variables['theme_hook_suggestions']) as $hook) {
if (array_key_exists($hook, $templates)) {
$template = $templates[$hook];
break;
}
}
if (!$template) {
watchdog('tims', 'Unable to find the correct TWIG Template for a hooked element.', array(), WATCHDOG_ERROR);
return;
}
$loader = new Twig_Loader_String();
$twig = new Twig_Environment($loader, array(
'debug' => TRUE,
'autoescape' => FALSE,
));
$twig->addExtension(new Twig_Extension_Debug());
$twig->addExtension(new Tims_Extension());
$output = $twig->render($template, $variables);
return $output;
}
/**
* Implements hook_menu().
*/
function tims_menu() {
$items = array();
$items['admin/structure/tims'] = array(
'title' => 'Twig templates',
'description' => 'Manage content templates using the Twig templating engine.',
'page callback' => 'drupal_get_form',
'page arguments' => array('tims_list'),
'access arguments' => array('administer site configuration'),
'type' => MENU_NORMAL_ITEM,
'file' => 'tims.admin.php',
);
$items['admin/structure/tims/create'] = array(
'title' => 'Create template',
'page callback' => 'drupal_get_form',
'page arguments' => array('tims_edit', '_new_'),
'access arguments' => array('administer site configuration'),
'type' => MENU_LOCAL_ACTION,
'file' => 'tims.admin.php',
);
$items['admin/structure/tims/%/edit'] = array(
'title' => 'Edit template',
'page callback' => 'drupal_get_form',
'page arguments' => array('tims_edit', 3),
'access arguments' => array('administer site configuration'),
'file' => 'tims.admin.php',
);
$items['admin/structure/tims/%/delete'] = array(
'title' => 'Delete template',
'page callback' => 'drupal_get_form',
'page arguments' => array('tims_delete', 3),
'access arguments' => array('administer site configuration'),
'file' => 'tims.admin.php',
);
return $items;
}