Skip to content

Refactor SBF field formatters so we have Theme hints and preprocessors #14

@DiegoPino

Description

@DiegoPino

Use case

This is something we will have to do at certain point, and the sooner we do it the easier it will be to theme/extend and improve our Media and Metadata Formatters. A lot of the heavy work is done now directly on the formatter itself, and we could benefit of moving some of that logic into Themes/Templates and Preprocessors. That way other modules can simply override our decisions and make, with the same logic/data, better looking and more powerful ways of displaying Digital Object assets.

How?

Add custom themes in each ::viewElements method for each formatter

public function viewElements(FieldItemListInterface $items) {
    $elements = parent::viewElements($items);
    foreach ($elements as &$element) {
      $element['#theme'] = 'formatter_strawberryfield_audio_formatter';
    }

    return $elements;
  }

Then make sure we implement hook_theme() and template_preprocess_HOOK() and then add a twig template to do the actual rendering.

The code right now is done, so this is an improvement of course, we are already generating a render array() but would be lovely to move some of that out of arrays into twig templates.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions