Tags, collections and sections (v2)
17 April 2022 515 views | Documentation v2
Organising content
Related content can be grouped using tags and collections, both of which are regular content objects (\Tfish\Content\Entity\Content) in their own right, with their type property set to TfTag and TfCollection, respectively. Refer to the User Manual for their general usage. Add tags and collections to the system through the regular content submission form, setting the appropriate type.
Tags are essentially "subject labels" that you can apply to other content objects; you can label a content object with as many tags as you want. Associations between tags and content are recorded in the taglink table of the database. Every time a content object is inserted, updated or deleted its associated taglinks are updated automatically.
Collections basically function as categories, but like tags they are fully-fledged content objects in their own right. You can assign any content object to any existing collection using the 'Parent (collection)' field in the content data entry form. Any type of content can be assigned to a collection, so mixed sets of content are possible. Collections can be nested by assigning one as the parent of another, thereby creating subcategories.
Filtering content by tag
Call the setTag() method on the \Tfish\Criteria object to apply a tag filter to a database query.
...
$criteria = $this->criteriaFactory->criteria();
$criteria->setTag(42);
...
Adding tag functionality to a new class
Support for tagging can be added to new MVVM classes with Traits:
- Content objects (entities) should use \Tfish\Traits\Tag. This adds a 'tags' property and getter/setter methods tags() and setTags().
- Models handling content objects for display should use \Tfish\Traits\TagRead. This adds methods for retrieving an object's tags getTagsForObject(int $id), and several methods for retrieving tags to build select boxes, including tags in use by a particular module activeTagOptions(string $module), tags that belong to a specific collection collectionTagOptions(int $id), and only tags that have been marked online onlineTagSelectOptions().
- Models handling content object entry and editing (admin models) should use \Tfish\Traits\Taglink, which adds methods for creating, reading, updating and deleting taglinks associated with content objects.
Filtering content by collection
To retrieve a collection of content objects, set the parent (collection) ID as a filter criteria:
...
$criteria = $this->criteriaFactory->criteria();
$criteria->add($this->criteriaFactory->item('parent', $id));
...
Creating sections
Both tags and collections can be used to quickly create sections for a website. By 'section' I mean a logical division of content, such as by subject, that you might include as menu or navigation element.
Both tags and collections display a list of member / child content below their own description, in teaser format. If you wanted to create a "Publications" section on your website consisting of (say) PDF downloads, you could:
- Create a 'collection' type content object called "Publications".
- Edit the relevant download objects, setting "Publications" as their 'parent' (collection).
- Add a link to the ID of the "Publications" collection in your navigation menu (remember it's as simple as https://yoursite.com?id=42).
- If you would prefer a user-friendly URL such as 'yoursite.com/publications', you can add an entry to the routing table.
Your new section will automatically update itself as you add new content to the Publications collection. Tags work the same way. Static pages with custom URLs also also make good sections, However, you can use any content object.
Copyright, all rights reserved.