1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* SEOmatic plugin for Craft CMS 3.x |
4
|
|
|
* |
5
|
|
|
* A turnkey SEO implementation for Craft CMS that is comprehensive, powerful, |
6
|
|
|
* and flexible |
7
|
|
|
* |
8
|
|
|
* @link https://nystudio107.com |
9
|
|
|
* @copyright Copyright (c) 2017 nystudio107 |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace nystudio107\seomatic\validators; |
13
|
|
|
|
14
|
|
|
use nystudio107\seomatic\Seomatic; |
15
|
|
|
use nystudio107\seomatic\variables\SeomaticVariable; |
16
|
|
|
|
17
|
|
|
use Craft; |
18
|
|
|
|
19
|
|
|
use yii\base\Model; |
20
|
|
|
use yii\validators\Validator; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @author nystudio107 |
|
|
|
|
24
|
|
|
* @package Seomatic |
25
|
|
|
* @since 3.4.0 |
26
|
|
|
*/ |
27
|
|
|
class TwigExpressionValidator extends Validator |
28
|
|
|
{ |
29
|
|
|
// Public Methods |
30
|
|
|
// ========================================================================= |
31
|
|
|
|
32
|
|
|
/** |
|
|
|
|
33
|
|
|
* @inheritdoc |
34
|
|
|
*/ |
35
|
|
|
public function validateAttribute($model, $attribute) |
36
|
|
|
{ |
37
|
|
|
/** @var Model $model */ |
|
|
|
|
38
|
|
|
$value = $model->$attribute; |
39
|
|
|
$error = null; |
40
|
|
|
if (!empty($value) && \is_string($value)) { |
41
|
|
|
try { |
42
|
|
|
if (Seomatic::$seomaticVariable === null) { |
43
|
|
|
Seomatic::$seomaticVariable = new SeomaticVariable(); |
44
|
|
|
} |
45
|
|
|
Craft::$app->getView()->renderString($value, [ |
46
|
|
|
'seomatic' => Seomatic::$seomaticVariable |
47
|
|
|
]); |
48
|
|
|
} catch (\Exception $e) { |
49
|
|
|
$error = Craft::t( |
50
|
|
|
'seomatic', |
51
|
|
|
'Error rendering template string -> {error}', |
52
|
|
|
['error' => $e->getMessage()] |
53
|
|
|
); |
54
|
|
|
} |
55
|
|
|
} else { |
56
|
|
|
$error = Craft::t('seomatic', 'Is not a string.'); |
57
|
|
|
} |
58
|
|
|
// If there's an error, add it to the model, and log it |
59
|
|
|
if ($error) { |
60
|
|
|
$model->addError($attribute, $error); |
61
|
|
|
Craft::error($error, __METHOD__); |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|