Problem/Motivation
I am currently adding Facebook Instant Articles to one of our project and stumbled upon the FieldItemListNormalizer::normalize() fallback that uses the FBIA SDK's Transformer class to transform rendered markup into the appropriate FBIA elements. This is especially useful when dealing with media entities (e.g. images, where you cannot use the "FBIA Image" formatter directly due to the referenced media entity)
Unfortunately all transformed FBIA elements are added to the content element of the article, but you may have use cases where you want to add things to the region, where the field is added to (e.g. place the header image media entity into the article header element and not the content element).
Is there a special reason, why these transformed FBIA elements are added to the article's content element during FieldItemListNormalizer::normalize()?
Proposed resolution
Extend the FieldItemListNormalizer::normalize() transformation fallback to use the appropriate article header/footer element as transformation context (instead of the article context everywhere).
Example for fields in the header region of the fb_instant_articles view mode:
$context = $article;
if ($component['region'] === Regions::REGION_HEADER) {
$header = $article->getHeader();
if (!$header) {
$header = Header::create();
$article->withHeader($header);
}
$context = $header;
}
$this->transformer->transform($context, $document);
This would e.g. allow to add a custom transformer rule using the HeaderImageRule to inject a referenced image media entity into the article header, where it belongs. With the current approach, such a rule would not create any markup, because the HeaderImageRule is used in the article content context and is therefore ignored.
Remaining tasks
- Discuss whether such an approach would be the right way to go
- Create a pull request for this functionality
User interface changes
n/a
API changes
- Users that rely on added header fields with transformed markup being added to the article's content, will have to move their fields to the appropriate content region in the
fb_instant_articlesview mode instead
Data model changes
n/a
| Comment | File | Size | Author |
|---|---|---|---|
| #6 | interdiff-2915661-4-6.txt | 1.29 KB | hctom |
| #6 | improve-2915661-6.patch | 2.66 KB | hctom |
| #4 | improve-2915661-4.patch | 2.61 KB | hctom |
Comments
Comment #2
hctom... another idea to solve this for referenced entites would be to create a "FBIA Rendered entity" formatter (extending
Drupal\Core\Field\Plugin\Field\FieldFormatter\EntityReferenceEntityFormatterclass) that renders and transforms the referenced entities with the above stated additions for correct context handling like so:Of course there should be another formatter for entities referenced via
entity_reference_revisionsmodule (e.g. for paragraphs).Comment #3
hctomComment #4
hctomThinking more about this, I would prefer to extend the transformation fallback in
FieldItemListNormalizer::normalize(), because this is more generic and sou everybody can use it in combination with added transformer rules - regardless of what field type or formatter is in use.Attached is a patch for this (if anybody needs to reference this e.g. in their composer.json file easily) and you can find a corresponding PR at:
https://github.com/BurdaMagazinOrg/module-fb_instant_articles/pull/121
Comment #5
m4oliveiThis is looking great. Thanks for doing this! I left a minor comment on your PR, plus, it would be nice to have test coverage for this change.
Comment #6
hctomAttached is the new updated patch without the
$contextvariable naming conflict. The PR is updated as well.Comment #8
m4oliveiThanks so much! Reviewed and merged to 8.x-2.x.