Skip to content

Architecture

Domain Short URL extends the base Short URL module through service decoration. Each decorated service wraps the original and adds domain-aware behavior while delegating non-domain operations to the inner service.

Decorated services

DomainAwareShortUrlManager

Decorates ShortUrlManager. Overrides:

  • buildFullUrl() — uses the assigned domain's hostname and path prefix instead of the current request's host
  • syncRedirects() — sets domain_id on all created redirect entities to scope them to the correct domain
  • resolveNodeBySlug() — finds the short URL the active domain's redirect serves: the domain's own, else one with no domain, never another domain's
  • getUrlOptions() — injects the domain entity into the options array for outbound path processing

DomainAwareSlugGenerator

Decorates SlugGenerator. Adds:

  • generateAutoIncrementForDomain(string $domain_id) — generates a domain-scoped sequential slug using a per-domain State API counter when counter_scope is per_domain
  • isCounterPerDomain() — tells whether counter_scope is per_domain, so the presave hook leaves the base module's global number alone otherwise

Base36 generation delegates directly to the inner generator since random slugs don't need domain scoping.

CounterScopeSubscriber deletes the per-domain counters from State whenever counter_scope changes, so each one is taken again from the highest slug on its domain.

DomainCsvColumn

Adds the domain column to the base CSV import, as a service tagged shorturl.csv_column. It refuses a domain that does not exist or that the current user may not choose, scopes slug uniqueness by domain, finds the short URL a row updates on the row's own domain (a row without a domain only matches a short URL without one), and sets the domain on the node.

DomainChoiceChecker holds the rules for which domains a user may choose. The node form's domain selector uses it too, and so does the ShortUrlDomainChoice constraint on the domain field, which REST and JSON:API apply.

DomainAwareVisitTracker

A service provider (DomainShorturlServiceProvider) swaps the class of the base VisitTracker service for this subclass, and registers domain_id as a column the base module's visit subscriber writes. buildVisitFields() adds the redirect's domain_id to visits recorded through VisitTracker::trackVisit().

Data flow

Visit tracking

  1. A redirect response is generated by the Domain Redirect module.
  2. DomainShortUrlEntityHooks::redirectResponseAlter() stamps the X-Shorturl-Domain-Id header from the redirect's domain_id field, on short URL redirects only.
  3. The base module's ShortUrlVisitMiddleware collects every X-Shorturl-* header of the response, so the visit row gets domain_id, including for redirects served from the page cache.
  4. The middleware strips the headers before the response reaches the client.

Slug generation (auto-increment)

  1. User creates a short URL node with a domain selected.
  2. ShortUrlEntityHooks::nodePresave() runs first and generates a global slug.
  3. DomainShortUrlEntityHooks::nodePresave() runs after (via OrderAfter attribute), detects the domain, and, when counters are per domain, replaces the slug with the domain's next number that no other redirect on the domain uses. It does the same when an existing short URL switches to auto-increment.
  4. The node title is set to {slug} @ {domain_label}.

Hook implementations

Class Hooks
DomainShortUrlEntityHooks node_presave, redirect_response_alter
DomainShortUrlFormHooks form_node_shorturl_form_alter, form_node_shorturl_edit_form_alter
DomainShortUrlViewsHooks views_data_alter

Database additions

The shorturl_visits table gains a domain_id column (VARCHAR 255, indexed) added during module installation and removed on uninstall.