-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFieldtypePageContextData.module
More file actions
executable file
·665 lines (594 loc) · 26.8 KB
/
FieldtypePageContextData.module
File metadata and controls
executable file
·665 lines (594 loc) · 26.8 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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
<?php namespace ProcessWire;
/**
* ProcessWire Page Fieldtype with Context Data
*
* Field that stores one or more references to ProcessWire pages with additional data in field context
*
* @author Christoph Thelen aka @kixe 2021/06/07
* @copyright © 2020 Christoph Thelen
* @license Licensed under GNU/GPL v3
* @link https://processwire.com/talk/topic/...
* @version 1.0.8 BETA
*
* @since 1.0.0 2021/06/07 - init
* @since 1.0.2 2021/09/28 - fixed bug: separator for options not compatible with windows
* @since 1.0.3 2022/09/16 - fixed bug: database scheme column data_context should allow NULL values, default modal title include fallback to name if title is not set
* @since 1.0.4 2022/09/16 - fixed bug: make language specific labels idependent from order in setup
* @since 1.0.5 2023/03/21 - fixed bug: assign user language for tab label and modal title
* @since 1.0.6 2023/04/24 - fixed bug: Fatal Error: Nesting level too deep
* @since 1.0.7 2025/02/20 - fixed bug: Loss of context data when cloning
* @since 1.0.8 2025/04/22 - fixed bug: skipping of Inputfield settings 'labelFieldName' and 'labelFieldFormat'
*
* made for ProcessWire 3.x, by Ryan Cramer
* https://processwire.com
*
*
*/
class FieldtypePageContextData extends FieldtypePage {
public static function getModuleInfo() {
return array(
'title' => 'Page Reference Context Data',
'version' => 108,
'summary' => 'Field that stores one or more references to ProcessWire pages with additional data in field context, editable via page edit modal of the referenced page.',
'requires' => ['ProcessWire>=3.0.5, AdminPageFieldEditLinks >=3.1.4']
);
}
protected static function getDefaultModalTitle() {
return __('Edit Page "{title|name}" [#{id}] with additional values related to Pagefield: "{forfield.name}" of page "{forpage.title|forpage.name}" [#{forpage.id}]');
}
protected static function getDefaultContextTabLabel() {
return __("Pagefield Context Data");
}
/**
* Init (populate default values)
*
*/
public function init() {
$url = wire('config')->urls->get($this->className());
wire('config')->scripts->add($url . 'FieldtypePageContextData.js');
// if page (value to which the context values are assigned) is saved from modal
$this->addHookBefore('Pages::saveReady', function($e) {
$page = $e->arguments[0];
$forPage = (int) wire('input')->post->forpage;
$forField = wire('sanitizer')->fieldName(wire('input')->post->forfield);
// quick exit
$for = $this->getPageAndField($forPage, $forField);
if (!$for) return;
list($fp, $ff) = $for;
$inputfield = $ff->getInputfield($page)->getInputfield();
$multiple = $inputfield instanceof InputfieldSelectMultiple;
// item already selected?
$item = $fp->{"$forField"}->findOne($page->id);
// assign runtime context data to page (value) if present
if (wire('input')->post) {
foreach (wire('input')->post as $key => $val) {
$prefix = "_{$forPage}_{$forField}";
// ignore other than context data fields
if (strpos($key, $prefix) !== 0) continue;
// language value
if (strpos($key, '__')) $key = str_replace('__', '', $key);
// assign to page object for later storage
$page->$key = $val;
}
}
// if page context data should be saved via modal the page MUST be selected/ added to the field
if (!$item && !$multiple) $forPage->{"$forField"} = new PageArray();
if (!$item) $fp->{"$forField"}->add($page);
// save forpage with context data
$fp->save($forField);
});
$this->addHookAfter('ProcessPageEdit::buildForm', function ($event) {
if (!wire('config')->modal) return;
$forPage = (int) wire('input')->get('forpage');
$forField = wire('sanitizer')->fieldName(wire('input')->get('forfield'));
// quick exit
$for = $this->getPageAndField($forPage, $forField);
if (!$for) return;
list($fp, $ff) = $for;
// call wakeupValue() to assign values
$fp->{"$forField"};
$page = $event->object->getPage();
// get language
$langID = '';
if (wire('languages')) {
if (!wire('user')->language->isDefault()) $langID = (string) wire('user')->language;
}
// modal title
if ($ff->{"modalTitle$langID"}) $mt = $ff->{"modalTitle$langID"};
else if ($ff->modalTitle) $mt = $ff->modalTitle;
else $mt = self::getDefaultModalTitle();
$vars = clone $page;
$vars->forpage = $fp;
$vars->forfield = $ff;
$event->object->browserTitle(wirePopulateStringTags($mt, $vars));
$form = $event->return;
// context tab label
$inputfields = $this->getContextDataInputfields($ff, $page, $fp);
if ($ff->{"contextTabLabel$langID"}) $cl = $ff->{"contextTabLabel$langID"};
else if ($ff->contextTabLabel) $cl = $ff->contextTabLabel;
else $cl = self::getDefaultContextTabLabel();
$inputfields->attr('title', $cl);
$inputfields->attr('name+id', "PageContextDataInputfields"); // we need both unique ID and Name
$pageEditTab = $form->find('id=ProcessPageEditContent')->first();
$form->insertAfter($inputfields, $pageEditTab);
// inserting in the right place is not enough to set the tab order
// we need the following hook
$this->addHookAfter('ProcessPageEdit::getTabs', function ($event) use ($ff, $fp, $cl) {
$event->return = array_merge(
array_slice($event->return, 0, 1, true),
array('PageContextDataInputfields' => $cl), // should be identical to any weird id/name/title set above
array_slice($event->return, 1, null, true)
);
});
// always provide forpage and forfield in form action url
$form->attr('action', $event->object->getEditUrl(array('id' => $page->id, 'forpage' => $forPage, 'forfield' => $forField)));
// modify labels in modal, add 'Select'
if($ff->allowUnpub || !$page->hasStatus(Page::statusUnpublished)) {
$labelSave = $form->submit_save->attr('value');
$form->submit_save->attr('value', $labelSave . ' + ' . $this->_('Select'));
}
if($ff->allowUnpub && $page->hasStatus(Page::statusUnpublished)) {
$labelPublish = $form->submit_publish->attr('value');
if (!empty($labelPublish) && wire('session')->get('clientWidth') > 900) $form->submit_publish->attr('value', $labelPublish . ' + ' . $this->_('Select'));
} else if (!$ff->allowUnpub && $page->hasStatus(Page::statusUnpublished)) {
$this->warning(sprintf($this->_('Unpublished pages cannot be selected in the Pagefield: "%1$s". Values entered in context of this Pagefield under the tab "%2$s" are not saved.'), $forField, self::getDefaultContextTabLabel()));
}
$event->return = $form;
});
/**
* Hook ProcessPageEdit::processSaveRedirect to keep forpage and forfield in case of redirect (e.g. field error)
*
*/
$this->addHookBefore('ProcessPageEdit::processSaveRedirect', function ($event) {
if (!wire('config')->modal) return;
$forPage = (int) wire('input')->get('forpage');
$forField = wire('sanitizer')->fieldName(wire('input')->get('forfield'));
// quick exit
$for = $this->getPageAndField($forPage, $forField);
if (!$for) return;
$redirectUrl = $event->arguments[0];
if($redirectUrl) {
$c = substr($redirectUrl, 0, 1);
$admin = $c === '.' || $c === '?' || strpos($redirectUrl, $this->config->urls->admin) === 0;
if($admin) $redirectUrl .= (strpos($redirectUrl, '?') === false ? '?' : '&') . "forpage=$forPage&forfield=$forField";
}
if(!$redirectUrl) $redirectUrl = $this->getEditUrl(array('forpage' => $forPage, 'forfield' => $forField));
$event->arguments('redirectUrl', $redirectUrl);
});
}
/**
* Given a raw value (value as stored in DB), return the value as it would appear in a Page object
*
* @param Page $page
* @param Field $field
* @param string|int|array $value
* @return string|int|array|object $value
*
* @example value format with 'data_context' as json string
* array(1) {
* [0]=>
* array(2) {
* ["data"] => string(2) "27",
* ["data_context"] => string(1) {"text":"Hello World"}
* }
* }
*
*/
public function ___wakeupValue(Page $page, Field $field, $value) {
if (empty($value)) return $this->getBlankValue($page, $field);
if (is_array($value) && is_array($value[0])) {
$contextValues = array_column($value, 'data_context', 'data');
$pageArray = parent::___wakeupValue($page, $field, array_column($value,'data'));
foreach ($pageArray as $_page) {
if (!empty($contextValues[$_page->id])) {
$inputfields = $this->getContextDataInputfields($field, $_page, $page);
$values = json_decode($contextValues[$_page->id], true);
foreach ($inputfields as $inputfield) {
$k = $inputfield->contextDataField;
$v = !empty($values[$k])? $values[$k] : null;
if ($inputfield instanceof InputfieldPage) {
$options = [];
if ($inputfield->parent_id) $options['parent'] = $inputfield->parent_id;
if ($inputfield->template_id) $options['template'] = $inputfield->template_id;
if (!is_array($v)) {
$options['getOne'] = true;
}
$v = $this->wire()->pages->getByIDs($v, $options);
}
if ($inputfield instanceof InputfieldCheckbox) $v = (int) $v;
// multilanguage support
if ($inputfield->useLanguages) {
$langValue = [];
foreach ($this->wire()->languages as $language) {
if (!$language->isDefault()) {
$lv = empty($values["$k$language->id"])? '' : $values["$k$language->id"];
$_page->{"$inputfield->name$language->id"} = $lv;
$langValue["$language->id"] = $lv;
}
else $langValue["$language->id"] = (string) $v;
}
$v = new LanguagesPageFieldValue($_page, new Field(), $langValue);
}
// assign runtime values
$_page->set("_$k", $v); // not safe – can be overwritten, if multiple fields of this type are in use and context field names are not unique
$_page->set($inputfield->name, $v); // safe
}
}
}
return $pageArray;
} else throw new WireException('Wrong data format. Unable to assign values.');
}
/**
* Given an 'awake' value, as set by wakeupValue, convert the value back to a basic type for storage in DB.
*
* @param Page $page
* @param Field $field
* @param string|int|array|object $value
* @return array
*
*/
public function ___sleepValue(Page $page, Field $field, $value) {
if($field->hasContext($page)) $field = $field->getContext($page);
$sleepValue = array();
$forpageID = $page->id;
// cloning action? retrieve context values from previously assigned runtime value
if ($page->get('_cloning') && $page->get('_cloning') instanceof Page) {
$forpageID = $page->get('_cloning')->id;
}
if($field->get('derefAsPage') > 0) {
// if the $value isn't specifically a Page, make it a blank array for storage
if(!$value instanceof Page || !$value->id) return $sleepValue;
// if $value is a Page (not a NullPage) then place it's ID in an array for storage
$sleepValue[] = ['data' => $value->id,'data_context' => $this->getContextDataString($value, $forpageID, $field->name)];
} else {
// if $value isn't a PageArray then we'll store a blank array
if(!$value instanceof PageArray) return $sleepValue;
// iterate through the array and place each Page ID
foreach($value as $pg) {
if(!$pg->id) continue;
$sleepValue[] = ['data' => $pg->id,'data_context' => $this->getContextDataString($pg, $forpageID, $field->name)];
}
}
return $sleepValue;
}
/**
* validate input, get Page and Field instance
*
* @param numeric $pageID – page where the field lives in
* @param string $fieldname
* @return bool|array
*
*/
protected function getPageAndField($pageID = 0, $fieldname = '') {
// quick exit
if (empty($pageID) && empty($fieldname)) return false;
$page = wire('pages')->get($pageID);
// $page does not exist
if (!$page->id) return false;
// $field not part of targetPage
if (!$page->hasField($fieldname)) return false;
// fieldtype does not match
$field = wire('fields')->get($fieldname);
$class = get_class($this);
if (!$field->type instanceof $class) return false;
return [$page, $field];
}
/**
* get Inputfields for Context Data
* also used as validator
*
* @param object Field
* @param object $page - value
* @param object Page - page where the field lives in
* @return object InputfieldWrapper
*
*/
protected function getContextDataInputfields(Field $field, Page $page = null, Page $forPage = null) {
if (!empty($_POST['contextDataFields'])) {
$field->contextDataFields = $_POST['contextDataFields'];
}
$class = get_class($this);
if (!$field->type instanceof $class) throw new WireException('Expected field of type FieldtypePageContextData.');
// if (empty($field->contextDataFields)) throw new WireException('Field settings error. context data fields missing.');
$errorPrefix = $this->_('Syntax error in field settings (context data fields). ');
$inputfields = new InputfieldWrapper();
$contextDataField = strtok($field->contextDataFields, "\r\n");
$names = [];
$line = 0;
$userLanguage = wire()->user->language;
while ($contextDataField !== false) {
$line++;
$settings = null;
// missing colon
if (!strpos($contextDataField, ':')) {
$this->error($errorPrefix . sprintf($this->_('Use colon as separator between field name and Inputfield. Line %1$s'), $line));
$contextDataField = strtok("\r\n");
continue;
}
// split by first occurence of colon
$name = trim(strstr($contextDataField,':', true));
if (in_array($name, $names)) {
$this->error($errorPrefix . sprintf($this->_('Duplicate field name: "%1$s". Use unique field names. Line %2$s'), $name, $line));
$contextDataField = strtok("\r\n");
continue;
}
$names[] = $name;
$type = trim(strstr($contextDataField,':'),' :');
// Inputfieldtype not defined
if (!strlen($type)) {
$this->error($errorPrefix . sprintf($this->_('Inputfield not defined for "%1$s". Line %2$s'), $name, $line));
$contextDataField = strtok("\r\n");
continue;
}
if (strpos($type, ',')) {
// replace escaped commata with placeholder
if (strpos($type, '\,')) $type = str_replace('\,', '¿?', $type);
$settings = array_filter(explode(',', trim(strstr($type,','),' ,')));
$type = trim(strstr($type,',',true));
}
$inputfield = wire()->modules->get($type);
// Inputfieldtype does not exist
if (!$inputfield) {
$this->error($errorPrefix . sprintf($this->_('Inputfield class: "%1$s" does not exist. Line %2$s'), $type, $line));
$contextDataField = strtok("\r\n");
continue;
}
// Inputfieldtype not supported
if ($inputfield instanceof InputfieldFile) {
$this->error($errorPrefix . sprintf($this->_('Inputfield class: "%1$s" is not supported. Line %2$s'), $type, $line));
$contextDataField = strtok("\r\n");
continue;
}
// apply settings
if ($settings) {
// $languageLabel = null;
foreach ($settings as $setting) {
// split by first occurence of equal sign
$settingName = trim(strstr($setting,'=', true));
$settingValue = trim(strstr($setting,'='),' =');
// replace comma placeholders with comma
if (strpos($settingValue, '¿?')) $settingValue = str_replace('¿?', ',', $settingValue);
if ($inputfield instanceof InputfieldSelect && $settingName == 'options') {
$inputfield->addOptionsString(str_replace('\n', PHP_EOL, $settingValue));
continue;
} else if (!in_array($settingName, ['labelFieldName','labelFieldFormat']) && $this->wire('languages') && wire('user')->language->isDefault() == false) {
// assign label, description, notes in user language if set
$langID = 0;
if (strpos($settingName,'label') === 0 && strlen($settingName) > 5) {
$settingName = 'label';
$langID = (int) substr($settingName, 5);
} else if (strpos($settingName,'description') === 0 && strlen($settingName) > 11) {
$langID = (int) substr($settingName, 11);
$settingName = 'description';
} else if (strpos($settingName,'notes') === 0 && strlen($settingName) > 5) {
$langID = (int) substr($settingName, 5);
$settingName = 'notes';
}
if ($langID) {
$language = wire('languages')->get($langID);
if (!$language->id) continue;
if ($userLanguage !== $language) continue;
$inputfield->$settingName = trim($settingValue);
continue;
}
}
if (in_array($settingName, ['label','description','notes']) && $inputfield->$settingName) continue;
$inputfield->$settingName = trim($settingValue);
}
}
// default to prevent ugly Inputfield names e.g. _1234_contextfield_name
if (empty($inputfield->label)) $inputfield->label = $name;
// check support of Page Inputfields
if ($inputfield instanceof InputfieldPage) {
if (in_array($inputfield->inputfield, ['InputfieldTextTags','InputfieldPageAutocomplete'])) {
$this->error($errorPrefix . sprintf($this->_('Inputfield: "%1$s" not supported as Input for InputfieldPage "%2$s". Line %3$s'), $inputfield->inputfield, $name, $line));
$contextDataField = strtok("\r\n");
continue;
}
if (!$inputfield->labelFieldName) $inputfield->set('labelFieldName', 'title|name');
}
/*
// check naming of multilanguage inputfields
if ($inputfield->useLanguages && is_numeric(substr($name, -1))) {
$this->error($errorPrefix . sprintf($this->_('The name of a multilanguage field must not end with a number. Line %s'), $line));
$contextDataField = strtok("\r\n");
continue;
}
*/
// assign context data field name used by getMatchQuery()
$inputfield->contextDataField = strtolower(trim($name));
// assign values if present
if ($page && $forPage) {
$inputfield->attr('name+id', "_{$forPage->id}_{$field->name}_" . strtolower(trim($name)));
$value = $page->{"_{$forPage->id}_{$field->name}_" . strtolower(trim($name))};
if ($inputfield instanceof InputfieldCheckbox) {
$inputfield->attr('checked', $value ? 'checked' : '');
}
$inputfield->attr('value', $value);
if($this->wire('languages') && $inputfield->useLanguages) {
foreach($this->wire('languages') as $language) {
$key = $language->isDefault()? strtolower(trim($name)) : strtolower(trim($name)) . $language->id;
if ($value instanceof LanguagesPageFieldValue) {
$langValue = $value->getLanguageValue($language->id);
} else {
$langValue = $page->{"_{$forPage->id}_{$field->name}_$key"};
}
if(empty($langValue)) continue;
$inputfield->set('value' . $language->id, $langValue);
}
}
}
$inputfields->add($inputfield);
$contextDataField = strtok("\r\n");
}
// add hidden fields for page edit
if ($page && $forPage) {
$inputfield = wire()->modules->get('InputfieldHidden');
$inputfield->attr('name+id', 'forfield');
$inputfield->attr('value', $field->name);
$inputfields->add($inputfield);
$inputfield = wire()->modules->get('InputfieldHidden');
$inputfield->attr('name+id', 'forpage');
$inputfield->attr('value', $forPage->id);
$inputfields->add($inputfield);
}
return $inputfields;
}
/**
* get json encoded string of data array for storage
*
* @param Page $page
* @param numeric $forpage (page ID)
* @param string $forfield (field name)
* @return string
* @throws WireException
*
*/
protected function getContextDataString($page, $forpage, $forfield) {
$for = $this->getPageAndField($forpage, $forfield);
if (!$for) throw new WireException('Function getContextDataString() failed. Unable to determine page and field.');
list($fp, $ff) = $for;
$data = [];
$contextDataField = strtok($ff->contextDataFields, "\r\n");
while ($contextDataField !== false) {
$name = strtolower(trim(strstr($contextDataField,':',true)));
$type = trim(strstr($contextDataField,':'),' :');
if (strpos($type, ',')) {
$langField = strpos($type, 'useLanguages=1') !== false;
$type = trim(strstr($type,',',true));
}
$value = $page->{"_{$forpage}_{$forfield}_$name"};
if ($type == 'InputfieldPage' && ($value instanceof Page || $value instanceof PageArray)) {
if ($value instanceof PageArray) {
$value = $value->each('id');
} else $value = $value->id;
}
$contextDataField = strtok("\r\n");
if (empty($value)) continue;
if ($langField && $this->wire('languages')) {
foreach($this->wire('languages') as $language) {
$key = $language->isDefault()? strtolower(trim($name)) : strtolower(trim($name)) . $language->id;
if ($value instanceof LanguagesPageFieldValue) {
$langValue = $value->getLanguageValue($language->id);
} else {
$langValue = $page->{"_{$forpage}_{$forfield}_$key"};
}
if(empty($langValue)) continue;
$data[$key] = $langValue;
}
} else $data[$name] = $value;
}
if (empty($data)) return null;
// return json_encode($data, JSON_NUMERIC_CHECK);
return json_encode($data, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_NUMERIC_CHECK);
}
/**
* Get Inputfields to configure extra field
*
* @param Field $field
* @return InputfieldWrapper
*
*/
public function ___getConfigInputfields(Field $field) {
$this->getContextDataInputfields($field); // any configuration errors?
$inputfields = parent::___getConfigInputfields($field);
$lang = '';
// tab label 2nd config inputfield
$f = wire('modules')->get('InputfieldText');
$f->attr('name+id', 'contextTabLabel');
$f->label = $this->_('Tab Label');
$f->description = $this->_('');
$f->placeholder = self::getDefaultContextTabLabel();
$f->notes = $this->_('');
$f->columnWidth = 33;
$contextTabLabelValue = $field->contextTabLabel? $field->contextTabLabel : self::getDefaultContextTabLabel();
$f->attr('value', $contextTabLabelValue);
if($this->wire('languages')) {
$f->useLanguages = 1;
foreach($this->wire('languages') as $language) {
if ($language->isDefault()) continue;
$f->attr("value$language", $field->{"contextTabLabel$language"});
if (wire('user')->language == $language && $field->{"contextTabLabel$language"}) {
$contextTabLabelValue = $field->{"contextTabLabel$language"};
}
}
}
$inputfields->add($f);
// context data field setup 1st config inputfield
$f = wire('modules')->get('InputfieldTextarea');
$f->attr('name+id', 'contextDataFields');
$f->label = $this->_('Additional page fields in Pagefield context');
$f->description = sprintf($this->_('Enter one field per line in the format `name:Inputfield`. The Inputfield must be defined by classname e.g. **InputfieldTextarea**. After specifying the inputfield, further inputfield settings can be added separated by commata, e.g. `text:InputfieldText,columnWidth=50,label=Hello World`. Commata is reserved as separator. If you need to use commata in a setting value escape via backslash "\,". Please see [instructions for using this field](%s).'), 'https://processwire.com/');
$f->notes = sprintf($this->_('Inputfields defined here only appear in the page edit modal under the tab "%s" that was opened from the Pagefield.'), $contextTabLabelValue);
$f->attr('value', $field->contextDataFields);
$f->attr('rows', 10);
$f->required = true;
$inputfields->insertBefore($f, $inputfields->get('contextTabLabel'));
// modal title
$f = wire('modules')->get('InputfieldText');
$f->attr('name+id', 'modalTitle');
$f->label = $this->_('Modal Title');
$f->description = $this->_("Title of the modal within the page is edited. Use placeholders surrounded by curled brackets to provide page fields and properties. Field and page beeing edited from where this modal has been opened are accessible via the properties: 'forpage' and 'forfield'.");
$f->notes = "**" . $this->_('Default') . ":** " . self::getDefaultModalTitle();
$f->columnWidth = 67;
$f->attr('value', $field->modalTitle? $field->modalTitle : self::getDefaultModalTitle());
if($this->wire('languages')) {
$f->useLanguages = 1;
foreach($this->wire('languages') as $language) {
if ($language->isDefault()) continue;
$f->attr("value$language", $field->{"modalTitle$language"});
}
}
$inputfields->add($f);
return $inputfields;
}
/**
* Update a DatabaseSelectQuery object to match a Page associated with a matching context data field
* This function is limited to the usage with the equalsign operator in selectors
* and fields that do not support array values or multilanguage values
* use selector '<fieldname><languageID>=<searchstring>' to search for a specific language
*
*
* @param DatabaseQuerySelect|PageFinderDatabaseQuerySelect $query
* @param string $table
* @param string $subfield
* @param string $operator
* @param string $value
* @return DatabaseQuery
* @throws WireException if operator not supported or Inputfield has array value
*
*/
public function getMatchQuery($query, $table, $subfield, $operator, $value) {
$fieldSubfield = $this->getContextDataInputfields($query->field)->child("contextDataField=$subfield");
// remove leading underscore if present
$subfield = ltrim($subfield, '_');
// quick exit
if (!$fieldSubfield) return parent::getMatchQuery($query, $table, $subfield, $operator, $value);
else if ($operator != '=') throw new WireException("Operator '$operator' is not allowed for context data fields");
if($fieldSubfield instanceof InputfieldHasArrayValue || $fieldSubfield instanceof InputfieldSupportsArrayValue ||wire('modules')->get($fieldSubfield->inputfield) instanceof InputfieldHasArrayValue || wire('modules')->get($fieldSubfield->inputfield) instanceof InputfieldSupportsArrayValue) {
throw new WireException("Context data fields supporting array values are not matchable via selector.");
}
// translate selector
$value = trim(json_encode([$subfield => $value]), '{}');
$subfield = 'data_context';
$operator = '%=';
$ft = new DatabaseQuerySelectFulltext($query);
$ft->match($table, $subfield, $operator, $value);
return $query;
}
/**
* Return the database schema in predefined format
*
* @param Field $field
* @return array
*
*/
public function getDatabaseSchema(Field $field) {
$schema = parent::getDatabaseSchema($field);
$schema['data'] = 'int NOT NULL';
$schema['data_context'] = "mediumtext NULL";
$schema['keys']['data'] = 'KEY data (data, pages_id, sort)';
$schema['keys']['data_context'] = 'FULLTEXT KEY `data_context` (`data_context`)';
return $schema;
}
}