|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* CodeEditor for Craft CMS |
|
4
|
|
|
* |
|
5
|
|
|
* Provides a code editor field with Twig & Craft API autocomplete |
|
6
|
|
|
* |
|
7
|
|
|
* @link https://nystudio107.com |
|
|
|
|
|
|
8
|
|
|
* @copyright Copyright (c) 2022 nystudio107 |
|
|
|
|
|
|
9
|
|
|
*/ |
|
|
|
|
|
|
10
|
|
|
|
|
11
|
|
|
namespace nystudio107\codeeditor\validators; |
|
12
|
|
|
|
|
13
|
|
|
use Craft; |
|
14
|
|
|
use Exception; |
|
15
|
|
|
use nystudio107\codeeditor\events\RegisterTwigValidatorVariablesEvent; |
|
16
|
|
|
use yii\base\Model; |
|
17
|
|
|
use yii\validators\Validator; |
|
18
|
|
|
use function is_string; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
|
|
|
|
|
21
|
|
|
* @author nystudio107 |
|
|
|
|
|
|
22
|
|
|
* @package CodeEditor |
|
|
|
|
|
|
23
|
|
|
* @since 1.0.0 |
|
|
|
|
|
|
24
|
|
|
*/ |
|
|
|
|
|
|
25
|
|
|
class TwigTemplateValidator extends Validator |
|
26
|
|
|
{ |
|
27
|
|
|
// Constants |
|
28
|
|
|
// ========================================================================= |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
|
|
|
|
|
31
|
|
|
* @event RegisterTwigValidatorVariablesEvent The event that is triggered to allow |
|
32
|
|
|
* you to register Variables to be passed down to the Twig context during |
|
33
|
|
|
* Twig template validation |
|
34
|
|
|
* |
|
35
|
|
|
* ```php |
|
36
|
|
|
* use nystudio107\codeeditor\validators\TwigTemplateValidator; |
|
37
|
|
|
* use nystudio107\codeeditor\events\RegisterTwigValidatorVariablesEvent; |
|
38
|
|
|
* use yii\base\Event; |
|
39
|
|
|
* |
|
40
|
|
|
* Event::on(TwigTemplateValidator::class, |
|
41
|
|
|
* TwigTemplateValidator::EVENT_REGISTER_TWIG_VALIDATOR_VARIABLES, |
|
42
|
|
|
* function(RegisterTwigValidatorVariablesEvent $event) { |
|
43
|
|
|
* $event->variables['variableName'] = $variableValue; |
|
44
|
|
|
* } |
|
45
|
|
|
* ); |
|
46
|
|
|
* ``` |
|
47
|
|
|
*/ |
|
48
|
|
|
public const EVENT_REGISTER_TWIG_VALIDATOR_VARIABLES = 'registerTwigValidatorVariables'; |
|
49
|
|
|
|
|
50
|
|
|
// Public Properties |
|
51
|
|
|
// ========================================================================= |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
|
|
|
|
|
54
|
|
|
* @var array Variables in key => value format that should be available during the template rendering |
|
55
|
|
|
*/ |
|
56
|
|
|
public $variables = []; |
|
57
|
|
|
|
|
58
|
|
|
// Public Methods |
|
59
|
|
|
// ========================================================================= |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
|
|
|
|
|
62
|
|
|
* @inheritdoc |
|
63
|
|
|
*/ |
|
|
|
|
|
|
64
|
|
|
public function validateAttribute($model, $attribute) |
|
65
|
|
|
{ |
|
66
|
|
|
/** @var Model $model */ |
|
|
|
|
|
|
67
|
|
|
$value = $model->$attribute; |
|
68
|
|
|
$error = null; |
|
69
|
|
|
if (!empty($value) && is_string($value)) { |
|
70
|
|
|
try { |
|
71
|
|
|
$event = new RegisterTwigValidatorVariablesEvent([ |
|
|
|
|
|
|
72
|
|
|
'variables' => $this->variables, |
|
73
|
|
|
]); |
|
|
|
|
|
|
74
|
|
|
$this->trigger(self::EVENT_REGISTER_TWIG_VALIDATOR_VARIABLES, $event); |
|
75
|
|
|
Craft::$app->getView()->renderString($value, $event->variables); |
|
76
|
|
|
} catch (Exception $e) { |
|
77
|
|
|
$error = Craft::t( |
|
78
|
|
|
'codeeditor', |
|
79
|
|
|
'Error rendering template string -> {error}', |
|
80
|
|
|
['error' => $e->getMessage()] |
|
81
|
|
|
); |
|
82
|
|
|
} |
|
83
|
|
|
} else { |
|
84
|
|
|
$error = Craft::t('codeeditor', 'Is not a string.'); |
|
85
|
|
|
} |
|
86
|
|
|
// If there's an error, add it to the model, and log it |
|
87
|
|
|
if ($error) { |
|
88
|
|
|
$model->addError($attribute, $error); |
|
89
|
|
|
Craft::error($error, __METHOD__); |
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
|