-
Notifications
You must be signed in to change notification settings - Fork 0
/
taxonomy_breadcrumb.module
155 lines (144 loc) · 6.37 KB
/
taxonomy_breadcrumb.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
<?php
/**
* @file
* Generates taxonomy based breadcrumbs on node pages and taxonomy/term pages.
*
* The breadcrumb trail takes on the form:
* [HOME] >> [VOCABULARY] >> TERM >> [TERM] ...
*
* - The HOME breadcrumb (if present) links to the homepage. The text
* displayed for HOME is administrator configurable. If the HOME
* breadcrumb is not defined by the administrator, it will not appear
* in the breadcrumb trail.
* - The VOCABULARY breadcrumb (if present) will link to an administrator
* defined page. If the VOCABULARY does not have an administrator
* defined page, it will not appear in the breadcrumb trail.
* - Each TERM breadcrumb will link to either
* (1) taxonomy/term/tid by default, or
* (2) an administrator defined page if one is defined for the term.
* - These administrator defined "breadcrumb links" for VOCABULARIES and TERMS
* are controlled from the add/edit vocabulary and add/edit term
* administrator pages.
*
* Examples:
* home >> term >> term
* mysite >> term >> term
* home >> vocabulary >> term >> term
* vocabulary >> term >> term
*/
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Url;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
/**
* Implements hook_help().
*/
function taxonomy_breadcrumb_help($path) {
switch ($path) {
case 'admin/help#taxonomy_breadcrumb':
$output = '<p>' . t('The taxonomy_breadcrumb module generates taxonomy based breadcrumbs on node pages and taxonomy/term pages. The breadcrumb trail takes on the form:') . '</p>';
$output .= '<p>' . t('[HOME] >> [VOCABULARY] >> TERM >> [TERM] ...') . '</p>';
$output .= '<ul>';
$output .= '<li>' . t('The text displayed for HOME is configurable below. The <em>HOME</em> breadcrumb (if present) links to the homepage. The text displayed for HOME is configurable by an administrator. If the HOME breadcrumb is not defined by the administrator, it will not appear in the breadcrumb trail.') . '</li>';
$output .= '<li>' . t('The <em>VOCABULARY</em> breadcrumb (if present) will link to an administrator defined page. If the VOCABULARY does not have an administrator defined page, it will not appear in the breadcrumb trail. This can be configured on the add/edit vocabulary pages within <a href="@taxonomy">Structure >> Taxonomy</a>', array('@taxonomy' => Url::fromRoute('entity.taxonomy_vocabulary.collection'))) . '</li>';
$output .= '<li>' . t('Each <em>TERM</em> breadcrumb will link to either (1) taxonomy/term/tid by default, or (2) an administrator defined page if one is defined for the term. This can be configured on the add/edit term pages within <a href="@taxonomy">Structure >> Taxonomy</a>', array('@taxonomy' => Url::fromRoute('entity.taxonomy_vocabulary.collection'))) . '</li>';
$output .= '</ul>';
$output .= '<h3>' . t('Examples:') . '</h3>';
$output .= '<ul>';
$output .= '<li>' . t('home >> term >> term') . '</li>';
$output .= '<li>' . t('mysite >> term >> term') . '</li>';
$output .= '<li>' . t('home >> vocabulary >> term >> term') . '</li>';
$output .= '<li>' . t('vocabulary >> term >> term') . '</li>';
$output .= '</ul>';
return $output;
}
}
/**
* Implements hook_ENTITY_TYPE_alter().
*/
function taxonomy_breadcrumb_entity_type_alter(array &$entity_types) {
$entity_types['taxonomy_vocabulary']->setHandlerClass('form',
[
'default' => 'Drupal\taxonomy_breadcrumb\VocabularyForm',
'reset' => 'Drupal\taxonomy\Form\VocabularyResetForm',
'delete' => 'Drupal\taxonomy\Form\VocabularyDeleteForm',
]);
}
/**
* Implements hook_ENTITY_TYPE_update().
*/
function taxonomy_breadcrumb_taxonomy_vocabulary_update(EntityInterface $entity) {
if (!FieldConfig::loadByName('taxonomy_term', $entity->id(), 'taxonomy_breadcrumb_path')) {
FieldConfig::create(array(
'field_name' => 'taxonomy_breadcrumb_path',
'entity_type' => 'taxonomy_term',
'bundle' => $entity->id(),
'label' => t('Breadcrumb path (taxonomy_breadcrumb)'),
'description' => t('The image of this term.'),
'required' => FALSE,
))->save();
}
}
/**
* Implements hook_ENTITY_TYPE_insert().
*/
function taxonomy_breadcrumb_taxonomy_vocabulary_insert(EntityInterface $entity) {
if (!FieldConfig::loadByName('taxonomy_term', $entity->id(), 'taxonomy_breadcrumb_path')) {
FieldConfig::create(array(
'field_name' => 'taxonomy_breadcrumb_path',
'entity_type' => 'taxonomy_term',
'bundle' => $entity->id(),
'label' => t('Breadcrumb path (taxonomy_breadcrumb)'),
'description' => t('The image of this term.'),
'required' => FALSE,
))->save();
}
}
/**
* Implements hook_ENTITY_TYPE_delete().
*/
function taxonomy_breadcrumb_taxonomy_vocabulary_delete(EntityInterface $entity) {
if (FieldStorageConfig::loadByName('taxonomy_term', 'taxonomy_breadcrumb_path')) {
FieldConfig::loadByName('taxonomy_term', $entity->id(), 'taxonomy_breadcrumb_path')->delete();
}
}
/**
* Implements hook_ENTITY_TYPE_update().
*/
function taxonomy_breadcrumb_taxonomy_term_update(EntityInterface $entity) {
$bundle = $entity->bundle();
if (!FieldConfig::loadByName('taxonomy_term', $bundle, 'taxonomy_breadcrumb_path')) {
FieldConfig::create(array(
'field_name' => 'taxonomy_breadcrumb_path',
'entity_type' => 'taxonomy_term',
'bundle' => $bundle,
'label' => t('Breadcrumb path (taxonomy_breadcrumb)'),
'description' => t('The image of this term.'),
'required' => FALSE,
))->save();
}
}
/**
* Implements hook_ENTITY_TYPE_insert().
*/
function taxonomy_breadcrumb_taxonomy_term_insert(EntityInterface $entity) {
$bundle = $entity->bundle();
if (!FieldConfig::loadByName('taxonomy_term', $bundle, 'taxonomy_breadcrumb_path')) {
FieldConfig::create(array(
'field_name' => 'taxonomy_breadcrumb_path',
'entity_type' => 'taxonomy_term',
'bundle' => $bundle,
'label' => t('Breadcrumb path (taxonomy_breadcrumb)'),
'description' => t('The image of this term.'),
'required' => FALSE,
))->save();
}
}
/**
* Implements hook_ENTITY_TYPE_delete().
*/
function taxonomy_breadcrumb_taxonomy_term_delete(EntityInterface $entity) {
if (FieldStorageConfig::loadByName('taxonomy_term', 'taxonomy_breadcrumb_path')) {
FieldConfig::loadByName('taxonomy_term', $entity->bundle(), 'taxonomy_breadcrumb_path')->delete();
}
}