|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Rich Variables plugin for Craft CMS 3.x |
|
4
|
|
|
* |
|
5
|
|
|
* Allows you to easily use Craft Globals as variables in Rich Text fields |
|
6
|
|
|
* |
|
7
|
|
|
* @link https://nystudio107.com |
|
|
|
|
|
|
8
|
|
|
* @copyright Copyright (c) 2017 nystudio107 |
|
|
|
|
|
|
9
|
|
|
*/ |
|
|
|
|
|
|
10
|
|
|
|
|
11
|
|
|
namespace nystudio107\richvariables\controllers; |
|
12
|
|
|
|
|
13
|
|
|
use aelvan\preparsefield\fields\PreparseFieldType as PreparseField; |
|
|
|
|
|
|
14
|
|
|
use Craft; |
|
15
|
|
|
use craft\base\Field; |
|
16
|
|
|
use craft\ckeditor\Field as CKEditorField; |
|
|
|
|
|
|
17
|
|
|
use craft\fields\Date as DateField; |
|
18
|
|
|
use craft\fields\Dropdown as DropdownField; |
|
19
|
|
|
use craft\fields\Number as NumberField; |
|
20
|
|
|
use craft\fields\PlainText as PlainTextField; |
|
21
|
|
|
use craft\helpers\Json; |
|
22
|
|
|
use craft\redactor\Field as RedactorField; |
|
|
|
|
|
|
23
|
|
|
use craft\web\Controller; |
|
24
|
|
|
use nystudio107\richvariables\assetbundles\richvariables\RichVariablesAsset; |
|
25
|
|
|
use nystudio107\richvariables\RichVariables; |
|
26
|
|
|
use yii\base\InvalidConfigException; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
|
|
|
|
|
29
|
|
|
* @author nystudio107 |
|
|
|
|
|
|
30
|
|
|
* @package RichVariables |
|
|
|
|
|
|
31
|
|
|
* @since 1.0.0 |
|
|
|
|
|
|
32
|
|
|
*/ |
|
|
|
|
|
|
33
|
|
|
class DefaultController extends Controller |
|
34
|
|
|
{ |
|
35
|
|
|
// Constants |
|
36
|
|
|
// ========================================================================= |
|
37
|
|
|
|
|
38
|
|
|
const VALID_FIELD_CLASSES = [ |
|
39
|
|
|
PlainTextField::class, |
|
40
|
|
|
NumberField::class, |
|
41
|
|
|
DateField::class, |
|
42
|
|
|
DropdownField::class, |
|
43
|
|
|
RedactorField::class, |
|
44
|
|
|
CKEditorField::class, |
|
45
|
|
|
PreparseField::class, |
|
46
|
|
|
]; |
|
47
|
|
|
|
|
48
|
|
|
// Public Methods |
|
49
|
|
|
// ========================================================================= |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
|
|
|
|
|
52
|
|
|
* @return mixed |
|
53
|
|
|
*/ |
|
54
|
|
|
public function actionIndex() |
|
55
|
|
|
{ |
|
56
|
|
|
$result = []; |
|
57
|
|
|
$variablesList = []; |
|
58
|
|
|
|
|
59
|
|
|
// Get the global set to use |
|
60
|
|
|
$settings = RichVariables::$plugin->getSettings(); |
|
61
|
|
|
$globalsSet = Craft::$app->getGlobals()->getSetByHandle($settings['globalSetHandle']); |
|
62
|
|
|
// Grab the first global set if they haven't specified one yet |
|
63
|
|
|
if (!$globalsSet) { |
|
64
|
|
|
$allGlobalsSetIds = Craft::$app->getGlobals()->getAllSetIds(); |
|
65
|
|
|
if (!empty($allGlobalsSetIds)) { |
|
66
|
|
|
$globalsSet = Craft::$app->globals->getSetById($allGlobalsSetIds[0]); |
|
|
|
|
|
|
67
|
|
|
} |
|
68
|
|
|
} |
|
69
|
|
|
if ($globalsSet) { |
|
70
|
|
|
// Get the field layout fields used for this global set |
|
71
|
|
|
$layout = $globalsSet->getFieldLayout(); |
|
72
|
|
|
if ($layout) { |
|
73
|
|
|
$fieldLayoutFields = $layout->getFields(); |
|
74
|
|
|
/** @var Field $field */ |
|
|
|
|
|
|
75
|
|
|
foreach ($fieldLayoutFields as $field) { |
|
76
|
|
|
foreach (self::VALID_FIELD_CLASSES as $fieldClass) { |
|
77
|
|
|
if ($field instanceof $fieldClass) { |
|
78
|
|
|
// Add the field title and Reference Tag as per https://craftcms.com/docs/reference-tags |
|
79
|
|
|
$thisVar = [ |
|
80
|
|
|
'title' => $field->name, |
|
81
|
|
|
'text' => '{globalset:' . $globalsSet->attributes['id'] . ':' . $field->handle . '}', |
|
82
|
|
|
]; |
|
83
|
|
|
$variablesList[] = $thisVar; |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
// Get the URL to our menu icon from our resource bundle |
|
91
|
|
|
try { |
|
92
|
|
|
Craft::$app->getView()->registerAssetBundle(RichVariablesAsset::class); |
|
93
|
|
|
} catch (InvalidConfigException $e) { |
|
94
|
|
|
Craft::error($e->getMessage(), __METHOD__); |
|
95
|
|
|
} |
|
96
|
|
|
$menuIconUrl = Craft::$app->assetManager->getPublishedUrl( |
|
|
|
|
|
|
97
|
|
|
'@nystudio107/richvariables/web/assets/dist', |
|
|
|
|
|
|
98
|
|
|
true |
|
|
|
|
|
|
99
|
|
|
) . '/img/RichVariables-menu-icon.svg'; |
|
|
|
|
|
|
100
|
|
|
|
|
101
|
|
|
// Return everything to our JavaScript encoded as JSON |
|
102
|
|
|
$result['variablesList'] = $variablesList; |
|
103
|
|
|
$result['menuIconUrl'] = $menuIconUrl; |
|
104
|
|
|
$result['useIconForMenu'] = $settings['useIconForMenu']; |
|
105
|
|
|
|
|
106
|
|
|
return Json::encode($result); |
|
107
|
|
|
} |
|
108
|
|
|
} |
|
109
|
|
|
|