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\autocompletes; |
||
| 12 | |||
| 13 | use Craft; |
||
| 14 | use craft\base\ElementInterface; |
||
|
0 ignored issues
–
show
The type
craft\base\ElementInterface was not found. Maybe you did not declare it correctly or list all dependencies?
The issue could also be caused by a filter entry in the build configuration.
If the path has been excluded in your configuration, e.g. filter:
dependency_paths: ["lib/*"]
For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths Loading history...
|
|||
| 15 | use craft\elements\Entry; |
||
| 16 | use nystudio107\twigfield\base\ObjectParserAutocomplete; |
||
| 17 | use nystudio107\twigfield\models\CompleteItem; |
||
| 18 | use nystudio107\twigfield\types\AutocompleteTypes; |
||
| 19 | use nystudio107\twigfield\types\CompleteItemKind; |
||
| 20 | |||
| 21 | /** |
||
|
0 ignored issues
–
show
|
|||
| 22 | * @author nystudio107 |
||
|
0 ignored issues
–
show
Content of the @author tag must be in the form "Display Name <[email protected]>"
Loading history...
|
|||
| 23 | * @package twigfield |
||
|
0 ignored issues
–
show
|
|||
| 24 | * @since 1.0.13 |
||
|
0 ignored issues
–
show
|
|||
| 25 | */ |
||
|
0 ignored issues
–
show
|
|||
| 26 | class SectionShorthandFieldsAutocomplete extends ObjectParserAutocomplete |
||
| 27 | { |
||
| 28 | // Constants |
||
| 29 | // ========================================================================= |
||
| 30 | |||
| 31 | public const OPTIONS_DATA_KEY = 'SectionShorthandFields'; |
||
| 32 | |||
| 33 | /** |
||
|
0 ignored issues
–
show
|
|||
| 34 | * @array properties that are defined only using the `@property` docblock annotation |
||
| 35 | */ |
||
| 36 | public const MAGIC_GETTER_PROPERTIES = [ |
||
| 37 | Entry::class => [ |
||
| 38 | 'typeId' => "the entry type’s ID", |
||
| 39 | 'authorId' => "the entry author’s ID", |
||
| 40 | 'type' => "the entry type", |
||
| 41 | 'section' => "the entry’s section", |
||
| 42 | 'author' => "the entry’s author", |
||
| 43 | ] |
||
| 44 | ]; |
||
| 45 | |||
| 46 | // Public Properties |
||
| 47 | // ========================================================================= |
||
| 48 | |||
| 49 | /** |
||
|
0 ignored issues
–
show
|
|||
| 50 | * @var string The name of the autocomplete |
||
| 51 | */ |
||
| 52 | public $name = 'SectionShorthandFieldsAutocomplete'; |
||
| 53 | |||
| 54 | /** |
||
|
0 ignored issues
–
show
|
|||
| 55 | * @var string The type of the autocomplete |
||
| 56 | */ |
||
| 57 | public $type = AutocompleteTypes::TwigExpressionAutocomplete; |
||
| 58 | |||
| 59 | /** |
||
|
0 ignored issues
–
show
|
|||
| 60 | * @var string Whether the autocomplete should be parsed with . -delimited nested sub-properties |
||
| 61 | */ |
||
| 62 | public $hasSubProperties = true; |
||
| 63 | |||
| 64 | /** |
||
|
0 ignored issues
–
show
|
|||
| 65 | * @inheritdoc |
||
| 66 | */ |
||
| 67 | public $parseBehaviors = false; |
||
| 68 | |||
| 69 | /** |
||
|
0 ignored issues
–
show
|
|||
| 70 | * @inheritdoc |
||
| 71 | */ |
||
| 72 | public $parseMethods = false; |
||
| 73 | |||
| 74 | /** |
||
|
0 ignored issues
–
show
|
|||
| 75 | * @inheritdoc |
||
| 76 | */ |
||
| 77 | public $customPropertySortPrefix = ''; |
||
| 78 | |||
| 79 | /** |
||
|
0 ignored issues
–
show
|
|||
| 80 | * @inheritdoc |
||
| 81 | */ |
||
| 82 | public $propertySortPrefix = ''; |
||
| 83 | |||
| 84 | /** |
||
|
0 ignored issues
–
show
|
|||
| 85 | * @inheritdoc |
||
| 86 | */ |
||
| 87 | public $methodSortPrefix = ''; |
||
| 88 | |||
| 89 | /** |
||
|
0 ignored issues
–
show
|
|||
| 90 | * @var ?int The section id. A sectionId of 0 denotes we don't know what this section is, so use |
||
| 91 | * a generic `Entry` and don't generate any complete items for custom fields |
||
| 92 | */ |
||
| 93 | public $sectionId = null; |
||
| 94 | |||
| 95 | // Public Methods |
||
| 96 | // ========================================================================= |
||
| 97 | |||
| 98 | /** |
||
|
0 ignored issues
–
show
|
|||
| 99 | * @inerhitDoc |
||
| 100 | */ |
||
|
0 ignored issues
–
show
|
|||
| 101 | public function init(): void |
||
| 102 | { |
||
| 103 | $this->sectionId = $this->twigfieldOptions[self::OPTIONS_DATA_KEY] ?? null; |
||
| 104 | } |
||
| 105 | |||
| 106 | /** |
||
| 107 | * Core function that generates the autocomplete array |
||
| 108 | */ |
||
|
0 ignored issues
–
show
|
|||
| 109 | public function generateCompleteItems(): void |
||
| 110 | { |
||
| 111 | if ($this->sectionId !== null) { |
||
| 112 | // A sectionId of 0 denotes we don't know what this section is, so use |
||
| 113 | // a generic `Entry` and don't generate any complete items for custom fields |
||
| 114 | if ($this->sectionId === 0) { |
||
| 115 | $element = new Entry(); |
||
| 116 | $this->parseObject('', $element, 0); |
||
| 117 | $this->addMagicGetterProperties($element); |
||
| 118 | |||
| 119 | return; |
||
| 120 | } |
||
| 121 | // Find the entry types used in the passed in sectionId |
||
| 122 | $sections = Craft::$app->getSections(); |
||
| 123 | $section = $sections->getSectionById($this->sectionId); |
||
| 124 | if ($section) { |
||
| 125 | $entryTypes = $section->getEntryTypes(); |
||
| 126 | foreach ($entryTypes as $entryType) { |
||
| 127 | // Add the native fields in |
||
| 128 | if ($entryType->elementType) { |
||
| 129 | $element = new $entryType->elementType; |
||
| 130 | /* @var ElementInterface $element */ |
||
| 131 | $this->parseObject('', $element, 0); |
||
| 132 | $this->addMagicGetterProperties($element); |
||
| 133 | } |
||
| 134 | // Add the custom fields in |
||
| 135 | if (version_compare(Craft::$app->getVersion(), '4.0', '>=')) { |
||
| 136 | $customFields = $entryType->getCustomFields(); |
||
| 137 | } else { |
||
| 138 | $customFields = $entryType->getFields(); |
||
| 139 | } |
||
| 140 | foreach ($customFields as $customField) { |
||
| 141 | $name = $customField->handle; |
||
| 142 | $docs = $customField->instructions ?? ''; |
||
| 143 | if ($name) { |
||
| 144 | CompleteItem::create() |
||
| 145 | ->insertText($name) |
||
| 146 | ->label($name) |
||
| 147 | ->detail(Craft::t('twigfield', 'Custom Field Shorthand')) |
||
| 148 | ->documentation($docs) |
||
| 149 | ->kind(CompleteItemKind::FieldKind) |
||
| 150 | ->add($this); |
||
| 151 | } |
||
| 152 | } |
||
| 153 | } |
||
| 154 | } |
||
| 155 | } |
||
| 156 | } |
||
| 157 | |||
| 158 | /** |
||
| 159 | * Add in magic getter properties that are defined only in the `@property` docblock annotation |
||
| 160 | * |
||
| 161 | * @param ElementInterface $element |
||
|
0 ignored issues
–
show
|
|||
| 162 | * @return void |
||
|
0 ignored issues
–
show
|
|||
| 163 | */ |
||
| 164 | protected function addMagicGetterProperties(ElementInterface $element): void |
||
| 165 | { |
||
| 166 | foreach (self::MAGIC_GETTER_PROPERTIES as $key => $value) { |
||
| 167 | if ($key === $element::class) { |
||
| 168 | foreach ($value as $name => $docs) { |
||
| 169 | CompleteItem::create() |
||
| 170 | ->insertText($name) |
||
| 171 | ->label($name) |
||
| 172 | ->detail(Craft::t('twigfield', 'Custom Field Shorthand')) |
||
| 173 | ->documentation($docs) |
||
| 174 | ->kind(CompleteItemKind::FieldKind) |
||
| 175 | ->add($this); |
||
| 176 | } |
||
| 177 | } |
||
| 178 | } |
||
| 179 | } |
||
| 180 | } |
||
| 181 |