Description
Summary:
- Find a Drupal pattern data-mapping solution that is a good fit for our specific implementation and tooling
Benefits:
- We provide a solid solution to the problem of data mapping in Drupal 8 with themes using pattern libraries for our users to get started with
To Do:
- Discuss solutions with community
- Select a "best possible" solution for our particular implementation
- Create required issues to implement the solution
One of the hard problems with making Drupal play nice with a pattern library is mapping your backend Drupal entities and fields to the pattern variables. Some patterns expect strings, or arrays, or can even accept complicated objects for rendering that make use of recursive rendering from the parent platform.
This would be relatively simple to solve if we just use schemas for our patterns and imported those at run-time into Drupal. Unfortunately for that approach, many Drupal sites have a complicated set of backend data, entity, and field organization that makes it impossible to map fields 1:1 to a frontend. This also makes it hard to dynamically generate data models in Drupal for all scenarios, since often we’ll want to include values contained in other fields/entities.
1:1 mapping is easier though than tackling data mapping, and there are a couple of solutions that implement it pretty well.
A common solution for data mapping in Drupal 8 is to use “presenter” twig templates that include a pattern from the library and do all the complicated data mapping in the Drupal “presenter” template. This essentially looks like:
Drupal controller, view node response render -> node.tpl.php (presenter data-mapping template) -> twig include patterns/myCustomPattern.twig
One possible solution is to use a Pattern Renderer API service, that allows a configuration tool to display and set replacement tokens for Drupal to manage. The issue with this is that it relies on developers to create those tokens for all of the backend data.
Another possible solution is to have a Drupal module that has a configurable set of data mapping transformations, much like Views, that allows assigning fields to the pattern schema variables.
Here are some of the current open source community attempts at solving this problem, in order of scope from smallest to largest:
- https://www.drupal.org/project/twig_field_value Simply makes it easier to get the raw field values from fields. It doesn't hook into Drupal's preprocessing systems or renderable output. This is in use on some production Drupal 8 sites.
- https://www.drupal.org/sandbox/cybtachyon/2744911 Recreates Drupal 8 Core's rendering subsystem and returns all preprocess variables In use on multiple enterprise Drupal 8 sites.
- https://www.drupal.org/node/2848222 (and https://www.drupal.org/node/2707849 ) An attempt at adding a mapping GUI to the Components module, possibly using a token or node-based editor. This is one of the more flexible approaches, but still requires every single pattern be mapped by a user at least once.
- https://www.drupal.org/project/ui_patterns Full-scope module that imports pattern schemas to create paragraphs and other fielded entities in Drupal. Requires a 1:1 data mapping relationship with Drupal entities and fields.
- https://github.com/PatternBuilder/pattern-builder-drupal Dependent on PatternKit, this full-scope module imports and caches PatternKit patterns and their schemas, and allows them to be integrated as drag-and-drop panels in Drupal 7. Drupal Tokens are used for variable replacement. In use on some Red Hat Drupal 7 projects.
The teams I work with internally already have a number of projects in this vein and are planning on moving forwards with a consolidated project starting August 21st.