Skip to content

Generic tagging for any module (tags + tag_links) #529

Open

Description

@caseysoftware

Current state

  • Core has no tagging. Nothing in the schema or classes lets a user label projects, tasks, files or other records with free-form tags.
  • Modules that want tags (the Cards module first) would each have to build their own tables, autocomplete, filtering and admin. Tags couldn't be shared across modules.

Intended solution

Schema

  • tags
    • tag_id
    • tag_name varchar(64)
    • tag_slug varchar(64), unique: lower-cased, trimmed, inner whitespace collapsed
    • tag_color char(7), null
    • tag_created, tag_creator
  • tag_links
    • tag_id
    • link_module varchar(40)
    • link_item int
    • link_created, link_creator
    • Primary key (tag_id, link_module, link_item); index (link_module, link_item)

API

A CTag object plus a small w2p_Core_Tags service:

  • forItems($module, array $ids): tags for many items in one query (no N+1 on list views and boards).
  • setTags($module, $id, array $names): diffs against current tags, creates missing tags, and returns ['added' => [...], 'removed' => [...]] so callers can log changes.
  • itemsTagged($module, array $tagIds, $match = 'any'|'all'): for filtering lists.
  • suggest($prefix, $limit = 10): autocomplete.
  • Admin: rename(), merge($fromId, $intoId), delete() (removes its links), usage counts per module.

UI

  • A shared helper renders tag chips and a tag input (autocomplete from suggest()), so every module looks the same.
  • System Admin → Tags page: rename, merge, delete, usage counts.
  • hook_search support, so Smart Search finds items by tag.

Rules

  1. One vocabulary for all modules. "Q4 launch" is the same tag on a card, a project or a file. Tags aren't scoped per module or per project.
  2. Uniqueness by slug. "Q4 Launch", "q4 launch" and "Q4 launch" are one tag. The first spelling used is the display name until an admin renames it.
  3. Who can tag: anyone who can edit an item can add or remove its tags, including creating a new tag by typing it. Renaming, merging and deleting tags is admin-only (System Admin rights).
  4. Visibility follows the item. Seeing that an item has a tag requires view rights on the item. Tag names themselves aren't secret: the autocomplete shows all names.
  5. Renames apply everywhere. Modules that log tag changes (e.g. an activity feed) should record the tag name at the time of the change, not just the ID.
  6. Cleanup on delete. When any tagged object is deleted, its tag_links rows are removed. This uses the shared dispatcher from Let modules subscribe to other modules' object events (global event dispatcher) #526 (a core listener on postDeleteEvent for any resource). A hook_cron sweep removes links whose item no longer exists, as a backstop.
  7. Unused tags are kept. A tag with no links stays in the vocabulary until an admin deletes it, so autocomplete doesn't lose it.
  8. Limits: at most 64 characters per tag, and a sensible cap per item (e.g. 20) enforced in setTags().

Acceptance

  • Two modules tag their own items with the same tag and itemsTagged() finds each.
  • Loading tags for 200 items takes one query.
  • Deleting an item removes its links.
  • Merge moves every link to the surviving tag without duplicates.

Context

First consumer: the Cards module (caseysoftware/modules-cards), whose board groups and filters cards by tag.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions