nystudio107 /
craft-twigfield
| 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
Loading history...
|
|||
| 8 | * @copyright Copyright (c) 2022 nystudio107 |
||
|
0 ignored issues
–
show
|
|||
| 9 | */ |
||
|
0 ignored issues
–
show
|
|||
| 10 | |||
| 11 | namespace nystudio107\twigfield\controllers; |
||
| 12 | |||
| 13 | use craft\helpers\Json; |
||
| 14 | use craft\web\Controller; |
||
| 15 | use nystudio107\twigfield\Twigfield; |
||
| 16 | use yii\web\Response; |
||
| 17 | |||
| 18 | /** |
||
|
0 ignored issues
–
show
|
|||
| 19 | * @author nystudio107 |
||
|
0 ignored issues
–
show
Content of the @author tag must be in the form "Display Name <[email protected]>"
Loading history...
|
|||
| 20 | * @package Twigfield |
||
|
0 ignored issues
–
show
|
|||
| 21 | * @since 1.0.0 |
||
|
0 ignored issues
–
show
|
|||
| 22 | */ |
||
|
0 ignored issues
–
show
|
|||
| 23 | class AutocompleteController extends Controller |
||
| 24 | { |
||
| 25 | /** |
||
|
0 ignored issues
–
show
|
|||
| 26 | * @inheritDoc |
||
| 27 | */ |
||
|
0 ignored issues
–
show
|
|||
| 28 | public function beforeAction($action): bool |
||
| 29 | { |
||
| 30 | if (Twigfield::$settings->allowFrontendAccess) { |
||
| 31 | $this->allowAnonymous = 1; |
||
| 32 | } |
||
| 33 | |||
| 34 | return parent::beforeAction($action); |
||
| 35 | } |
||
| 36 | |||
| 37 | /** |
||
| 38 | * Return all of the autocomplete items in JSON format |
||
| 39 | * |
||
| 40 | * @param string $fieldType |
||
|
0 ignored issues
–
show
|
|||
| 41 | * @param string $twigfieldOptions |
||
|
0 ignored issues
–
show
|
|||
| 42 | * @return Response |
||
|
0 ignored issues
–
show
|
|||
| 43 | */ |
||
| 44 | public function actionIndex(string $fieldType = Twigfield::DEFAULT_FIELD_TYPE, string $twigfieldOptions = ''): Response |
||
| 45 | { |
||
| 46 | $options = []; |
||
| 47 | $parsedJson = Json::decodeIfJson($twigfieldOptions); |
||
| 48 | if (is_array($parsedJson)) { |
||
| 49 | $options = $parsedJson; |
||
| 50 | } |
||
| 51 | $result = Twigfield::getInstance()->autocomplete->generateAutocompletes($fieldType, $options); |
||
| 52 | |||
| 53 | return $this->asJson($result); |
||
| 54 | } |
||
| 55 | } |
||
| 56 |