-
Notifications
You must be signed in to change notification settings - Fork 611
/
Template.php
137 lines (122 loc) · 3.17 KB
/
Template.php
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
<?php
/**
* @link https://craftcms.com/
* @copyright Copyright (c) Pixel & Tonic, Inc.
* @license https://craftcms.github.io/license/
*/
namespace craft\fieldlayoutelements;
use Craft;
use craft\base\ElementInterface;
use craft\helpers\Cp;
use craft\helpers\Html;
use craft\web\View;
use Throwable;
/**
* Template represents a UI element based on a custom template that can be included in field layouts.
*
* @author Pixel & Tonic, Inc. <support@pixelandtonic.com>
* @since 3.5.0
*/
class Template extends BaseUiElement
{
/**
* @var string The template path
*/
public string $template = '';
/**
* @inheritdoc
*/
protected function selectorLabel(): string
{
return $this->template ?: Craft::t('app', 'Template');
}
/**
* @inheritdoc
*/
protected function selectorIcon(): ?string
{
return 'file-code';
}
/**
* @inheritdoc
*/
protected function selectorLabelAttributes(): array
{
$attr = parent::selectorLabelAttributes();
if ($this->template) {
$attr['class'][] = 'code';
}
return $attr;
}
/**
* @inheritdoc
*/
public function hasCustomWidth(): bool
{
return true;
}
/**
* @inheritdoc
*/
public function hasSettings()
{
return true;
}
/**
* @inheritdoc
*/
protected function settingsHtml(): ?string
{
return Cp::textFieldHtml([
'label' => Craft::t('app', 'Template'),
'instructions' => Craft::t('app', 'The path to a template file within your `templates/` folder.'),
'tip' => Craft::t('app', 'The template will be rendered with an `element` variable.'),
'class' => 'code',
'id' => 'template',
'name' => 'template',
'value' => $this->template,
]);
}
/**
* @inheritdoc
*/
public function formHtml(?ElementInterface $element = null, bool $static = false): ?string
{
if (!$this->template) {
return $this->_error(Craft::t('app', 'No template path has been chosen yet.'), 'warning');
}
try {
$content = trim(Craft::$app->getView()->renderTemplate($this->template, [
'element' => $element,
'static' => $static,
], View::TEMPLATE_MODE_SITE));
} catch (Throwable $e) {
return $this->_error($e->getMessage(), 'error');
}
if ($content === '') {
return null;
}
return Html::tag('div', $content, $this->containerAttributes($element, $static));
}
/**
* Renders an error message.
*
* @param string $error
* @param string $errorClass
* @return string
*/
private function _error(string $error, string $errorClass): string
{
$icon = Html::tag('span', '', [
'data' => [
'icon' => 'alert',
],
]);
$content = Html::tag('p', $icon . ' ' . Html::encode($error), [
'class' => $errorClass,
]);
return Html::tag('div', $content, [
'class' => 'pane',
]);
}
}