Skip to content

Commit 085ff65

Browse files
committed
feat: added forms for article and category to opengraph plugin
1 parent 905cca3 commit 085ff65

File tree

6 files changed

+522
-3
lines changed

6 files changed

+522
-3
lines changed

plugins/system/opengraph/language/en-GB/plg_system_opengraph.ini

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,3 +65,112 @@ PLG_SYSTEM_OPENGRAPH_PRIORITY_ARTICLE="Article"
6565
PLG_SYSTEM_OPENGRAPH_PRIORITY_CATEGORY="Category"
6666
PLG_SYSTEM_OPENGRAPH_PRIORITY_GLOBAL="Global"
6767
PLG_SYSTEM_OPENGRAPH_PRIORITY_AUTO="Auto-generated"
68+
69+
; Form Fieldsets
70+
PLG_SYSTEM_OPENGRAPH_FIELDSET_ARTICLE="OpenGraph Settings"
71+
PLG_SYSTEM_OPENGRAPH_FIELDSET_ARTICLE_DESC="Configure OpenGraph metadata for social media sharing. These settings override global defaults."
72+
73+
PLG_SYSTEM_OPENGRAPH_FIELDSET_CATEGORY="OpenGraph Category Settings"
74+
PLG_SYSTEM_OPENGRAPH_FIELDSET_CATEGORY_DESC="Configure OpenGraph metadata for this category. These settings will be used for articles in this category when no article-specific settings are defined."
75+
76+
; Common Field Labels
77+
PLG_SYSTEM_OPENGRAPH_OG_TITLE_LABEL="OpenGraph Title"
78+
PLG_SYSTEM_OPENGRAPH_OG_TITLE_DESC="Custom title for social media sharing. Recommended length: 40-60 characters."
79+
PLG_SYSTEM_OPENGRAPH_OG_TITLE_HINT="Enter a compelling title for social sharing"
80+
81+
PLG_SYSTEM_OPENGRAPH_OG_DESCRIPTION_LABEL="OpenGraph Description"
82+
PLG_SYSTEM_OPENGRAPH_OG_DESCRIPTION_DESC="Brief description for social media sharing. Recommended length: 120-160 characters."
83+
PLG_SYSTEM_OPENGRAPH_OG_DESCRIPTION_HINT="Enter a brief, engaging description"
84+
85+
PLG_SYSTEM_OPENGRAPH_OG_IMAGE_LABEL="OpenGraph Image"
86+
PLG_SYSTEM_OPENGRAPH_OG_IMAGE_DESC="Image for social media sharing. Recommended size: 1200x630 pixels."
87+
88+
PLG_SYSTEM_OPENGRAPH_OG_IMAGE_ALT_LABEL="OpenGraph Image Alt Text"
89+
PLG_SYSTEM_OPENGRAPH_OG_IMAGE_ALT_DESC="Alternative text for the OpenGraph image for accessibility."
90+
91+
PLG_SYSTEM_OPENGRAPH_OG_TYPE_LABEL="OpenGraph Type"
92+
PLG_SYSTEM_OPENGRAPH_OG_TYPE_DESC="Content type for social media platforms."
93+
94+
PLG_SYSTEM_OPENGRAPH_OG_URL_LABEL="Custom URL"
95+
PLG_SYSTEM_OPENGRAPH_OG_URL_DESC="Override the canonical URL for sharing."
96+
PLG_SYSTEM_OPENGRAPH_OG_URL_HINT="Leave empty to use the article's URL"
97+
98+
PLG_SYSTEM_OPENGRAPH_ENABLE_LABEL="Enable OpenGraph"
99+
PLG_SYSTEM_OPENGRAPH_ENABLE_DESC="Enable OpenGraph tags for this content."
100+
101+
; OpenGraph Types
102+
PLG_SYSTEM_OPENGRAPH_USE_DEFAULT="Use Default"
103+
PLG_SYSTEM_OPENGRAPH_TYPE_ARTICLE="Article"
104+
PLG_SYSTEM_OPENGRAPH_TYPE_WEBSITE="Website"
105+
PLG_SYSTEM_OPENGRAPH_TYPE_BLOG="Blog"
106+
PLG_SYSTEM_OPENGRAPH_TYPE_VIDEO="Video"
107+
PLG_SYSTEM_OPENGRAPH_TYPE_MUSIC="Music"
108+
PLG_SYSTEM_OPENGRAPH_TYPE_BOOK="Book"
109+
PLG_SYSTEM_OPENGRAPH_TYPE_PROFILE="Profile"
110+
PLG_SYSTEM_OPENGRAPH_TYPE_PRODUCT="Product"
111+
PLG_SYSTEM_OPENGRAPH_TYPE_EVENT="Event"
112+
113+
; Article Specific
114+
PLG_SYSTEM_OPENGRAPH_ARTICLE_SECTION_LABEL="Article Section"
115+
PLG_SYSTEM_OPENGRAPH_ARTICLE_SECTION_DESC="Section or category tag for the article."
116+
PLG_SYSTEM_OPENGRAPH_ARTICLE_SECTION_HINT="e.g., Technology, Sports, News"
117+
118+
PLG_SYSTEM_OPENGRAPH_PRIORITY_NOTE_LABEL="Priority Information"
119+
PLG_SYSTEM_OPENGRAPH_PRIORITY_NOTE_DESC="These article settings take priority over category and global settings. Leave fields empty to inherit from higher levels."
120+
121+
; Category Specific
122+
PLG_SYSTEM_OPENGRAPH_CATEGORY_TITLE_DESC="Custom title for this category's social media sharing."
123+
PLG_SYSTEM_OPENGRAPH_CATEGORY_TITLE_HINT="Enter a title that represents this category"
124+
125+
PLG_SYSTEM_OPENGRAPH_CATEGORY_DESCRIPTION_DESC="Description for articles in this category when no article-specific description is set."
126+
PLG_SYSTEM_OPENGRAPH_CATEGORY_DESCRIPTION_HINT="Describe what this category contains"
127+
128+
PLG_SYSTEM_OPENGRAPH_CATEGORY_IMAGE_DESC="Default image for articles in this category."
129+
PLG_SYSTEM_OPENGRAPH_CATEGORY_TYPE_DESC="Default content type for articles in this category."
130+
PLG_SYSTEM_OPENGRAPH_CATEGORY_URL_DESC="Custom URL for this category page."
131+
PLG_SYSTEM_OPENGRAPH_CATEGORY_URL_HINT="Leave empty to use the category's URL"
132+
PLG_SYSTEM_OPENGRAPH_CATEGORY_ENABLE_DESC="Enable OpenGraph tags for this category and its articles."
133+
134+
PLG_SYSTEM_OPENGRAPH_INHERIT_PARENT_LABEL="Inherit from Parent"
135+
PLG_SYSTEM_OPENGRAPH_INHERIT_PARENT_DESC="Inherit OpenGraph settings from parent category when available."
136+
137+
PLG_SYSTEM_OPENGRAPH_DEFAULT_ARTICLES_LABEL="Default for Articles"
138+
PLG_SYSTEM_OPENGRAPH_DEFAULT_ARTICLES_DESC="Use these settings as defaults for articles in this category."
139+
140+
PLG_SYSTEM_OPENGRAPH_CATEGORY_SPECIFIC_LABEL="Category-Specific Options"
141+
142+
PLG_SYSTEM_OPENGRAPH_SHOW_CATEGORY_LABEL="Show Category in Articles"
143+
PLG_SYSTEM_OPENGRAPH_SHOW_CATEGORY_DESC="Include category name in article OpenGraph metadata."
144+
145+
PLG_SYSTEM_OPENGRAPH_USE_KEYWORDS_LABEL="Use Category Keywords"
146+
PLG_SYSTEM_OPENGRAPH_USE_KEYWORDS_DESC="Include category keywords in OpenGraph metadata."
147+
148+
PLG_SYSTEM_OPENGRAPH_INHERITANCE_NOTE_LABEL="Inheritance Information"
149+
PLG_SYSTEM_OPENGRAPH_INHERITANCE_NOTE_DESC="Category settings are inherited by articles unless overridden at the article level. Global settings serve as the final fallback."
150+
151+
; Character Counters
152+
PLG_SYSTEM_OPENGRAPH_TITLE_COUNTER_DESC="Character counter for title optimization"
153+
PLG_SYSTEM_OPENGRAPH_DESCRIPTION_COUNTER_DESC="Character counter for description optimization"
154+
155+
; Note Fields
156+
PLG_SYSTEM_OPENGRAPH_CATEGORY_NOTE_LABEL="Category Settings Note"
157+
PLG_SYSTEM_OPENGRAPH_CATEGORY_NOTE_DESC="These settings apply to this category and can be inherited by articles within it."
158+
159+
; Additional Missing Constants
160+
PLG_SYSTEM_OPENGRAPH_CATEGORY_SETTINGS="Category OpenGraph Settings"
161+
PLG_SYSTEM_OPENGRAPH_GLOBAL_SETTINGS="Global OpenGraph Settings"
162+
PLG_SYSTEM_OPENGRAPH_ARTICLE_SETTINGS="Article OpenGraph Settings"
163+
164+
; Default values text
165+
PLG_SYSTEM_OPENGRAPH_DEFAULT_TITLE="Use site title"
166+
PLG_SYSTEM_OPENGRAPH_DEFAULT_DESCRIPTION="Use site description"
167+
PLG_SYSTEM_OPENGRAPH_DEFAULT_IMAGE="Use site logo"
168+
169+
; Status messages for category fields
170+
PLG_SYSTEM_OPENGRAPH_CATEGORY_ENABLED="OpenGraph enabled for this category"
171+
PLG_SYSTEM_OPENGRAPH_CATEGORY_DISABLED="OpenGraph disabled for this category"
172+
PLG_SYSTEM_OPENGRAPH_CATEGORY_INHERIT="Inherit from global settings"
173+
174+
; Help text for complex fields
175+
PLG_SYSTEM_OPENGRAPH_TYPE_HELP="Select the content type that best describes this category. This affects how social media platforms display shared links."
176+
PLG_SYSTEM_OPENGRAPH_IMAGE_HELP="Upload an image that represents this category. Recommended dimensions: 1200x630 pixels for optimal display on social media."

plugins/system/opengraph/opengraph.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
<files>
1515
<folder plugin="opengraph">src</folder>
16+
<folder>services</folder>
1617
<filename>opengraph.xml</filename>
1718
</files>
1819

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
/**
4+
* @package Joomla.Plugin
5+
* @subpackage System.Opengraph
6+
*
7+
* @copyright (C) 2024 Open Source Matters, Inc. <https://www.joomla.org>
8+
* @license GNU General Public License version 2 or later; see LICENSE.txt
9+
*/
10+
11+
\defined('_JEXEC') or die;
12+
13+
use Joomla\CMS\Extension\PluginInterface;
14+
use Joomla\CMS\Factory;
15+
use Joomla\CMS\Plugin\PluginHelper;
16+
use Joomla\DI\Container;
17+
use Joomla\DI\ServiceProviderInterface;
18+
use Joomla\Event\DispatcherInterface;
19+
use Joomla\Plugin\System\Opengraph\Extension\Opengraph;
20+
21+
return new class () implements ServiceProviderInterface {
22+
/**
23+
* Registers the service provider with a DI container.
24+
*
25+
* @param Container $container The DI container.
26+
*
27+
* @return void
28+
*
29+
* @since __DEPLOY_VERSION__
30+
*/
31+
public function register(Container $container)
32+
{
33+
$container->set(
34+
PluginInterface::class,
35+
function (Container $container) {
36+
$plugin = new Opengraph(
37+
$container->get(DispatcherInterface::class),
38+
(array) PluginHelper::getPlugin('system', 'opengraph')
39+
);
40+
$plugin->setApplication(Factory::getApplication());
41+
42+
return $plugin;
43+
}
44+
);
45+
}
46+
};

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

Lines changed: 119 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,12 @@
99

1010
namespace Joomla\Plugin\System\Opengraph\Extension;
1111

12+
use Joomla\CMS\Application\CMSApplication;
1213
use Joomla\CMS\Event\Application\BeforeCompileHeadEvent;
14+
use Joomla\CMS\Event\Model\PrepareFormEvent;
1315
use Joomla\CMS\Plugin\CMSPlugin;
1416
use Joomla\Event\SubscriberInterface;
17+
use Joomla\CMS\Form\Form;
1518

1619
// phpcs:disable PSR1.Files.SideEffects
1720
\defined('_JEXEC') or die;
@@ -24,15 +27,128 @@
2427
* @since __DEPLOY_VERSION__
2528
*/
2629

27-
final class PlgSystemOpengraph extends CMSPlugin implements SubscriberInterface
30+
final class Opengraph extends CMSPlugin implements SubscriberInterface
2831
{
29-
30-
32+
/**
33+
* Returns an array of events this subscriber will listen to.
34+
*
35+
* @return array
36+
*
37+
* @since __DEPLOY_VERSION__
38+
*/
3139
public static function getSubscribedEvents(): array
3240
{
3341
return [
3442
'onBeforeCompileHead' => 'onBeforeCompileHead',
43+
'onContentPrepareForm' => 'onContentPrepareForm',
3544
];
3645
}
3746

47+
/**
48+
* Application object
49+
*
50+
* @var CMSApplication
51+
* @since __DEPLOY_VERSION__
52+
*/
53+
protected $app;
54+
55+
/**
56+
* Load the language file on instantiation.
57+
*
58+
* @var boolean
59+
* @since __DEPLOY_VERSION__
60+
*/
61+
protected $autoloadLanguage = true;
62+
63+
/**
64+
* Method to handle the onBeforeCompileHead event
65+
*
66+
* @param BeforeCompileHeadEvent $event The event object
67+
*
68+
* @return void
69+
*
70+
* @since __DEPLOY_VERSION__
71+
*/
72+
public function onBeforeCompileHead(BeforeCompileHeadEvent $event)
73+
{
74+
$app = $this->getApplication();
75+
76+
// Only run in frontend
77+
if (!$app->isClient('site')) {
78+
return;
79+
}
80+
81+
// Will add OpenGraph meta tags logic here in the future
82+
// This method will be called to generate OpenGraph tags
83+
}
84+
85+
/**
86+
* Method to add OpenGraph fields to content forms
87+
*
88+
* @param PrepareFormEvent $event The event object.
89+
*
90+
* @return void
91+
*
92+
* @since __DEPLOY_VERSION__
93+
*/
94+
public function onContentPrepareForm(PrepareFormEvent $event): void
95+
{
96+
$app = $this->getApplication();
97+
98+
if (!$app->isClient('administrator')) {
99+
return;
100+
}
101+
102+
$form = $event->getForm();
103+
$data = $event->getData();
104+
105+
$name = $form->getName();
106+
107+
108+
109+
// Check if it's the article form
110+
if ($name === 'com_content.article') {
111+
$xmlFile = __DIR__ . '/../Forms/Article/Article.xml';
112+
if (file_exists($xmlFile)) {
113+
$result = $form->loadFile($xmlFile, false);
114+
115+
}
116+
}
117+
118+
// Check if it's the category form
119+
if (strpos($name, 'com_categories.category') === 0) {
120+
$extension = '';
121+
122+
// Extract extension from form name if it's concatenated
123+
if ($name === 'com_categories.categorycom_content') {
124+
$extension = 'com_content';
125+
} else {
126+
// Try to get extension from different sources
127+
if (is_object($data) && isset($data->extension)) {
128+
$extension = $data->extension;
129+
} elseif (is_array($data) && isset($data['extension'])) {
130+
$extension = $data['extension'];
131+
} else {
132+
$extension = $form->getValue('extension');
133+
}
134+
135+
// If still empty, try from request
136+
if (empty($extension)) {
137+
$input = $this->getApplication()->input;
138+
$extension = $input->get('extension', '', 'cmd');
139+
}
140+
}
141+
142+
143+
144+
// Load form only for content categories
145+
if ($extension === 'com_content') {
146+
$xmlFile = __DIR__ . '/../Forms/Category/Category.xml';
147+
if (file_exists($xmlFile)) {
148+
$result = $form->loadFile($xmlFile, false);
149+
150+
}
151+
}
152+
}
153+
}
38154
}

0 commit comments

Comments
 (0)