-
Notifications
You must be signed in to change notification settings - Fork 690
Expand file tree
/
Copy pathCpScreenResponseBehavior.php
More file actions
771 lines (707 loc) · 20.6 KB
/
CpScreenResponseBehavior.php
File metadata and controls
771 lines (707 loc) · 20.6 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
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
<?php
/**
* @link https://craftcms.com/
* @copyright Copyright (c) Pixel & Tonic, Inc.
* @license https://craftcms.github.io/license/
*/
namespace craft\web;
use Craft;
use craft\helpers\Html;
use craft\helpers\UrlHelper;
use craft\models\Site;
use yii\base\Behavior;
/**
* Control panel screen response behavior.
*
* @extends Behavior<Response>
* @author Pixel & Tonic, Inc. <[email protected]>
* @since 4.0.0
*/
class CpScreenResponseBehavior extends Behavior
{
public const NAME = 'cp-screen';
/**
* @var callable|null Callable that will be called before other properties are added to the screen.
* @see prepareScreen()
*/
public $prepareScreen = null;
/**
* @var string|null The control panel edit URL for this screen.
* @see editUrl()
*/
public ?string $editUrl = null;
/**
* @var string|null The document title. If null, [[title]] will be used.
*
* This will only be used by full-page screens.
*
* @see docTitle()
*/
public ?string $docTitle = null;
/**
* @var string|null The page title.
*
* This will only be used by full-page screens.
*
* @see title()
*/
public ?string $title = null;
/**
* @var string|null The selected subnav item’s key in the global sidebar.
*
* This will only be used by full-page screens.
*
* @see selectedSubnavItem()
*/
public ?string $selectedSubnavItem = null;
/**
* @var Site|null The site that should be displayed within the breadcrumbs.
* @see site()
* @since 5.0.0
*/
public ?Site $site = null;
/**
* @var array<Site|array{site:Site,status?:string}>|null The sites that should be selectable by the site breadcrumb menu.
* @see selectableSites()
* @since 5.0.0
*/
public ?array $selectableSites = null;
/**
* @var array|callable|null Breadcrumbs.
*
* This will only be used by full-page screens.
*
* @see crumbs()
* @see addCrumb()
*/
public $crumbs = null;
/**
* @var array Tabs.
*
* @see tabs()
* @see addTab()
*/
public array $tabs = [];
/**
* @var string|null Class that should be added to the slideout body.
* @since 4.5.0
*/
public ?string $slideoutBodyClass = null;
/**
* @var array Custom attributes to add to the `<main>` tag.
*
* See [[\yii\helpers\BaseHtml::renderTagAttributes()]] for supported attribute syntaxes.
*
* This will only be used by full-page screens.
*
* @see mainAttributes()
*/
public array $mainAttributes = [];
/**
* @var array Custom attributes to add to the `<form>` tag.
*
* See [[\yii\helpers\BaseHtml::renderTagAttributes()]] for supported attribute syntaxes.
*
* @see formAttributes()
*/
public array $formAttributes = [];
/**
* @var string|null The form action.
* @see action()
*/
public ?string $action = null;
/**
* @var array|callable|null Alternate form actions.
*
* This will only be used by full-page screens.
*
* @see altActions()
* @see addAltAction()
*/
public $altActions = null;
/**
* @var string|null The URL the form should redirect to after posting.
*
* This will only be used by full-page screens.
*
* @see redirectUrl()
*/
public ?string $redirectUrl = null;
/**
* @var string|null The URL the form should redirect to after posting, if submitted via the
* <kbd>Ctrl</kbd><kbd>Command</kbd> + <kbd>S</kbd> keyboard shortcut.
*
* This will only be used by full-page screens.
*
* @see saveShortcutRedirectUrl()
*/
public ?string $saveShortcutRedirectUrl = null;
/**
* @var callable|null Context menu items factory.
* @see contextMenuItems()
* @since 5.0.0
*/
public $contextMenuItems = null;
/**
* @var string|callable|null Toolbar HTML
* @see toolbarHtml()
* @see toolbarTemplate()
* @since 5.7.0
*/
public $toolbarHtml = null;
/**
* @var callable|null Action menu items factory.
* @see actionMenuItems()
* @since 5.0.0
*/
public $actionMenuItems = null;
/**
* @var string|null The submit button label.
* @see submitButtonLabel()
*/
public ?string $submitButtonLabel = null;
/**
* @var string|callable|null Additional buttons’ HTML.
*
* This will only be used by full-page screens.
*
* @see additionalButtonsHtml()
* @see additionalButtonsTemplate()
* @since 5.0.0
*/
public $additionalButtonsHtml = null;
/**
* @var string|callable|null The content HTML.
* @see contentHtml()
* @see contentTemplate()
* @since 5.0.0
*/
public $contentHtml = null;
/**
* @var string|callable|null The right-hand meta sidebar HTML.
* @see metaSidebarHtml()
* @see metaSidebarTemplate()
* @since 5.0.0
*/
public $metaSidebarHtml = null;
/**
* @var string|callable|null The left-hand page sidebar HTML (only used by full-page screens).
* @see pageSidebarHtml()
* @see pageSidebarTemplate()
* @since 5.0.0
*/
public $pageSidebarHtml = null;
/**
* @var string|callable|null The content notice HTML.
* @see noticeHtml()
* @see noticeTemplate()
* @since 5.0.0
*/
public $noticeHtml = null;
/**
* @var string|callable|null The errors summary HTML (DEV-212).
* @see errorSummary()
* @see errorSummaryTemplate()
* @since 4.5.0
*/
public $errorSummary = null;
/**
* Sets a callable that will be called before other properties are added to the screen.
*
* @param callable|null $value
* @return Response
*/
public function prepareScreen(?callable $value): Response
{
$this->prepareScreen = $value;
return $this->owner;
}
/**
* Sets the control panel edit URL for this screen.
*
* @param string|null $value
* @return Response
*/
public function editUrl(?string $value): Response
{
$this->editUrl = $value;
return $this->owner;
}
/**
* Sets the document title.
*
* This will only be used by full-page screens.
*
* @param string|null $value
* @return Response
*/
public function docTitle(?string $value): Response
{
$this->docTitle = $value;
return $this->owner;
}
/**
* Sets the page title.
*
* This will only be used by full-page screens.
*
* @param string|null $value
* @return Response
*/
public function title(?string $value): Response
{
$this->title = $value;
return $this->owner;
}
/**
* Sets the selected subnav item’s key in the global sidebar.
*
* This will only be used by full-page screens.
*
* @param string|null $value
* @return Response
*/
public function selectedSubnavItem(?string $value): Response
{
$this->selectedSubnavItem = $value;
return $this->owner;
}
/**
* Sets the breadcrumbs.
*
* Breadcrumbs should be defined by arrays with the following keys:
*
* - `label` – The breadcrumb label, to be HTML-encoded
* - `url` – The URL that the breadcrumb should link to
* - `icon` – The icon which should be displayed beside the label
* - `menu` – The menu items which should be displayed alongside the breadcrumb
* (see [[\craft\helpers\Cp::disclosureMenu()]] for documentation on supported item properties)
* - `current` – Whether the breadcrumb represents the current page
*
* This will only be used by full-page screens.
*
* @param callable|array|null $value
* @return Response
*/
public function crumbs(callable|array|null $value): Response
{
$this->crumbs = $value;
return $this->owner;
}
/**
* Adds a breadcrumb.
*
* This will only be used by full-page screens.
*
* @param string $label
* @param string $url
* @return Response
*/
public function addCrumb(string $label, string $url): Response
{
if (!is_array($this->crumbs)) {
$this->crumbs = [];
}
$this->crumbs[] = [
'label' => $label,
'url' => UrlHelper::cpUrl($url),
];
return $this->owner;
}
/**
* Sets the site that should be displayed within the breadcrumbs.
*
* @param Site|null $value
* @return Response
* @since 5.0.0
*/
public function site(?Site $value): Response
{
$this->site = $value;
return $this->owner;
}
/**
* Sets the sites that should be selectable by the site breadcrumb menu.
*
* @param array<Site|array{site:Site,status?:string}>|null $value
* @return Response
* @since 5.0.0
*/
public function selectableSites(?array $value): Response
{
$this->selectableSites = $value;
return $this->owner;
}
/**
* Sets the tabs.
*
* Each tab should be represented by a nested array with the following keys:
*
* - `label` – The human-facing tab label.
* - `url` – The `href` attribute of the tab’s anchor. Set to `#container-ids` if the tabs are meant to toggle in-page content.
* - `class` _(optional)_ - Class name(s) that should be added to the tab’s anchor.
* - `visible` _(optional)_ – Whether the tab should be initially visible (defaults to `true`).
*
* If the tabs are meant to toggle in-page content, the array keys should be set to the `id` attributes of the
* container elements they represent.
*
* @param array $value
* @return Response
*/
public function tabs(array $value): Response
{
$this->tabs = $value;
return $this->owner;
}
/**
* Adds a tab.
*
* @param string $id
* @param string $label
* @param string $url
* @param string|string[]|null $class
* @param bool $visible
* @return Response
*/
public function addTab(string $id, string $label, string $url, array|string $class = null, bool $visible = true): Response
{
$this->tabs[$id] = [
'label' => $label,
'url' => $url,
'class' => Html::explodeClass($class),
'visible' => $visible,
];
return $this->owner;
}
/**
* Sets custom attributes that should be added to the `<main>` tag.
*
* See [[\yii\helpers\BaseHtml::renderTagAttributes()]] for supported attribute syntaxes.
*
* This will only be used by full-page screens.
*
* @param array $value
* @return Response
*/
public function mainAttributes(array $value): Response
{
$this->mainAttributes = $value;
return $this->owner;
}
/**
* Sets custom attributes that should be added to the `<form>` tag.
*
* See [[\yii\helpers\BaseHtml::renderTagAttributes()]] for supported attribute syntaxes.
*
* @param array $value
* @return Response
*/
public function formAttributes(array $value): Response
{
$this->formAttributes = $value;
return $this->owner;
}
/**
* Sets the form action.
*
* @param string|null $value
* @return Response
*/
public function action(?string $value): Response
{
$this->action = $value;
return $this->owner;
}
/**
* Sets alternate form actions.
*
* Each action should be represented by a nested array with the following keys:
*
* - `label` – The human-facing action label.
* - `destructive` _(optional)_ – Whether the action should be considered destructive (defaults to `false`).
* - `action` _(optional)_ – The controller action that should be posted to.
* - `redirect` _(optional)_ – The URL the form should redirect to afterwards.
* - `confirm` _(optional)_ – A confirmation message that should be shown.
* - `params` _(optional)_ – Array of additional params that should be posted.
* - `eventData` _(optional)_ – Additional properties that should be assigned to the JavaScript `submit` event.
* - `shortcut` _(optional)_ – Whether the action can be triggered with a <kbd>Command</kbd>/<kbd>Ctrl</kbd> + <kbd>S</kbd> keyboard shortcut
* (or <kbd>Command</kbd>/<kbd>Ctrl</kbd> + <kbd>Shift</kbd> + <kbd>S</kbd> if `'shift' => true` is also set).
* - `retainScroll` _(optional)_ – Whether the browser should retain its scroll position on the next page.
*
* This will only be used by full-page screens.
*
* @param callable|array|null $value
* @return Response
*/
public function altActions(callable|array|null $value): Response
{
$this->altActions = $value;
return $this->owner;
}
/**
* Adds an alternate form action.
*
* This will only be used by full-page screens.
*
* @param string $label
* @param array $config
* @return Response
* @see altActions()
*/
public function addAltAction(string $label, array $config): Response
{
if (!is_array($this->altActions)) {
$this->altActions = [];
}
$this->altActions[] = ['label' => $label] + $config;
return $this->owner;
}
/**
* Sets the URL the form should redirect to after posting.
*
* This will only be used by full-page screens.
*
* @param string|null $value
* @return Response
*/
public function redirectUrl(?string $value): Response
{
$this->redirectUrl = $value;
return $this->owner;
}
/**
* Sets URL the form should redirect to after posting, if submitted via the
* <kbd>Ctrl</kbd><kbd>Command</kbd> + <kbd>S</kbd> keyboard shortcut.
*
* This will only be used by full-page screens.
*
* @param string|null $value
* @return Response
*/
public function saveShortcutRedirectUrl(?string $value): Response
{
$this->saveShortcutRedirectUrl = $value;
return $this->owner;
}
/**
* Sets the context menu items.
*
* See [[\craft\helpers\Cp::disclosureMenu()]] for documentation on supported item properties.
*
* @param callable|null $value A callback function which returns the menu items
* @return Response
* @since 5.0.0
*/
public function contextMenuItems(?callable $value): Response
{
$this->contextMenuItems = $value;
return $this->owner;
}
/**
* Sets the toolbar HTML.
*
* @param callable|string|null $value
* @return Response
* @since 5.7.0
*/
public function toolbarHtml(callable|string|null $value): Response
{
$this->toolbarHtml = $value;
return $this->owner;
}
/**
* Sets a template that should be used to render the toolbar HTML.
*
* @param string $template
* @param array $variables
* @return Response
* @since 5.7.0
*/
public function toolbarTemplate(string $template, array $variables = []): Response
{
return $this->toolbarHtml(
fn() => Craft::$app->getView()->renderTemplate($template, $variables, View::TEMPLATE_MODE_CP)
);
}
/**
* Sets the action menu items.
*
* See [[\craft\helpers\Cp::disclosureMenu()]] for documentation on supported item properties.
*
* @param callable|null $value A callback function which returns the menu items
* @return Response
* @since 5.0.0
*/
public function actionMenuItems(?callable $value): Response
{
$this->actionMenuItems = $value;
return $this->owner;
}
/**
* Sets the submit button label.
*
* @param string|null $value
* @return Response
*/
public function submitButtonLabel(?string $value): Response
{
$this->submitButtonLabel = $value;
return $this->owner;
}
/**
* Sets the additional buttons’ HTML.
*
* This will only be used by full-page screens.
*
* @param callable|string|null $value
* @return Response
* @since 5.0.0
*/
public function additionalButtonsHtml(callable|string|null $value): Response
{
$this->additionalButtonsHtml = $value;
return $this->owner;
}
/**
* Sets a template that should be used to render the additional buttons’ HTML.
*
* This will only be used by full-page screens.
*
* @param string $template
* @param array $variables
* @return Response
*/
public function additionalButtonsTemplate(string $template, array $variables = []): Response
{
return $this->additionalButtonsHtml(
fn() => Craft::$app->getView()->renderTemplate($template, $variables, View::TEMPLATE_MODE_CP)
);
}
/**
* Sets the content HTML.
*
* @param callable|string|null $value
* @return Response
* @since 5.0.0
*/
public function contentHtml(callable|string|null $value): Response
{
$this->contentHtml = $value;
return $this->owner;
}
/**
* Sets a template that should be used to render the content HTML.
*
* @param string $template
* @param array $variables
* @return Response
*/
public function contentTemplate(string $template, array $variables = []): Response
{
return $this->contentHtml(
fn() => Craft::$app->getView()->renderTemplate($template, $variables, View::TEMPLATE_MODE_CP)
);
}
/**
* Sets the right-hand meta sidebar HTML.
*
* @param callable|string|null $value
* @return Response
* @since 5.0.0
*/
public function metaSidebarHtml(callable|string|null $value): Response
{
$this->metaSidebarHtml = $value;
return $this->owner;
}
/**
* Sets a template that should be used to render the right-hand meta sidebar HTML.
*
* @param string $template
* @param array $variables
* @return Response
*/
public function metaSidebarTemplate(string $template, array $variables = []): Response
{
return $this->metaSidebarHtml(
fn() => Craft::$app->getView()->renderTemplate($template, $variables, View::TEMPLATE_MODE_CP)
);
}
/**
* Sets the left-hand page sidebar HTML (only used by full-page screens).
*
* @param callable|string|null $value
* @return Response
* @since 5.0.0
*/
public function pageSidebarHtml(callable|string|null $value): Response
{
$this->pageSidebarHtml = $value;
return $this->owner;
}
/**
* Sets a template that should be used to render the left-hand page sidebar HTML (only used by full-page screens).
*
* @param string $template
* @param array $variables
* @return Response
* @since 4.5.0
*/
public function pageSidebarTemplate(string $template, array $variables = []): Response
{
return $this->pageSidebarHtml(
fn() => Craft::$app->getView()->renderTemplate($template, $variables, View::TEMPLATE_MODE_CP)
);
}
/**
* Sets the content notice HTML.
*
* @param callable|string|null $value
* @return Response
* @since 5.0.0
*/
public function noticeHtml(callable|string|null $value): Response
{
$this->noticeHtml = $value;
return $this->owner;
}
/**
* Sets a template that should be used to render the content notice HTML.
*
* @param string $template
* @param array $variables
* @return Response
*/
public function noticeTemplate(string $template, array $variables = []): Response
{
return $this->noticeHtml(
fn() => Craft::$app->getView()->renderTemplate($template, $variables, View::TEMPLATE_MODE_CP)
);
}
/**
* Sets the errors summary HTML.
*
* @param callable|string|null $value
* @return Response
* @since 4.5.0
*/
public function errorSummary(callable|string|null $value): Response
{
$this->errorSummary = $value;
return $this->owner;
}
/**
* Sets a template that should be used to render the errors summary HTML.
*
* @param string $template
* @param array $variables
* @return Response
* @since 4.5.0
*/
public function errorSummaryTemplate(string $template, array $variables = []): Response
{
return $this->errorSummary(
fn() => Craft::$app->getView()->renderTemplate($template, $variables, View::TEMPLATE_MODE_CP)
);
}
}