Making Tags required for Joomla Articles in specific Categories

General questions relating to Joomla! 3.x.

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
Locked
User avatar
sseguin
Joomla! Enthusiast
Joomla! Enthusiast
Posts: 130
Joined: Sat Apr 10, 2010 2:24 am
Contact:

Making Tags required for Joomla Articles in specific Categories

Post by sseguin » Fri Nov 16, 2018 2:18 pm

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.
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

waarnemer
Joomla! Hero
Joomla! Hero
Posts: 2954
Joined: Sun May 04, 2008 12:37 pm

Re: Making Tags required for Joomla Articles in specific Categories

Post by waarnemer » Fri Nov 16, 2018 3:03 pm

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...

User avatar
sseguin
Joomla! Enthusiast
Joomla! Enthusiast
Posts: 130
Joined: Sat Apr 10, 2010 2:24 am
Contact:

Re: Making Tags required for Joomla Articles in specific Categories

Post by sseguin » Fri Nov 16, 2018 3:14 pm

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

waarnemer
Joomla! Hero
Joomla! Hero
Posts: 2954
Joined: Sun May 04, 2008 12:37 pm

Re: Making Tags required for Joomla Articles in specific Categories

Post by waarnemer » Fri Nov 16, 2018 3:34 pm

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.

SharkyKZ
Joomla! Hero
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

Post by SharkyKZ » Fri Nov 16, 2018 3:53 pm

This can and should be done with a content plugin.

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;
	}
In the plugin's manifest file add a category field:

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>
Edit the plugin in Plugin Manager to select which categories should require tags.

https://docs.joomla.org/Category:Plugin_Development
Last edited by SharkyKZ on Fri Nov 16, 2018 4:55 pm, edited 1 time in total.

waarnemer
Joomla! Hero
Joomla! Hero
Posts: 2954
Joined: Sun May 04, 2008 12:37 pm

Re: Making Tags required for Joomla Articles in specific Categories

Post by waarnemer » Fri Nov 16, 2018 4:01 pm

@SharkyKZ oh wow that is awesome.. something I want to try... tx.

waarnemer
Joomla! Hero
Joomla! Hero
Posts: 2954
Joined: Sun May 04, 2008 12:37 pm

Re: Making Tags required for Joomla Articles in specific Categories

Post by waarnemer » Fri Nov 16, 2018 4:39 pm

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?

SharkyKZ
Joomla! Hero
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

Post by SharkyKZ » Fri Nov 16, 2018 10:46 pm

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:

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');
}

waarnemer
Joomla! Hero
Joomla! Hero
Posts: 2954
Joined: Sun May 04, 2008 12:37 pm

Re: Making Tags required for Joomla Articles in specific Categories

Post by waarnemer » Sat Nov 17, 2018 1:08 pm

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..."

waarnemer
Joomla! Hero
Joomla! Hero
Posts: 2954
Joined: Sun May 04, 2008 12:37 pm

Re: Making Tags required for Joomla Articles in specific Categories

Post by waarnemer » Tue Nov 20, 2018 2:24 pm

Code: Select all

	public function onContentPrepareForm(Form $form, $data)
	{
		if (!$this->params->get('categories', array()) || $form->getName() !== 'com_content.article')
		{
should be

Code: Select all

	public function onContentPrepareForm($form, $data)
	{
		if (!$this->params->get('categories', array()) || $form->getName() != 'com_content.article')
		{
Then the plugin is triggered in my setup.

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.
I think this can only be solved using a soft method using javascript validation.

waarnemer
Joomla! Hero
Joomla! Hero
Posts: 2954
Joined: Sun May 04, 2008 12:37 pm

Re: Making Tags required for Joomla Articles in specific Categories

Post by waarnemer » Tue Nov 20, 2018 9:52 pm

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.

SharkyKZ
Joomla! Hero
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

Post by SharkyKZ » Wed Nov 21, 2018 6:44 am

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.

waarnemer
Joomla! Hero
Joomla! Hero
Posts: 2954
Joined: Sun May 04, 2008 12:37 pm

Re: Making Tags required for Joomla Articles in specific Categories

Post by waarnemer » Wed Nov 21, 2018 11:34 am

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.

SharkyKZ
Joomla! Hero
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

Post by SharkyKZ » Wed Nov 21, 2018 12:50 pm

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.

waarnemer
Joomla! Hero
Joomla! Hero
Posts: 2954
Joined: Sun May 04, 2008 12:37 pm

Re: Making Tags required for Joomla Articles in specific Categories

Post by waarnemer » Wed Nov 21, 2018 1:21 pm

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...


Locked

Return to “General Questions/New to Joomla! 3.x”