-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAdoptDefaultsFromParents.module
115 lines (94 loc) · 3.66 KB
/
AdoptDefaultsFromParents.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
<?php // namespace ProcessWire;
/**
*
*/
class AdoptDefaultsFromParents extends WireData implements Module, ConfigurableModule {
public static function getModuleInfo() {
return array(
'title' => 'Adopt field values as defaults from parents',
'version' => 003,
'author' => 'Steffen Henschel',
'summary' => 'Fills fields of newly added pages with corresponding field-values of their parents. Configurable by setting "giving templates" and "adopting fields".',
'href' => '',
'autoload' => true,
'singular' => true
);
}
public function ready() {
$this->pages->addHookAfter('Pages::added', $this, 'adoptDefaults');
}
/**
* [adoptDefaults description]
* @param [type] $event [description]
* @return [type] [description]
*/
public function adoptDefaults($event) {
$page = $event->arguments(0);
$parent = $page->parent;
$adoptingFields = $this->adopting_fields; // array of field names
$givingTemplates = $this->giving_templates; // array of template ids
// save outputformatting state
// and set to false
$of = $page->of();
$page->of(false);
foreach ($page->data as $f => $fValue) {
// constrain to allowed adopting fields first
if (in_array($f, $adoptingFields)) {
switch ($this->data['parent_mode']) {
case 'immediate':
// field not empty & parent is allowed by template
if ($parent->$f != "" && in_array($parent->template->id, $givingTemplates)) {
$page->set($f, $parent->$f);
}
break;
case 'closest':
$closestParent = $page->closest("{$f}!=''");
// parent is allowed by template
if (get_class($closestParent) != "NullPage" && in_array($closestParent->template->id, $givingTemplates)) {
$page->set($f, $closestParent->$f);
}
break;
}
}
}
// save page
// and re-set of to initial state
$page->save();
$page->of($of);
}
public function getModuleConfigInputfields($data) {
$inputfields = new InputfieldWrapper();
$field = wire('modules')->get("InputfieldRadios");
$field->name = "parent_mode";
$field->label = __("Which parent?");
$field->description = __('If set to "Immediate parent", only the direct parents will be called. If set to "Closest parent" the next parent which has "that field" will be called.');
$field->addOption("immediate", __("Immediate parent"));
$field->addOption("closest", __("Closest parent"));
if (isset($data['parent_mode'])) {
$field->value = $data['parent_mode'];
} else {
$field->value = "immediate";
}
$inputfields->add($field);
$field = wire('modules')->get("InputfieldAsmSelect");
$field->name = "giving_templates";
$field->label = __("Giving templates");
$field->description = __("Choose those templates, which may give field values to their newly added children.");
foreach (wire('templates')->getAll() as $key => $template) {
$field->addOption($key, $template);
}
if (isset($data['giving_templates'])) $field->value = $data['giving_templates'];
$inputfields->add($field);
$field = wire('modules')->get('InputfieldAsmSelect');
$field->name = 'adopting_fields';
$field->label = __("Adopting fields");
$field->description = __("Fields that can adopt default values from parent template / page");
foreach (wire('fields')->getAll() as $f) {
$field->addOption($f->name, $f->title);
}
if(isset($data['adopting_fields'])) $field->value = $data['adopting_fields'];
$inputfields->add($field);
return $inputfields;
}
}
?>