Repository navigation
Expand file tree
/
Copy pathPageUseIdAsName.module.php
More file actions
107 lines (94 loc) · 3.61 KB
/
Copy pathPageUseIdAsName.module.php
File metadata and controls
107 lines (94 loc) · 3.61 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
<?php
namespace Processwire;
class PageUseIdAsName extends WireData implements Module, ConfigurableModule
{
public static function getModuleInfo()
{
return array(
'title' => 'PageUseIdAsName',
'class' => 'PageUseIdAsName',
'version' => 120,
'summary' => 'Overrides the name field (used for url) with the page-id and hides the field from the admin GUI',
'singular' => true,
'autoload' => 'template=admin',
'requires' => [
'PHP>=7.4.0 || ^8.0',
'ProcessWire>=3.0.148'
]
);
}
public function init()
{
$this->pages->addHookAfter('added', $this, 'changePageName');
$this->addHookAfter('AdminTheme::getExtraMarkup', $this, 'hideNameField');
}
public function hideNameField(HookEvent $event)
{
$parts = $event->return;
$fieldName = 'page_use_id_as_name_templates';
$allowedTemplates = $this->get($fieldName);
if($allowedTemplates && !empty($allowedTemplates)) {
$allowedTemplates = wire('templates')->find("id=" . implode('|', $allowedTemplates));
if ($allowedTemplates && $allowedTemplates->count() > 0) {
$cssSelectors = [];
foreach ($allowedTemplates as $template) {
array_push($cssSelectors, ".ProcessPageEdit-template-$template->name .InputfieldPageName");
}
if(count($cssSelectors) > 0) {
$parts['head'] .= '<style type="text/css">' . implode(',', $cssSelectors) . ' { display: none; }</style>';
}
}
}
$event->return = $parts;
}
public function changePageName(HookEvent $event)
{
$page = $event->arguments[0];
if ($this->isAllowedTemplate($page->template)) {
$id = wire('sanitizer')->pageName($page->id);
if (wire('languages')) {
foreach (wire('languages') as $language) {
if ($language->isDefault()) {
$page->name = $id;
} else {
// activate language
$page->set("status$language->id", 1);
// set pageid
$page->set("name$language->id", $id);
}
}
} else {
$page->name = $id;
}
$page->save([
"quiet" => true,
"noHooks" => true
]);
}
}
public static function getModuleConfigInputfields(array $data)
{
$wrapper = new InputfieldWrapper();
$fieldName = 'page_use_id_as_name_templates';
$templates = [];
foreach (wire('templates') as $template) {
// dont add system templates
if ($template->flags == Template::flagSystem) continue;
$templates[$template->id] = $template->name;
}
$field = wire('modules')->get('InputfieldAsmSelect');
$field->label = __('Templates');
$field->description = __('Choose for which templates the rule should be applied.');
$field->attr('name', $fieldName);
$field->attr('value', $data[$fieldName] ?? null);
$field->addOptions($templates);
$wrapper->append($field);
return $wrapper;
}
protected function isAllowedTemplate(Template $template)
{
$fieldName = 'page_use_id_as_name_templates';
$allowedTemplates = $this->get($fieldName);
return is_array($allowedTemplates) && in_array($template->id, $allowedTemplates);
}
}