Tagging contacts on registration Topic is solved

This forum is for general questions about extensions for Joomla! 3.x.

Moderators: pe7er, General Support Moderators

Forum rules
Forum Rules
Absolute Beginner's Guide to Joomla! <-- please read before posting, this means YOU.
Forum Post Assistant - If you are serious about wanting help, you will use this tool to help you post.
Windows Defender SmartScreen Issues <-- please read this if using Windows 10.
Post Reply
PTuson
Joomla! Fledgling
Joomla! Fledgling
Posts: 3
Joined: Mon Jul 15, 2019 3:31 pm

Tagging contacts on registration

Post by PTuson » Mon Jul 15, 2019 3:59 pm

When users register with our site they are automatically created as contacts. I have been unsuccessfully attempting to assign tags to the contact on registration using a php script.

I have narrowed down the problem to the contentitem_tag_map table, which is not being completed. The other tables involved are being completed properly. My code for adding the tag is:

$tags = JHelperTags::searchTags(array('title' => 'XXXXXXXX'));
$tag_id = $tags[0]->value;
...
$tagsObserver = $contact->getObserverOfClass('JTableObserverTags');
$tagsObserver->setNewTags(array($tag_id), false);

Then check and store as usual. Other than the tags, the correct information is being written to the contact table.

I have monitored what is going on in the code. The most relevant section appears to be the addTagMapping method within TagsHelper. A subset of the query is:

[insert:protected] => JDatabaseQueryElement Object
(
[name:protected] => INSERT INTO
[elements:protected] => Array
(
[0] => #__contentitem_tag_map
)

[glue:protected] => ,
)

[from:protected] =>
[join:protected] =>
[set:protected] =>
[where:protected] =>
[group:protected] =>
[having:protected] =>
[columns:protected] => JDatabaseQueryElement Object
(
[name:protected] => ()
[elements:protected] => Array
(
[0] => `type_alias`
[1] => `core_content_id`
[2] => `content_item_id`
[3] => `tag_id`
[4] => `tag_date`
[5] => `type_id`
)

[glue:protected] => ,
)

[values:protected] => JDatabaseQueryElement Object
(
[name:protected] => ()
[elements:protected] => Array
(
[0] => 'com_contact.contact', 4569, 1374, '1090', CURRENT_TIMESTAMP(), 3
)

[glue:protected] => ),(
)

There are no errors being reported on the execution of this. I tried manually inserting this directly within the database using PhpMyAdmin and it worked correctly. However, when I run my code nothing is entered within the contentitem_tag_map table.

I am running this on my test computer running Debian Linux. I am using MariaDB V10.3, PHP 7.3, Apache 2.4, PhpMyAdmin 4.6.6 and Joomla 3.9.10. I have tried giving the registering user full access rites to everything. I have used this tagging approach successfully before with articles and categories but this is the first time I have used with contacts.

Does anyone have any ideas about what I am doing wrong and how I might correct this?
Last edited by imanickam on Tue Jul 16, 2019 2:18 am, edited 1 time in total.
Reason: Moved the topic from the forum Extensions for Joomla! 3.x to the forum Joomla! 3.x Coding

 
PTuson
Joomla! Fledgling
Joomla! Fledgling
Posts: 3
Joined: Mon Jul 15, 2019 3:31 pm

Re: Tagging contacts on registration

Post by PTuson » Sat Jul 20, 2019 1:53 pm

I have managed to resolve this.

I appears the check and store functions remove any tags that mat have been added by setNewTags.

The solution was to add the tags using setNewTags after the check and store functions for the main Contact object.

cradenborg
Joomla! Apprentice
Joomla! Apprentice
Posts: 5
Joined: Wed Oct 10, 2018 8:28 pm

Re: Tagging contacts on registration

Post by cradenborg » Wed Jun 17, 2020 10:50 am

Hi, since I'm trying to do the same, could you please help me in the code to do this? I copied the contactcreator plugin and added some extra fields, which all works fine. Now I want to add a specific tag to all new contacts, so I wanted to write a SQL insert statement, but I got stuck on the core content id, which doesn't exist yet.

Since I'm no developer, but merely a try-and-learn guy, I can't get any further.

Hope you can help a bit...

UPDATE: Fixed it ;-)

I just added

Code: Select all

	$tagsObserver = $contact->getObserverOfClass('JTableObserverTags');
	$tagsObserver->setNewTags(array("4"), false);


right before the store function in the contactcreator.php file of the plugin :

Code: Select all

			
if ($contact->check() && $contact->store())
{
	   //add tag to contact so that it will be shown in contact list and  is available for editing in frontend
	$tagsObserver = $contact->getObserverOfClass('JTableObserverTags');
	$tagsObserver->setNewTags(array("4"), false);
	
	return true;
}
"4" is the id of the tag I wanted to add.

PTuson
Joomla! Fledgling
Joomla! Fledgling
Posts: 3
Joined: Mon Jul 15, 2019 3:31 pm

Re: Tagging contacts on registration

Post by PTuson » Wed Jun 17, 2020 12:53 pm

Hi, This is my code for the user

if ($contact->check() && $contact->store()) {
if ($tag_id) {
$tagsObserver = $contact->getObserverOfClass('JTableObserverTags');
$tagsObserver->setNewTags(array($tag_id), false);
}
return true;
}


and this is a section out of my code for contacts

// Store results
if ($newtags) {
$tagsObserver = $contact->getObserverOfClass('JTableObserverTags');
$tagsObserver->onBeforeStore(true, false);
$tagsObserver->setNewTags($newtags, true);
}
return $contact->store();

Hope this helps.

Regards,

Peter.

 

Post Reply

Return to “Extensions for Joomla! 3.x”