Refactor button rendering into a service for flexible reuse

Created on 27 October 2025, 8 days ago

Problem/Motivation

Currently, the share button is rendered via hook_ENTITY_view(), which limits its usage to entity view contexts only. This makes it difficult to reuse the button in other places like:
- Controllers and custom routes
- Custom blocks
- Headless/decoupled outputs
Developers have no clean way to render the button outside of entity templates.

Proposed resolution

Introduce a dedicated service, e.g. mobile_native_share.renderer, with a method like
public function render(?EntityInterface $entity = NULL): array;

This service would encapsulate the logic for generating the render array of the share button, using metadata from the passed entity (if any). It would:
- Extract url, title, and description from the entity or fallback to current request.
- Return a renderable array with the appropriate attributes and attached library.
- Be easily reusable in blocks, controllers, or Twig extensions.
The existing hook_ENTITY_view() implementation can then delegate to this service.

Benefits

- Decouples rendering logic from entity view hooks.
- Enables reuse in any context (blocks, templates, custom routes).
- Makes the module more developer-friendly and testable.
- Opens the door for future enhancements (e.g. Twig function, JSON output for headless use).

Feature request
Status

Active

Version

1.0

Component

Code

Created by

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

No activities found.

Production build 0.71.5 2024