Skip to content

Commit 8ad6e20

Browse files
committed
feat : add ogtest field plugin
1 parent 9db1b2c commit 8ad6e20

File tree

4 files changed

+93
-0
lines changed

4 files changed

+93
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,3 +107,5 @@ cypress.config.mjs
107107

108108
# WebAuthn FIDO metadata cache
109109
/plugins/system/webauthn/fido.jwt
110+
111+
/plugins/fields/ogtest

libraries/src/Opengraph/OpengraphServiceInterface.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,18 @@ interface OpengraphServiceInterface
3131
*/
3232
public function getOpengraphFields(): array;
3333
}
34+
35+
36+
37+
38+
interface MappableFieldInterface
39+
{
40+
/**
41+
* Returns the OpenGraph group this field should be listed under.
42+
*
43+
* @return OpengraphGroup One of the enum cases defined in {@see OpengraphGroup}.
44+
*
45+
* @since __DEPLOY_VERSION__
46+
*/
47+
public static function getOpengraphGroup(): OpengraphGroup;
48+
}

plugins/system/opengraph/src/Extension/opengraph.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,8 @@ public function onBeforeCompileHead(BeforeCompileHeadEvent $event): void
270270
$category = $categoryModel->getCategory();
271271

272272

273+
274+
273275
// Get menu parameters
274276
$menuParams = $this->getMenuParams();
275277
$articleAttribs = new Registry($article->attribs ?? '{}');

plugins/system/opengraph/src/Field/OpengraphField.php

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,80 @@ protected function getGroups()
7575
}
7676

7777

78+
if (!$component instanceof FieldsServiceInterface) {
79+
return $groups;
80+
}
81+
82+
// Allowed field types for each OpenGraph group
83+
$allowedFieldTypes = [
84+
OpengraphGroup::TEXT->value => ['text', 'textarea'],
85+
OpengraphGroup::IMAGE->value => ['media', 'imagelist'],
86+
OpengraphGroup::IMAGE_ALT->value => ['text'],
87+
];
88+
89+
$nativeTypes = $allowedFieldTypes[$fieldType] ?? [];
90+
91+
92+
93+
$catId = (int) $this->form->getValue('id'); // editing existing cat
94+
if (!$catId) {
95+
// Creating a new category: use the chosen parent so assignments still work
96+
$catId = (int) $this->form->getValue('parent_id');
97+
}
98+
99+
// Dummy item with catid so FieldsService filters by assignment
100+
$scopeItem = $catId ? (object) ['catid' => $catId] : null;
101+
102+
$customFields = FieldsHelper::getFields('com_content.article', $scopeItem);
103+
$customOptions = [];
104+
105+
foreach ($customFields as $field) {
106+
$accept = \in_array($field->type, $nativeTypes, true);
107+
108+
109+
// If not native-allowed, see if the field’s plugin implements our interface
110+
if (!$accept) {
111+
112+
// Ensure the specific fields plugin is loaded
113+
PluginHelper::importPlugin('fields', $field->type);
114+
115+
$ucType = ucfirst((string) $field->type);
116+
117+
// Candidate class names in priority order (modern first, then legacy)
118+
$candidates = [
119+
"Joomla\\Plugin\\Fields\\{$ucType}\\Extension\\{$ucType}", // J4/5 namespaced
120+
"Joomla\\Plugin\\Fields\\{$ucType}\\Field\\{$ucType}Field", // some third-party patterns
121+
"PlgFields{$ucType}", // legacy non-namespaced
122+
];
123+
124+
$implements = false;
125+
126+
foreach ($candidates as $fqcn) {
127+
if (\class_exists($fqcn) && \is_subclass_of($fqcn, MappableFieldInterface::class)) {
128+
$implements = ($fqcn::getOpengraphGroup()->value === $fieldType);
129+
if ($implements) {
130+
$accept = true;
131+
break;
132+
}
133+
}
134+
}
135+
}
136+
137+
138+
if (!$accept) {
139+
continue;
140+
}
141+
142+
143+
144+
$label = $field->title . ' (' . $field->name . ')';
145+
$customOptions[] = HTMLHelper::_('select.option', 'field.' . $field->name, $label);
146+
}
147+
148+
if (!empty($customOptions)) {
149+
$groups['Custom Fields'] = $customOptions;
150+
}
151+
78152
return $groups;
79153
}
80154
}

0 commit comments

Comments
 (0)