In past versions of Drupal, XBBCode usually handled tags similarly to how core handled filters: in D7, hook_xbbcode_info() returns an associative array keyed by tag ID, each with either a markup template or a render callback. In Drupal 5, hook_xbbcode() received an $op argument.
In D8, of course, filters are plugins, while XBBCode tags are still fully procedure-based. Besides not really fitting with the D8 object-oriented approach, there are numerous shortcomings of this design: The procedural code has no inheritance; tag names are hard-coded; tags cannot expose settings.
If tags were plugins, the renderer could be decoupled from the tag as it appears in the input format ([name=...][/name]). Tags could be renamed or duplicated.
The one difficulty is that custom tags would be a bit harder to implement - they'd be plugins too, but they'd be loaded and initialized separately.
The association from tag name to plugin would be handled centrally by XBBCode - there can be any number of plugins associated with a given tag name, and while configuring a text format you could simply choose one of them.
Comments
Comment #1
cburschkaLiterally turning *every* tag into a full-featured plugin class would be a large overhead, particularly for tags that only consist of a simple template.
Ideally, modules could create a tag plugin the same way a filter plugin is created, but they could also simply define a tag processor in a config file of a form like the following:
Which would be equivalent in all practical respects to creating two annotated plugin classes that implement the appropriate ::getTemplate() or ::render() functions, with the added benefit that if we switch to Twig, a statically defined template can be cached and precompiled.
If processors are stored in config, then they can also be created by a web interface, which neatly allows custom tags. ... maybe processors are entities? Something like single-filter text formats?
In that case, maybe defining tags in config or extending the plugin base class shouldn't be interchangeable. Maybe a tag processor should EITHER contain a template OR refer to a plugin class. Like
The association between tag name and processor (an N:N relationship) is global and configurable. A module can define a default association:
(Modules should be able to provide multiple processors for the same tag, and multiple tag names for the same processor, which is why this is an unkeyed sequence.)
Comment #2
eric_a commentedI don't think it gets more lightweight then using just plugins. The way I see it is that xbbcode would define exactly one plugin type and one deriver (for the web interface created ones). The tags would just be very simple classes: straight conversion from the info hooks, just like hook_element_info() got converted.
Comment #3
cburschkaOoh, thanks for the tip! I haven't really dealt with the Plugin API yet and didn't know how to dynamically provide new plugins.
Comment #9
cburschkaDone.
Comment #10
cburschka(In 8.x-3.x)