If you want to have a tagging facility for items in a component then it is a good idea to use the inbuilt Joomla tagging system. This does quite a lot of the heavy lifting for you (once you have set things up) and has the big advantage that tags can be shared across components. So if you wish to, you can have tags that can be applied both to articles and to items in your component, which saves having to maintain duplicate lists of tags.

You may come across some limitations in the functionality, especially when it comes to filtering a list of items by tag - but that will be a separate post.

This article is to remind me of the various hoops and enhancements needed to get tagging working in xbBooks 

 

(NB this relates to J3.* - I think things may change somewhat in Joomlla 4)

Because the Tags component uses the UCM tables to track what is tagged across components it is firstly necessary to register your component type(s) in the #__content_types table.

So in the SQL install file you need something like this:

INSERT INTO `#__content_types` (`type_title`, `type_alias`, `content_history_options`, `table`, `field_mappings`, `router`,`rules`) 
VALUES
('Xbbooks Book', 'com_xbbooks.book', 
'{"formFile":"administrator\\/components\\/com_xbbooks\\/models\\/forms\\/book.xml", 
    "hideFields":["checked_out","checked_out_time"], 
    "ignoreChanges":["checked_out", "checked_out_time"],
    "convertToInt":[], 
    "displayLookup":[
        {"sourceColumn":"catid","targetTable":"#__categories","targetColumn":"id","displayColumn":"title"}
    ]
 }',
'{"special":{"dbtable":"#__xbbooks","key":"id","type":"Book","prefix":"XbbooksTable","config":"array()"},
    "common":{"dbtable":"#__ucm_content","key":"ucm_id","type":"Corecontent","prefix":"JTable","config":"array()"}}',
'{"common": {
    "core_content_item_id": "id",
    "core_title": "title",
    "core_state": "state",
    "core_alias": "alias",
    "core_created_time": "created",
    "core_body": "synopsis",
    "core_catid": "catid"
  }}',
'XbbooksHelperRoute::getBookRoute',''),

 Having done that you then need to add a tag field to the edit form for the type of item you will be editing:

<field 
    name="tags" 
    type="tag"
    label="JTAG" 
    description="JTAG_DESC"
    multiple="true"
 >

Of course set multiple="false" if you only want one tag (hich would seem to undermine the point of tags)

Then just add the Tags Observer to the Table class in its constructor:

public function __construct(&$db) {
    $this->setColumnAlias('published', 'state');
    parent::__construct('#__xbbooks', 'id', $db);
    JTableObserverTags::createObserver($this, array('typeAlias' => 'com_xbbooks.book'));
}

finally you might think you need to explicitly show the Tag selector field in the edit page, but if you are using the standard facilities like this they will magically appear with the status, category and note fields.

echo JLayoutHelper::render('joomla.edit.global', $this); 

That gets tagging working for an item in your component. The next problem is to look at the ways it can be used in searching and filtering.

 

Tags:
Coding:
Joomla:
Php:

Comments powered by CComment