Currently the rdf module only works for content that is rendered via entity_view(), which invokes hook_entity_prepare_view(). This means that we can't render RDF markup in field rendered by views for example.
I'm looking to generalize the logic we have in rdf_entity_prepare_view() to make it work for any field regardless of the context it's rendered in, whether it's rendered by entity_view() or not. In Drupal 8, field formatters need to have certain attributes passed in. That's the reason why rdf.module implements hook_entity_prepare_view(), and prepares some attributes which are added to each item's _attributes property before the field formatters viewElements() method is called. Then field formatters can decide to render those attributes as part of their output (an example is ImageFormatter), or let them in $item->_attribute, in which case template_preprocess_field() will add them to the field template output (that's the case for most text field formatters). We can't rely on hook_preprocess_field() to achieve what I'm trying to do because at this point, the field formatters have already been called and it's too late for the rdf module to do anything useful at the field formatter output level.
Is there any hook or any way to invoke code from a module before the field formatters render their output?
Comments
Comment #1
Wim LeersIIRC the reason there isn't something like
hook_field_prepare_view()
is the performance concern: that'd be an order of magnitude more hook executions than forhook_entity_prepare_view()
. OTOH, since entities are now render cached by default, it should be less of a problem to do that now.Looking at
FormatterInterface
,::prepareView()
is where you want to alter, and::prepareView()
is invoked from only one place:EntityViewDisplay::buildMultiple()
. No hook is being invoked there. So I don't think there's anything you can do.The only thing I can think of is … to alter the field formatter plugin discovery, inject your own "RDF wrapped field formatter" class in there with an additional parameter, that calls the actual field formatter class's methods, but in the case of
prepareView()
executes some additional code. That'd be one way of simulating a hook … but that's pretty horrible :DComment #2
scor CreditAttribution: scor commentedThanks Wim, your help is always appreciated :)
Another option is I think to introduce a new hook when views starts to render the fields, similar to the way entity invokes hook_entity_prepare_view(), and the RDF module could implement this hook. How does that sound?
Comment #13
cilefen CreditAttribution: cilefen commentedI am closing this support request because there have been no recent comments.