forked from ueberbit/drupal-micro
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmicro.module
More file actions
146 lines (130 loc) · 3.78 KB
/
micro.module
File metadata and controls
146 lines (130 loc) · 3.78 KB
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
<?php
use Drupal\Component\Utility\Xss;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Render\Element;
use Drupal\Core\Url;
/**
* Entity URI callback.
*
* @param \Drupal\Core\Entity\EntityInterface $micro
* A micro entity.
*
* @return array
* An array with 'path' as the key and the path to the micro as its value.
*/
function micro_uri(EntityInterface $micro) {
return array(
'path' => 'micro/' . $micro->id(),
);
}
/**
* Implements hook_permission().
*/
function micro_permission() {
$permissions['administer micro types'] = array(
'title' => t('Administer micro types'),
'description' => t('View, edit and delete all micro types.'),
);
$permissions['administer micro content'] = array(
'title' => t('Administer micro content'),
'description' => t('View, edit and delete all micro content.'),
);
return $permissions;
}
/**
* Implements hook_theme().
*/
function micro_theme() {
return array(
'micro' => array(
'render element' => 'elements',
'template' => 'micro',
),
'field__micro__title' => array(
'base hook' => 'field',
),
'micro_add_list' => [
'variables' => ['micro' => NULL],
'template' => 'micro-add-list',
],
);
}
/**
* Prepares variables for micro templates.
*
* Default template: micro.html.twig.
*
* @param array $variables
* An associative array containing:
* - elements: An array of elements to display in view mode.
* - node: The micro object.
* - view_mode: View mode; e.g., 'full', 'teaser'...
*/
function template_preprocess_micro(&$variables) {
$variables['view_mode'] = $variables['elements']['#view_mode'];
// Provide a distinct $teaser boolean.
$variables['teaser'] = $variables['view_mode'] == 'teaser';
$variables['micro'] = $variables['elements']['#micro'];
$variables['label'] = $variables['elements']['title'];
unset($variables['elements']['title']);
// Helpful $content variable for templates.
$variables += array('content' => array());
foreach (Element::children($variables['elements']) as $key) {
$variables['content'][$key] = $variables['elements'][$key];
}
}
/**
* Implements hook_theme_suggestions_HOOK().
*/
function micro_theme_suggestions_micro(array $variables) {
$suggestions = array();
$micro = $variables['elements']['#micro'];
$suggestions[] = 'micro__' . $micro->bundle();
$suggestions[] = 'micro__' . $micro->bundle() . '__' . $variables['elements']['#view_mode'];
return $suggestions;
}
/**
* Returns HTML for the micro title field.
*
* This is an override of theme_field() for the micro title field. See that
* function for documentation about its details and overrides.
*
* @param array $variables
* An associative array. See theme_field() for details.
*
* @see theme_field()
*
* @ingroup themeable
*/
function theme_field__micro__title($variables) {
return drupal_render($variables['items']);
}
/**
* Implements hook_entity_type_alter().
*/
function micro_entity_type_alter(&$entity_types) {
/**
* @var \Drupal\Core\Entity\EntityType[] $entity_types
*/
$entity_types['micro']->setFormClass('inline entity form', '\Drupal\micro\Plugin\InlineEntityForm\MicroInlineEntityFormController');
}
/**
* Prepares variables for list of available micro type templates.
*
* Default template: micro-add-list.html.twig.
*
* @param array $variables
* An associative array containing:
* - content: An array of micro types.
*/
function template_preprocess_micro_add_list(&$variables) {
$variables['types'] = array();
if (!empty($variables['micro'])) {
foreach ($variables['micro'] as $type) {
$variables['types'][$type->type] = array(
'type' => $type->type,
'add_link' => \Drupal::l($type->name, Url::fromRoute('micro.add', ['micro_type' => $type->type])),
);
}
}
}