Skip to main content
added 6 characters in body
Source Link
RogerCO
  • 183
  • 7

Here's a simple solution that uses the batch() command in AdminModel and works ok even if the item doesn't yet have ucm core_content_id

 /** * @name addTagToItem() * @desc adds an existing tag to an item * @param string $compitem - the component item type in dotted lower case eg com_content.article * @param int $itemId - the id of the ite the tag is being added to * @param int $tagId - the id of the tag being added * @return boolean - true on success, false on failure */ public function addTagToItem($compitem, $itemId, $tagId) { $arr=explode('.',$compitem); $app = Factory::getApplication(); $factory = $app->bootComponent($arr[0])->getMVCFactory(); $model = $factory->createModel(ucfirst($arr[1]), 'Administrator'); $commands = array('tag'=>$tagId); $pks = [$itemId]; $contexts = [$itemId=>$compitem.'.'.$itemId]; $res = $model->batch($commands, $pks, $contexts ); return $res; } 

Seems to work ok for me. I think tags are about the only thing left using the ucm system. Hopefully it'll get removed soon.

You have to use your own custom getCreateTag() function to get the tagId if you don't have it or if it is a new tag. Easier than farting around with #new# prepended to the tag title. Or use the very simple function in @Zollie answer above.

You could easily adapt to handle multiple tags and/or items by passing them in as an array and iterating through them building the $contexts array.

Even easier to adapt to multiple items to have the same tag applied to each - just pass an array of item ids and pass that direct to the batch() function.

Same code could be used to remove tags if your component has implemented an batch.untag command (which IMHO should be part of core Joomla anyway)

Here's a simple solution that uses the batch() command in AdminModel and works ok even if the item doesn't yet have ucm core_content_id

 /** * @name addTagToItem() * @desc adds an existing tag to an item * @param string $compitem - the component item type in dotted lower case eg com_content.article * @param int $itemId - the id of the ite the tag is being added to * @param int $tagId - the id of the tag being added * @return boolean - true on success, false on failure */ public function addTagToItem($compitem, $itemId, $tagId) { $arr=explode('.',$compitem); $app = Factory::getApplication(); $factory = $app->bootComponent($arr[0])->getMVCFactory(); $model = $factory->createModel(ucfirst($arr[1]), 'Administrator'); $commands = array('tag'=>$tagId); $pks = [$itemId]; $contexts = [$itemId=>$compitem.'.'.$itemId]; $res = $model->batch($commands, $pks, $contexts ); return $res; } 

Seems to work ok for me. I think tags are about the only thing left using the ucm system. Hopefully it'll get removed soon.

You have to use your own custom getCreateTag() function to get the tagId if you don't have it or if it is a new tag. Easier than farting around with #new# prepended to the tag title. Or use the very simple function in @Zollie answer above.

You could easily adapt to handle multiple tags by passing them in as an array and iterating through them building the $contexts array.

Even easier to adapt to multiple items to have the same tag applied to each - just pass an array of item ids and pass that direct to the batch() function.

Same code could be used to remove tags if your component has implemented an batch.untag command (which IMHO should be part of core Joomla anyway)

Here's a simple solution that uses the batch() command in AdminModel and works ok even if the item doesn't yet have ucm core_content_id

 /** * @name addTagToItem() * @desc adds an existing tag to an item * @param string $compitem - the component item type in dotted lower case eg com_content.article * @param int $itemId - the id of the ite the tag is being added to * @param int $tagId - the id of the tag being added * @return boolean - true on success, false on failure */ public function addTagToItem($compitem, $itemId, $tagId) { $arr=explode('.',$compitem); $app = Factory::getApplication(); $factory = $app->bootComponent($arr[0])->getMVCFactory(); $model = $factory->createModel(ucfirst($arr[1]), 'Administrator'); $commands = array('tag'=>$tagId); $pks = [$itemId]; $contexts = [$itemId=>$compitem.'.'.$itemId]; $res = $model->batch($commands, $pks, $contexts ); return $res; } 

Seems to work ok for me. I think tags are about the only thing left using the ucm system. Hopefully it'll get removed soon.

You have to use your own custom getCreateTag() function to get the tagId if you don't have it or if it is a new tag. Easier than farting around with #new# prepended to the tag title. Or use the very simple function in @Zollie answer above.

You could adapt to handle multiple tags and/or items by passing them in as an array and iterating through them building the $contexts array.

Same code could be used to remove tags if your component has implemented an batch.untag command (which IMHO should be part of core Joomla anyway)

Source Link
RogerCO
  • 183
  • 7

Here's a simple solution that uses the batch() command in AdminModel and works ok even if the item doesn't yet have ucm core_content_id

 /** * @name addTagToItem() * @desc adds an existing tag to an item * @param string $compitem - the component item type in dotted lower case eg com_content.article * @param int $itemId - the id of the ite the tag is being added to * @param int $tagId - the id of the tag being added * @return boolean - true on success, false on failure */ public function addTagToItem($compitem, $itemId, $tagId) { $arr=explode('.',$compitem); $app = Factory::getApplication(); $factory = $app->bootComponent($arr[0])->getMVCFactory(); $model = $factory->createModel(ucfirst($arr[1]), 'Administrator'); $commands = array('tag'=>$tagId); $pks = [$itemId]; $contexts = [$itemId=>$compitem.'.'.$itemId]; $res = $model->batch($commands, $pks, $contexts ); return $res; } 

Seems to work ok for me. I think tags are about the only thing left using the ucm system. Hopefully it'll get removed soon.

You have to use your own custom getCreateTag() function to get the tagId if you don't have it or if it is a new tag. Easier than farting around with #new# prepended to the tag title. Or use the very simple function in @Zollie answer above.

You could easily adapt to handle multiple tags by passing them in as an array and iterating through them building the $contexts array.

Even easier to adapt to multiple items to have the same tag applied to each - just pass an array of item ids and pass that direct to the batch() function.

Same code could be used to remove tags if your component has implemented an batch.untag command (which IMHO should be part of core Joomla anyway)