Making Tags required for Joomla Articles in specific Categories
Moderator: General Support Moderators
Forum rules
Forum Rules
Absolute Beginner's Guide to Joomla! <-- please read before posting.
Forum Post Assistant - If you are serious about wanting help, you should use this tool to help you post.
Windows Defender SmartScreen Issues <-- please read this if using Windows 10
Forum Rules
Absolute Beginner's Guide to Joomla! <-- please read before posting.
Forum Post Assistant - If you are serious about wanting help, you should use this tool to help you post.
Windows Defender SmartScreen Issues <-- please read this if using Windows 10
- sseguin
- Joomla! Enthusiast
- Posts: 130
- Joined: Sat Apr 10, 2010 2:24 am
- Contact:
Making Tags required for Joomla Articles in specific Categories
Hello, I am wondering if there is a way to make adding a tag required when creating an article in a specific category.
From everything I can see from trial and error it doesn't seem like it but wondering if I am missing something or there is a plugin or something that can enable this functionality.
Thanks for your help.
From everything I can see from trial and error it doesn't seem like it but wondering if I am missing something or there is a plugin or something that can enable this functionality.
Thanks for your help.
https://www.SlopeEdge.ca - Dedicated to getting you up close and personal with skiing and adventure
https://www.RailFans.ca - Canada's Public Transit Rail Enthusiasts
https://www.RailFans.ca - Canada's Public Transit Rail Enthusiasts
-
- Joomla! Hero
- Posts: 2954
- Joined: Sun May 04, 2008 12:37 pm
Re: Making Tags required for Joomla Articles in specific Categories
you may try adding these to your fields in your overrides
class="required"
required-"required"
area-required="true"
Just picked them from a default joomla contact form... there is works...
class="required"
required-"required"
area-required="true"
Just picked them from a default joomla contact form... there is works...
- sseguin
- Joomla! Enthusiast
- Posts: 130
- Joined: Sat Apr 10, 2010 2:24 am
- Contact:
Re: Making Tags required for Joomla Articles in specific Categories
Hello, I am asking for Joomla Article manager when editing an article, and making the requirement specific to one category only.
https://www.SlopeEdge.ca - Dedicated to getting you up close and personal with skiing and adventure
https://www.RailFans.ca - Canada's Public Transit Rail Enthusiasts
https://www.RailFans.ca - Canada's Public Transit Rail Enthusiasts
-
- Joomla! Hero
- Posts: 2954
- Joined: Sun May 04, 2008 12:37 pm
Re: Making Tags required for Joomla Articles in specific Categories
You may do that using some custom jQuery addClass() or attr()
But you may want to have this jQuery dynamically created by a plugin so you can manage the tags involved from within Joomla Admin.
But you may want to have this jQuery dynamically created by a plugin so you can manage the tags involved from within Joomla Admin.
-
- Joomla! Hero
- Posts: 2909
- Joined: Fri Jul 05, 2013 10:35 am
- Location: Parts Unknown
Re: Making Tags required for Joomla Articles in specific Categories
This can and should be done with a content plugin.
In the plugin's manifest file add a category field:
Edit the plugin in Plugin Manager to select which categories should require tags.
https://docs.joomla.org/Category:Plugin_Development
Code: Select all
defined('_JEXEC') or die;
use Joomla\CMS\Form\Form;
use Joomla\CMS\Plugin\CMSPlugin;
class PlgContentExample extends CMSPlugin
{
protected $app;
public function onContentPrepareForm(Form $form, $data)
{
if (!$this->params->get('categories', array()) || $form->getName() !== 'com_content.article')
{
return true;
}
if (!$data)
{
$data = $this->app->input->get('jform', array(), 'array');
}
if (is_array($data))
{
$data = (object) $data;
}
if (isset($data->catid) && in_array($data->catid, $this->params->get('categories', array())))
{
$form->setFieldAttribute('tags', 'required', 'true');
}
return true;
}
Code: Select all
<config>
<fields name="params">
<fieldset name="basic">
<field
name="categories"
label="JCATEGORIES"
type="category"
extension="com_content"
multiple="true"
filter="uint"
validate="options"
/>
</fieldset>
</fields>
</config>
https://docs.joomla.org/Category:Plugin_Development
Last edited by SharkyKZ on Fri Nov 16, 2018 4:55 pm, edited 1 time in total.
-
- Joomla! Hero
- Posts: 2954
- Joined: Sun May 04, 2008 12:37 pm
Re: Making Tags required for Joomla Articles in specific Categories
@SharkyKZ oh wow that is awesome.. something I want to try... tx.
-
- Joomla! Hero
- Posts: 2954
- Joined: Sun May 04, 2008 12:37 pm
Re: Making Tags required for Joomla Articles in specific Categories
I checked this but since the choice for the category is made during authoring, the field will not get the required attribute..
It cannot as the plugin is not triggered during edit.
Also it doesn't add the attribute when opening an existing article in the specified category. Something I would expect since it has the onContentPrepareForm() event.
The article will just be saved without the tag set.
Wouldn't we need some onContentSave() event for this too?
Your thoughts?
It cannot as the plugin is not triggered during edit.
Also it doesn't add the attribute when opening an existing article in the specified category. Something I would expect since it has the onContentPrepareForm() event.
The article will just be saved without the tag set.
Wouldn't we need some onContentSave() event for this too?
Your thoughts?
-
- Joomla! Hero
- Posts: 2909
- Joined: Fri Jul 05, 2013 10:35 am
- Location: Parts Unknown
Re: Making Tags required for Joomla Articles in specific Categories
When creating a new article and the first category in category list happens to be selected in the plugin, the required attribute is not added. But articles without tags aren't saved. So server-side validation works OK.
To solve the display issue, perhaps a check like this could be used:
To solve the display issue, perhaps a check like this could be used:
Code: Select all
if (isset($data->catid))
{
$catid = $data->catid;
}
else
{
$field = $form->getField('catid');
if ($field->value)
{
$catid = $field->value;
}
else
{
$catid = $field->options[0]->value;
}
}
if ($catid && in_array($catid, $this->params->get('categories', array())))
{
$form->setFieldAttribute('tags', 'required', 'true');
}
-
- Joomla! Hero
- Posts: 2954
- Joined: Sun May 04, 2008 12:37 pm
Re: Making Tags required for Joomla Articles in specific Categories
When you create a new article, the category selected at first is "uncategorized"...
You then select a category it belongs too.. THEN the plugin is supposed to kick in and not save if category matches the one set in the plugin params... page is to remain as is with top message "article not saved..."
You then select a category it belongs too.. THEN the plugin is supposed to kick in and not save if category matches the one set in the plugin params... page is to remain as is with top message "article not saved..."
-
- Joomla! Hero
- Posts: 2954
- Joined: Sun May 04, 2008 12:37 pm
Re: Making Tags required for Joomla Articles in specific Categories
Code: Select all
public function onContentPrepareForm(Form $form, $data)
{
if (!$this->params->get('categories', array()) || $form->getName() !== 'com_content.article')
{
Code: Select all
public function onContentPrepareForm($form, $data)
{
if (!$this->params->get('categories', array()) || $form->getName() != 'com_content.article')
{
Issues remaining:
- this does not work on new articles as category is set to "uncategorized" unless "uncategorized" is set to have a mandatory tag too.
- this does not work when opening an existing article in a set category and one changes category to one that does not require a tag.
- Error messages are not consistent.
-
- Joomla! Hero
- Posts: 2954
- Joined: Sun May 04, 2008 12:37 pm
Re: Making Tags required for Joomla Articles in specific Categories
Thanks to SharkyKZ who pointed me in the right direction I think I got it. It needed some extra jQuery added so it also would function while editing and switching categories during editing.
I ported this to github as I think this is a better place for code. See: https://github.com/pieter-groeneweg/joo ... tag-plugin
Feel free to add your comments.
I ported this to github as I think this is a better place for code. See: https://github.com/pieter-groeneweg/joo ... tag-plugin
Feel free to add your comments.
-
- Joomla! Hero
- Posts: 2909
- Joined: Fri Jul 05, 2013 10:35 am
- Location: Parts Unknown
Re: Making Tags required for Joomla Articles in specific Categories
Unless you are using an outdated version of Joomla, using custom JS should not be required. Changing the category reloads the entire form with the new attributes taken into account.
-
- Joomla! Hero
- Posts: 2954
- Joined: Sun May 04, 2008 12:37 pm
Re: Making Tags required for Joomla Articles in specific Categories
3.9 is not exactly outdated. The form is NOT reloaded on change of category.
You can check the behaviour of the plugin WITHOUT the jQuery part.
1. Create new article
2. change category to one that requires a TAG, no asterisk set
3. Save -> Not saved, TAG required
4. Set TAG(s)
5. Save -> Article saved
Works fine but for the asterisk in the label.
6. Open same article.
7. Change category to one that does NOT require a TAG, asterisk not removed
8. Delete TAG(s)
9. Save -> NOT saved, Invalid TAG field.
Doesn't work as expected.
This happens because the classes, properties and attributes are not dynamically changed on the label and the form field. Nor is the "mandatory" asterisk toggled.
The jQuery script does take care of all that.
I only need to add this plugin is only tested working on ISIS admin template.
You can check the behaviour of the plugin WITHOUT the jQuery part.
1. Create new article
2. change category to one that requires a TAG, no asterisk set
3. Save -> Not saved, TAG required
4. Set TAG(s)
5. Save -> Article saved
Works fine but for the asterisk in the label.
6. Open same article.
7. Change category to one that does NOT require a TAG, asterisk not removed
8. Delete TAG(s)
9. Save -> NOT saved, Invalid TAG field.
Doesn't work as expected.
This happens because the classes, properties and attributes are not dynamically changed on the label and the form field. Nor is the "mandatory" asterisk toggled.
The jQuery script does take care of all that.
I only need to add this plugin is only tested working on ISIS admin template.
-
- Joomla! Hero
- Posts: 2909
- Joined: Fri Jul 05, 2013 10:35 am
- Location: Parts Unknown
Re: Making Tags required for Joomla Articles in specific Categories
Should note that form reload is handled by System - Fields plugin. Maybe it's disabled on your site? Either that or some JS error.
Regardless, you're right. Custom JS is indeed needed for production. So there would be no dependency on another plugin. Still, I'd use the form reload instead of manually changing the attributes.
Regardless, you're right. Custom JS is indeed needed for production. So there would be no dependency on another plugin. Still, I'd use the form reload instead of manually changing the attributes.
-
- Joomla! Hero
- Posts: 2954
- Joined: Sun May 04, 2008 12:37 pm
Re: Making Tags required for Joomla Articles in specific Categories
System Fields plugin is enabled, but isn't that for custom fields? Since I do not have a custom field set on the article edit form, nothing will happen based on that plugin then.
Also Category dropdown is not a custom field.
If that is the case (I would need to test WITH a custom field) then indeed the jQuery is needed for dependency reasons...
Also Category dropdown is not a custom field.
If that is the case (I would need to test WITH a custom field) then indeed the jQuery is needed for dependency reasons...