1 | <?php |
||
2 | /** |
||
3 | * Twigfield for Craft CMS |
||
4 | * |
||
5 | * Provides a twig editor field with Twig & Craft API autocomplete |
||
6 | * |
||
7 | * @link https://nystudio107.com |
||
0 ignored issues
–
show
Coding Style
introduced
by
![]() |
|||
8 | * @copyright Copyright (c) 2022 nystudio107 |
||
0 ignored issues
–
show
|
|||
9 | */ |
||
0 ignored issues
–
show
|
|||
10 | |||
11 | namespace nystudio107\twigfield; |
||
12 | |||
13 | use Craft; |
||
14 | use craft\events\RegisterTemplateRootsEvent; |
||
15 | use craft\helpers\UrlHelper; |
||
16 | use craft\i18n\PhpMessageSource; |
||
17 | use craft\web\Application as CraftWebApp; |
||
18 | use craft\web\View; |
||
19 | use nystudio107\twigfield\helpers\Config; |
||
20 | use nystudio107\twigfield\models\Settings; |
||
21 | use nystudio107\twigfield\services\AutocompleteService; |
||
22 | use yii\base\BootstrapInterface; |
||
23 | use yii\base\Event; |
||
24 | use yii\base\Module; |
||
25 | |||
26 | /** |
||
0 ignored issues
–
show
|
|||
27 | * @author nystudio107 |
||
0 ignored issues
–
show
Content of the @author tag must be in the form "Display Name <[email protected]>"
![]() |
|||
28 | * @package Twigfield |
||
0 ignored issues
–
show
|
|||
29 | * @since 1.0.0 |
||
0 ignored issues
–
show
|
|||
30 | * |
||
31 | * @property AutocompleteService $autocomplete |
||
32 | */ |
||
0 ignored issues
–
show
|
|||
33 | class Twigfield extends Module implements BootstrapInterface |
||
34 | { |
||
35 | // Constants |
||
36 | // ========================================================================= |
||
37 | |||
38 | const ID = 'twigfield'; |
||
39 | |||
40 | const DEFAULT_FIELD_TYPE = 'Twigfield'; |
||
41 | |||
42 | // Public Static Properties |
||
43 | // ========================================================================= |
||
44 | |||
45 | /** |
||
0 ignored issues
–
show
|
|||
46 | * @var Settings The Twigfield config settings |
||
47 | */ |
||
48 | public static $settings = null; |
||
49 | |||
50 | // Public Methods |
||
51 | // ========================================================================= |
||
52 | |||
53 | /** |
||
0 ignored issues
–
show
|
|||
54 | * @inerhitdoc |
||
55 | */ |
||
56 | public function __construct($id = self::ID, $parent = null, $config = []) |
||
57 | { |
||
58 | /** |
||
59 | * Explicitly set the $id parameter, as earlier versions of Yii2 look for a |
||
60 | * default parameter, and depend on $id being explicitly set: |
||
61 | * https://github.com/yiisoft/yii2/blob/f3d1534125c9c3dfe8fa65c28a4be5baa822e721/framework/di/Container.php#L436-L448 |
||
62 | */ |
||
63 | parent::__construct($id, $parent, $config); |
||
64 | } |
||
65 | |||
66 | /** |
||
0 ignored issues
–
show
|
|||
67 | * @inerhitDoc |
||
68 | */ |
||
0 ignored issues
–
show
|
|||
69 | public function bootstrap($app) |
||
70 | { |
||
71 | // Only bootstrap if this is a CraftWebApp |
||
72 | if (!$app instanceof CraftWebApp) { |
||
73 | return; |
||
74 | } |
||
75 | // Set the instance of this module class, so we can later access it with `Twigfield::getInstance()` |
||
76 | static::setInstance($this); |
||
77 | // Configure our module |
||
78 | $this->configureModule(); |
||
79 | // Register our components |
||
80 | $this->registerComponents(); |
||
81 | // Register our event handlers |
||
82 | $this->registerEventHandlers(); |
||
83 | Craft::info('Twigfield module bootstrapped', __METHOD__); |
||
84 | } |
||
85 | |||
86 | // Protected Methods |
||
87 | // ========================================================================= |
||
88 | |||
89 | /** |
||
90 | * Configure our module |
||
91 | * |
||
92 | * @return void |
||
93 | */ |
||
94 | protected function configureModule(): void |
||
95 | { |
||
96 | // Set up our alias |
||
97 | Craft::setAlias('@nystudio107/twigfield', $this->getBasePath()); |
||
98 | Craft::setAlias('@twigfieldEndpointUrl', UrlHelper::actionUrl('twigfield/autocomplete/index')); |
||
99 | // Register our module |
||
100 | Craft::$app->setModule($this->id, $this); |
||
101 | // Translation category |
||
102 | $i18n = Craft::$app->getI18n(); |
||
103 | /** @noinspection UnSafeIsSetOverArrayInspection */ |
||
0 ignored issues
–
show
|
|||
104 | if (!isset($i18n->translations[$this->id]) && !isset($i18n->translations[$this->id . '*'])) { |
||
105 | $i18n->translations[$this->id] = [ |
||
106 | 'class' => PhpMessageSource::class, |
||
107 | 'sourceLanguage' => 'en-US', |
||
108 | 'basePath' => '@nystudio107/twigfield/translations', |
||
109 | 'forceTranslation' => true, |
||
110 | 'allowOverrides' => true, |
||
111 | ]; |
||
112 | } |
||
113 | // Set our settings |
||
114 | $config = Config::getConfigFromFile($this->id); |
||
115 | self::$settings = new Settings($config); |
||
116 | } |
||
117 | |||
118 | /** |
||
119 | * Registers our components |
||
120 | * |
||
121 | * @return void |
||
122 | */ |
||
123 | public function registerComponents(): void |
||
124 | { |
||
125 | $this->setComponents([ |
||
0 ignored issues
–
show
|
|||
126 | 'autocomplete' => AutocompleteService::class, |
||
127 | ]); |
||
0 ignored issues
–
show
For multi-line function calls, the closing parenthesis should be on a new line.
If a function call spawns multiple lines, the coding standard suggests to move the closing parenthesis to a new line: someFunctionCall(
$firstArgument,
$secondArgument,
$thirdArgument
); // Closing parenthesis on a new line.
![]() |
|||
128 | } |
||
129 | |||
130 | /** |
||
131 | * Registers our event handlers |
||
132 | * |
||
133 | * @return void |
||
134 | */ |
||
135 | public function registerEventHandlers(): void |
||
136 | { |
||
137 | // Base CP templates directory |
||
138 | Event::on(View::class, View::EVENT_REGISTER_CP_TEMPLATE_ROOTS, function (RegisterTemplateRootsEvent $e) { |
||
0 ignored issues
–
show
|
|||
139 | if (is_dir($baseDir = $this->getBasePath() . DIRECTORY_SEPARATOR . 'templates')) { |
||
140 | $e->roots[$this->id] = $baseDir; |
||
141 | } |
||
142 | }); |
||
0 ignored issues
–
show
For multi-line function calls, the closing parenthesis should be on a new line.
If a function call spawns multiple lines, the coding standard suggests to move the closing parenthesis to a new line: someFunctionCall(
$firstArgument,
$secondArgument,
$thirdArgument
); // Closing parenthesis on a new line.
![]() |
|||
143 | // Base Site templates directory |
||
144 | if (self::$settings->allowFrontendAccess) { |
||
145 | Event::on(View::class, View::EVENT_REGISTER_SITE_TEMPLATE_ROOTS, function (RegisterTemplateRootsEvent $e) { |
||
0 ignored issues
–
show
|
|||
146 | if (is_dir($baseDir = $this->getBasePath() . DIRECTORY_SEPARATOR . 'templates')) { |
||
147 | $e->roots[$this->id] = $baseDir; |
||
148 | } |
||
149 | }); |
||
0 ignored issues
–
show
For multi-line function calls, the closing parenthesis should be on a new line.
If a function call spawns multiple lines, the coding standard suggests to move the closing parenthesis to a new line: someFunctionCall(
$firstArgument,
$secondArgument,
$thirdArgument
); // Closing parenthesis on a new line.
![]() |
|||
150 | } |
||
151 | } |
||
152 | } |
||
153 |