-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathInputfieldChosenSelect.module
executable file
·140 lines (112 loc) · 4.69 KB
/
InputfieldChosenSelect.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
<?php
/**
* InputfieldChosenSelect module
*
* Inputfield for the ProcessWire FieldtypePage. Progressively enhanced single
* value select.
*
* Copyright (C) 2015 Benjamin Milde
* Licensed under MIT License, see LICENSE.md
*
* http://www.kobrakai.de
*
* http://www.processwire.com
* http://www.ryancramer.com
*
*/
class InputfieldChosenSelect extends InputfieldSelect {
protected $chosenOptions = array();
protected $supportedAdminThemes = array(
"AdminThemeReno",
"AdminThemeDefault",
"AdminThemeUikit"
);
public static function getModuleInfo() {
return array(
'title' => __('Inputfield Chosen Select', __FILE__), // Module Title
'summary' => __('Inputfield for the ProcessWire FieldtypePage. Progressively enhanced single value select. IMPORTANT: Please note the change in the naming of the Inputfield', __FILE__), // Module Summary
'version' => 125,
);
}
public function init() {
parent::init();
wire('modules')->get('JqueryCore'); // required by jQuery UI
wire('modules')->get('JqueryUI');
$this->setChosenOption('width', "100%");
$this->setChosenOption('search_contains', 'true');
// TODO: remove this after a few versions
$pagefields = $this->fields->find("type=FieldtypePage");
foreach($pagefields as $pagefield){
if($pagefield->derefAsPage == 0 && $pagefield->inputfield === "InputfieldChosenSelect"){
$this->warning("The module has been updated to incorporate a single-select Inputfield, which led to a change to the previous Inputfield's name. You seem to still have fields with the old Inputfield selected. These will now show single selects, so please change them. Please make sure InputfieldChosenSelectMultiple is even installed, too.");
break;
}
}
foreach($pagefields as $pagefield){
if($pagefield->derefAsPage == 0 && $pagefield->inputfield === "InputfieldChosenSelect"){
$this->warning("Please update <a href='{$this->config->urls->admin}setup/field/edit?id={$pagefield->id}#InputfieldWrapper12'>{$pagefield->name}</a> to use InputfieldChosenSelectMultiple.", Notice::allowMarkup);
}
}
}
public function setChosenOption($key, $value) {
$this->chosenOptions[$key] = $value;
}
/**
* Called before render()
*
* @param Inputfield $parent
* @param bool $renderValueMode
* @return bool
*
*/
public function renderReady(Inputfield $parent = null, $renderValueMode = false) {
$class = $this->className();
$info = self::getModuleInfo();
$ver = $info['version'];
$this->config->scripts->add($this->config->urls->$class . "chosen/chosen.jquery.min.js?v=$ver");
$this->config->styles->add($this->config->urls->$class . "chosen/chosen.min.css?v=$ver");
// Special styles per admin theme
if(in_array($this->adminTheme, $this->supportedAdminThemes)){
$this->config->styles->add($this->config->urls->$class . $this->adminTheme . ".css?v=$ver");
}
if(!$this->required) $this->setChosenOption('allow_single_deselect', 'true');
if($this->placeholder) $this->setChosenOption('placeholder_text_single', $this->placeholder);
if($this->noresult) $this->setChosenOption('no_results_text', $this->noresult);
$this->config->js($this->id, $this->chosenOptions);
return parent::renderReady($parent, $renderValueMode);
}
public function ___install() {
$data = $this->modules->getModuleConfigData("InputfieldPage");
array_push($data["inputfieldClasses"], $this->className());
$this->modules->saveModuleConfigData("InputfieldPage", $data);
}
public function ___uninstall() {
$fields = wire('fields')->find("type=FieldtypePage");
foreach($fields as $field){
if($field->inputfield !== $this->className()) $fields->remove($field);
}
$this->warning($this->className()." was used in following fields: ".$fields->implode(", ", "name"));
$data = $this->modules->getModuleConfigData("InputfieldPage");
if(in_array($this->className(), $data["inputfieldClasses"])){
$key = array_search($this->className(), $data["inputfieldClasses"]);
unset($data["inputfieldClasses"][$key]);
$this->modules->saveModuleConfigData("InputfieldPage", $data);
}
}
public function ___getConfigInputfields() {
$inputfields = parent::___getConfigInputfields();
$f = wire('modules')->get('InputfieldText');
$f->attr('name', 'placeholder');
$f->attr('value', $this->placeholder);
$f->label = $this->_('Placeholder Text');
$f->description = $this->_('Placeholder-text to be displayed in empty field.');
$inputfields->add($f);
$f = wire('modules')->get('InputfieldText');
$f->attr('name', 'noresult');
$f->attr('value', $this->noresult);
$f->label = $this->_('No result');
$f->description = $this->_('Message to display if no result is found.');
$inputfields->add($f);
return $inputfields;
}
}