|
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 Craft; |
|
15
|
|
|
use Exception; |
|
16
|
|
|
use nystudio107\seomatic\Seomatic; |
|
17
|
|
|
use nystudio107\seomatic\variables\SeomaticVariable; |
|
18
|
|
|
use yii\base\Model; |
|
19
|
|
|
use yii\validators\Validator; |
|
20
|
|
|
use function is_string; |
|
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
|
|
|
Seomatic::$plugin->metaContainers->loadGlobalMetaContainers(); |
|
45
|
|
|
Seomatic::$seomaticVariable->init(); |
|
46
|
|
|
} |
|
47
|
|
|
Craft::$app->getView()->renderString($value, [ |
|
48
|
|
|
'seomatic' => Seomatic::$seomaticVariable |
|
49
|
|
|
]); |
|
50
|
|
|
} catch (Exception $e) { |
|
51
|
|
|
$error = Craft::t( |
|
52
|
|
|
'seomatic', |
|
53
|
|
|
'Error rendering template string -> {error}', |
|
54
|
|
|
['error' => $e->getMessage()] |
|
55
|
|
|
); |
|
56
|
|
|
} |
|
57
|
|
|
} else { |
|
58
|
|
|
$error = Craft::t('seomatic', 'Is not a string.'); |
|
59
|
|
|
} |
|
60
|
|
|
// If there's an error, add it to the model, and log it |
|
61
|
|
|
if ($error) { |
|
62
|
|
|
$model->addError($attribute, $error); |
|
63
|
|
|
Craft::error($error, __METHOD__); |
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
|
} |
|
67
|
|
|
|