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

cburschka’s picture

Literally 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:

xbbcode_processor.{module}_b:
  template: '<strong>{content}</strong>'
xbbcode_processor.{module}_img:
  selfclosing: 1
  template: '<img src="{option}" />'

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

xbbcode_processor.{module}_b:
  label: ''
  sample:
    content: 'Bold text'
  template: '<strong>{content}</strong>'
xbbcode_processor.{module}_img:
  selfclosing: 1
  plugin: '\Drupal\{module}\Plugin\XBBCode\MyXBBCodePlugin'

The association between tag name and processor (an N:N relationship) is global and configurable. A module can define a default association:

xbbcode_tags.{module}:
  -
    name: 'b'
    processor: {module}_b
  -
    name: 'b'
    processor: {module}_b2

(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.)

eric_a’s picture

I 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.

cburschka’s picture

Ooh, thanks for the tip! I haven't really dealt with the Plugin API yet and didn't know how to dynamically provide new plugins.

  • cburschka committed 1082b53 on 8.x-3.x
    Issue #2472767: Convert tags into plugins
    
    This is the first pass of a...

  • cburschka committed c77cbf0 on 8.x-3.x
    Issue #2472767, #2474505.
    
    Starting work on implementing custom tags as...

  • cburschka committed f111324 on 8.x-3.x
    Issue #2472767, #2474505. add/edit/delete actions.
    
    The XBBCodeCustomTag...

  • cburschka committed b1b2718 on 8.x-3.x
    Issue #2472767, #2474505. Implement a Deriver.
    
    The deriver now creates...

  • cburschka committed 3252807 on 8.x-3.x
    Issue #2472767, #2474505. Fix the deriver.
    
    This will pass the entity...
cburschka’s picture

Status: Active » Fixed

Done.

cburschka’s picture

Version: 8.x-2.x-dev » 8.x-3.x-dev

(In 8.x-3.x)

  • cburschka committed 1082b53 on 9.x-1.x
    Issue #2472767: Convert tags into plugins
    
    This is the first pass of a...
  • cburschka committed 3252807 on 9.x-1.x
    Issue #2472767, #2474505. Fix the deriver.
    
    This will pass the entity...
  • cburschka committed b1b2718 on 9.x-1.x
    Issue #2472767, #2474505. Implement a Deriver.
    
    The deriver now creates...
  • cburschka committed c77cbf0 on 9.x-1.x
    Issue #2472767, #2474505.
    
    Starting work on implementing custom tags as...
  • cburschka committed f111324 on 9.x-1.x
    Issue #2472767, #2474505. add/edit/delete actions.
    
    The XBBCodeCustomTag...

  • cburschka committed 1082b53 on 9.x-2.x
    Issue #2472767: Convert tags into plugins
    
    This is the first pass of a...
  • cburschka committed 3252807 on 9.x-2.x
    Issue #2472767, #2474505. Fix the deriver.
    
    This will pass the entity...
  • cburschka committed b1b2718 on 9.x-2.x
    Issue #2472767, #2474505. Implement a Deriver.
    
    The deriver now creates...
  • cburschka committed c77cbf0 on 9.x-2.x
    Issue #2472767, #2474505.
    
    Starting work on implementing custom tags as...
  • cburschka committed f111324 on 9.x-2.x
    Issue #2472767, #2474505. add/edit/delete actions.
    
    The XBBCodeCustomTag...

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.