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 |
|
|
|
|
8
|
|
|
* @copyright Copyright (c) 2022 nystudio107 |
|
|
|
|
9
|
|
|
*/ |
|
|
|
|
10
|
|
|
|
11
|
|
|
namespace nystudio107\seomatic\autocompletes; |
12
|
|
|
|
13
|
|
|
use Craft; |
14
|
|
|
use craft\base\ElementInterface; |
|
|
|
|
15
|
|
|
use nystudio107\twigfield\base\Autocomplete; |
16
|
|
|
use nystudio107\twigfield\models\CompleteItem; |
17
|
|
|
use nystudio107\twigfield\types\AutocompleteTypes; |
18
|
|
|
use nystudio107\twigfield\types\CompleteItemKind; |
19
|
|
|
|
20
|
|
|
/** |
|
|
|
|
21
|
|
|
* @author nystudio107 |
|
|
|
|
22
|
|
|
* @package twigfield |
|
|
|
|
23
|
|
|
* @since 1.0.13 |
|
|
|
|
24
|
|
|
*/ |
|
|
|
|
25
|
|
|
class SectionShorthandFieldsAutocomplete extends Autocomplete |
26
|
|
|
{ |
27
|
|
|
// Constants |
28
|
|
|
// ========================================================================= |
29
|
|
|
|
30
|
|
|
public const OPTIONS_DATA_KEY = 'SectionShorthandFieldsAutocomplete'; |
31
|
|
|
|
32
|
|
|
// Public Properties |
33
|
|
|
// ========================================================================= |
34
|
|
|
|
35
|
|
|
/** |
|
|
|
|
36
|
|
|
* @var string The name of the autocomplete |
37
|
|
|
*/ |
38
|
|
|
public $name = 'SectionShorthandFieldsAutocomplete'; |
39
|
|
|
|
40
|
|
|
/** |
|
|
|
|
41
|
|
|
* @var string The type of the autocomplete |
42
|
|
|
*/ |
43
|
|
|
public $type = AutocompleteTypes::TwigExpressionAutocomplete; |
44
|
|
|
|
45
|
|
|
/** |
|
|
|
|
46
|
|
|
* @var string Whether the autocomplete should be parsed with . -delimited nested sub-properties |
47
|
|
|
*/ |
48
|
|
|
public $hasSubProperties = false; |
49
|
|
|
|
50
|
|
|
/** |
|
|
|
|
51
|
|
|
* @var ?int The section id |
52
|
|
|
*/ |
53
|
|
|
public $sectionId = null; |
54
|
|
|
|
55
|
|
|
// Public Methods |
56
|
|
|
// ========================================================================= |
57
|
|
|
|
58
|
|
|
/** |
|
|
|
|
59
|
|
|
* @inerhitDoc |
60
|
|
|
*/ |
|
|
|
|
61
|
|
|
public function init(): void |
62
|
|
|
{ |
63
|
|
|
$this->sectionId = $this->twigfieldOptions[self::OPTIONS_DATA_KEY] ?? null; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Core function that generates the autocomplete array |
68
|
|
|
*/ |
|
|
|
|
69
|
|
|
public function generateCompleteItems(): void |
70
|
|
|
{ |
71
|
|
|
if ($this->sectionId) { |
|
|
|
|
72
|
|
|
$sections = Craft::$app->getSections(); |
73
|
|
|
$section = $sections->getSectionById($this->sectionId); |
74
|
|
|
if ($section) { |
75
|
|
|
$entryTypes = $section->getEntryTypes(); |
76
|
|
|
foreach ($entryTypes as $entryType) { |
77
|
|
|
// Add the native fields in |
78
|
|
|
if ($entryType->elementType) { |
79
|
|
|
$element = new $entryType->elementType; |
80
|
|
|
/* @var ElementInterface $element */ |
81
|
|
|
$nativeFields = $element->attributeLabels(); |
82
|
|
|
foreach ($nativeFields as $key => $value) { |
83
|
|
|
CompleteItem::create() |
84
|
|
|
->insertText($key) |
85
|
|
|
->label($key) |
86
|
|
|
->detail(Craft::t('twigfield', 'Field')) |
87
|
|
|
->documentation($element::displayName() . ' ' . $value) |
88
|
|
|
->kind(CompleteItemKind::PropertyKind) |
89
|
|
|
->add($this); |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
// Add the custom fields in |
93
|
|
|
$customFields = $entryType->getCustomFields(); |
94
|
|
|
foreach ($customFields as $customField) { |
95
|
|
|
$name = $customField->handle; |
96
|
|
|
$docs = $customField->instructions ?? ''; |
97
|
|
|
if ($name) { |
98
|
|
|
CompleteItem::create() |
99
|
|
|
->insertText($name) |
100
|
|
|
->label($name) |
101
|
|
|
->detail(Craft::t('twigfield', 'Custom Field')) |
102
|
|
|
->documentation($docs) |
103
|
|
|
->kind(CompleteItemKind::FieldKind) |
104
|
|
|
->add($this); |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|