Page 1 of 1

Tagging contacts on registration

Posted: Mon Jul 15, 2019 3:59 pm
by PTuson
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?

Re: Tagging contacts on registration

Posted: Sat Jul 20, 2019 1:53 pm
by PTuson
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.

Re: Tagging contacts on registration

Posted: Wed Jun 17, 2020 10:50 am
by cradenborg
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.

Re: Tagging contacts on registration

Posted: Wed Jun 17, 2020 12:53 pm
by PTuson
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.