Skip to content

Commit 768a2bf

Browse files
committed
feat : created opengraph service interface and field type for opengraph plugin
1 parent 7ba727c commit 768a2bf

File tree

5 files changed

+145
-9
lines changed

5 files changed

+145
-9
lines changed

administrator/components/com_content/src/Extension/ContentComponent.php

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
use Joomla\CMS\Helper\ContentHelper as LibraryContentHelper;
2626
use Joomla\CMS\HTML\HTMLRegistryAwareTrait;
2727
use Joomla\CMS\Language\Text;
28+
use Joomla\CMS\Opengraph\OpengraphServiceInterface;
2829
use Joomla\CMS\Schemaorg\SchemaorgServiceInterface;
2930
use Joomla\CMS\Schemaorg\SchemaorgServiceTrait;
3031
use Joomla\CMS\Tag\TagServiceInterface;
@@ -53,7 +54,8 @@ class ContentComponent extends MVCComponent implements
5354
SchemaorgServiceInterface,
5455
WorkflowServiceInterface,
5556
RouterServiceInterface,
56-
TagServiceInterface
57+
TagServiceInterface,
58+
OpengraphServiceInterface
5759
{
5860
use AssociationServiceTrait;
5961
use RouterServiceTrait;
@@ -204,6 +206,30 @@ public function getSchemaorgContexts(): array
204206
return $contexts;
205207
}
206208

209+
210+
/**
211+
* Returns valid contexts for opengraph
212+
*
213+
* @return array
214+
*
215+
* @since __DEPLOY_VERSION__
216+
*/
217+
public function getOpengraphFields(): array
218+
{
219+
Factory::getLanguage()->load('com_content', JPATH_ADMINISTRATOR);
220+
221+
$fields = [
222+
'title' => Text::_('JGLOBAL_TITLE'),
223+
'articletext' => Text::_('COM_CONTENT_FIELD_ARTICLETEXT_LABEL'),
224+
225+
// 'image_intro' => Text::_('COM_CONTENT_FIELD_INTRO_LABEL'),
226+
// 'image_intro_alt' => Text::_('COM_CONTENT_FIELD_ARTICLETEXT_LABEL'),
227+
];
228+
229+
return $fields;
230+
}
231+
232+
207233
/**
208234
* Returns valid contexts
209235
*
@@ -295,7 +321,6 @@ public function getModelName($context): string
295321

296322
return ucfirst($modelname);
297323
}
298-
299324
/**
300325
* Method to filter transitions by given id of state.
301326
*
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
/**
4+
* Joomla! Content Management System
5+
*
6+
* @copyright (C) 2025 Open Source Matters, Inc. <https://www.joomla.org>
7+
* @license GNU General Public License version 2 or later; see LICENSE.txt
8+
*/
9+
10+
namespace Joomla\CMS\Opengraph;
11+
12+
// phpcs:disable PSR1.Files.SideEffects
13+
\defined('_JEXEC') or die;
14+
// phpcs:enable PSR1.Files.SideEffects
15+
16+
/**
17+
* The Opengraph service.
18+
*
19+
* @since __DEPLOY_VERSION__
20+
*/
21+
interface OpengraphServiceInterface
22+
{
23+
/**
24+
* Returns valid contexts.
25+
*
26+
* @return array
27+
*
28+
* @since __DEPLOY_VERSION__
29+
*/
30+
public function getOpengraphFields(): array;
31+
}

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

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -132,11 +132,20 @@ public function onContentPrepareForm(PrepareFormEvent $event): void
132132
$form = $event->getForm();
133133
$name = $form->getName();
134134

135-
if ($name === 'com_content.article') {
136-
$xml = __DIR__ . '/../forms/opengraph.xml';
137-
if (file_exists($xml)) {
138-
$form->loadFile($xml, false);
139-
}
135+
//todo : replace with interface check
136+
$supportedForms = [
137+
'com_content.article',
138+
'com_categories.categorycom_content',
139+
'com_menus.item'
140+
];
141+
142+
if (!in_array($name, $supportedForms, true)) {
143+
return;
144+
}
145+
146+
$xml = __DIR__ . '/../forms/opengraph.xml';
147+
if (file_exists($xml)) {
148+
$form->loadFile($xml, false);
140149
}
141150
}
142151

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?php
2+
3+
/**
4+
* Joomla! Content Management System
5+
*
6+
* @copyright (C) 2009 Open Source Matters, Inc. <https://www.joomla.org>
7+
* @license GNU General Public License version 2 or later; see LICENSE.txt
8+
*/
9+
10+
namespace Joomla\Plugin\System\Opengraph\Field;
11+
12+
use Joomla\CMS\Extension\Component;
13+
use Joomla\CMS\Factory;
14+
use Joomla\CMS\Form\Field\ListField;
15+
use Joomla\CMS\Opengraph\OpengraphServiceInterface;
16+
17+
// phpcs:disable PSR1.Files.SideEffects
18+
\defined('_JEXEC') or die;
19+
// phpcs:enable PSR1.Files.SideEffects
20+
21+
/**
22+
* Form Field class for the Joomla Platform.
23+
* Supports a generic list of options.
24+
*
25+
* @since 1.7.0
26+
*/
27+
28+
class OpengraphField extends ListField
29+
{
30+
/**
31+
* The form field type.
32+
*
33+
* @var string
34+
* @since 1.7.0
35+
*/
36+
protected $type = 'Opengraph';
37+
38+
39+
/**
40+
* Method to get the field options.
41+
*
42+
* @return object[] The field option objects.
43+
*
44+
* @since 3.7.0
45+
*/
46+
protected function getOptions()
47+
{
48+
49+
$app = Factory::getApplication();
50+
$options = parent::getOptions();
51+
//todo : make this more modular or flexible
52+
$component = $app->bootComponent('com_content');
53+
if (!$component instanceof OpengraphServiceInterface) {
54+
return $options;
55+
}
56+
57+
$fields = $component->getOpengraphFields();
58+
foreach ($fields as $value => $text) {
59+
$tmp = [
60+
'value' => $value,
61+
'text' => $text
62+
63+
];
64+
//todo : ordering of the fields
65+
array_unshift($options, $tmp);
66+
}
67+
68+
return $options;
69+
}
70+
}

plugins/system/opengraph/src/forms/opengraph.xml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@
33
<fieldset name="opengraph"
44
label="PLG_SYSTEM_OPENGRAPH_FIELDSET_ARTICLE">
55
<fieldset name="opengraph_field_mapping"
6-
label="PLG_SYSTEM_OPENGRAPH_FIELD_MAPPING_SECTION">
6+
label="PLG_SYSTEM_OPENGRAPH_FIELD_MAPPING_SECTION"
7+
addfieldprefix="Joomla\Plugin\System\Opengraph\Field">
78
<field name="og_title_field"
8-
type="list"
9+
type="opengraph"
910
label="PLG_SYSTEM_OPENGRAPH_TITLE_FIELD_LABEL"
1011
description="PLG_SYSTEM_OPENGRAPH_TITLE_FIELD_DESC"
1112
class="form-select">

0 commit comments

Comments
 (0)