2222use Joomla \CMS \Document \Document ;
2323use Joomla \CMS \Opengraph \OpengraphServiceInterface ;
2424use Joomla \CMS \Uri \Uri ;
25+ use Exception ;
2526
2627
2728
@@ -124,34 +125,43 @@ public function onBeforeCompileHead(BeforeCompileHeadEvent $event): void
124125 */
125126 public function onContentPrepareForm (PrepareFormEvent $ event ): void
126127 {
127- $ app = $ this ->getApplication ();
128+ $ form = $ event ->getForm ();
129+ $ context = $ form ->getName ();
130+ $ app = $ this ->getApplication ();
128131
129- if (!$ app ->isClient ('administrator ' )) {
132+ if (!$ app ->isClient ('administrator ' ) || ! $ this -> isSupported ( $ context ) ) {
130133 return ;
131134 }
132135
133- $ form = $ event ->getForm ();
134- $ context = $ form ->getName ();
135- $ component = $ app ->bootComponent ('com_content ' );
136- if (!$ component instanceof OpengraphServiceInterface) {
137- return ;
138- }
136+ $ isCategory = $ this ->isCategoryContext ($ context );
137+ $ groupName = $ isCategory ? 'params ' : 'attribs ' ;
139138
140- // Check if this is a category context - load mappings form first for categories
141- if ($ this -> isCategoryContext ( $ context ) ) {
139+ // Load and modify opengraphmappings.xml (only for categories)
140+ if ($ isCategory ) {
142141 $ mappingsXml = __DIR__ . '/../forms/opengraphmappings.xml ' ;
143142 if (file_exists ($ mappingsXml )) {
144- $ form ->loadFile ($ mappingsXml , false );
143+ try {
144+ $ modifiedXml = $ this ->adjustFieldsGroup ($ mappingsXml , $ groupName );
145+ $ form ->load ($ modifiedXml , false );
146+ } catch (Exception $ e ) {
147+ error_log ('OpenGraph Plugin: Failed to load mappings form: ' . $ e ->getMessage ());
148+ }
145149 }
146150 }
147151
148- // Load the main OpenGraph form for all supported contexts
152+ // Load and modify opengraph.xml
149153 $ mainXml = __DIR__ . '/../forms/opengraph.xml ' ;
150154 if (file_exists ($ mainXml )) {
151- $ form ->loadFile ($ mainXml , false );
155+ try {
156+ $ modifiedXml = $ this ->adjustFieldsGroup ($ mainXml , $ groupName );
157+ $ form ->load ($ modifiedXml , false );
158+ } catch (Exception $ e ) {
159+ error_log ('OpenGraph Plugin: Failed to load main form: ' . $ e ->getMessage ());
160+ }
152161 }
153162 }
154163
164+
155165 /**
156166 * Inject the OpenGraph data into the document.
157167 *
@@ -225,6 +235,40 @@ private function setOpenGraphImage(Document $document, ?string $image, ?string $
225235 }
226236
227237
238+ /**
239+ * Check if the current plugin should execute opengraph related activities
240+ *
241+ * @param string $context
242+ *
243+ * @return boolean
244+ *
245+ * @since __DEPLOY_VERSION__
246+ */
247+ protected function isSupported ($ context ): bool
248+ {
249+ // @todo: will be changed in future to support other components
250+
251+
252+
253+ $ supportedContexts = [
254+ 'com_content.article ' ,
255+ 'com_categories.categorycom_content ' ,
256+ ];
257+
258+ if (!in_array ($ context , $ supportedContexts , true )) {
259+ return false ;
260+ }
261+
262+ //todo : currently we have interface in com_content only but we need to have it in other components
263+
264+ // $parts = explode('.', $context, 2);
265+
266+ // $component = $this->getApplication()->bootComponent($parts[0]);
267+
268+ // return $component instanceof OpengraphServiceInterface;
269+
270+ return true ;
271+ }
228272
229273 /**
230274 * Check if the context is a category context.
@@ -243,4 +287,21 @@ private function isCategoryContext(string $context): bool
243287
244288 return in_array ($ context , $ categoryContexts , true );
245289 }
290+
291+ private function adjustFieldsGroup (string $ filePath , string $ newGroup ): string
292+ {
293+ $ xmlContent = file_get_contents ($ filePath );
294+ $ xml = simplexml_load_string ($ xmlContent );
295+
296+ if ($ xml === false ) {
297+ throw new Exception ("Could not load XML file: {$ filePath }" );
298+ }
299+
300+ // Adjust all <fields> nodes to use the desired group
301+ foreach ($ xml ->xpath ('//fields ' ) as $ fields ) {
302+ $ fields ['name ' ] = $ newGroup ;
303+ }
304+
305+ return $ xml ->asXML ();
306+ }
246307}
0 commit comments