Acces raw data in Drupal 8 view template -
in overridden template of view module (views-view-field.html.twig), i'm trying access raw data of field.
in doc file, can read this:
* available variables: ... * - fields: list of fields, each 1 contains: ... * - raw: raw data field, if exists. not output safe. ...
but it's alway's empty.
{{ dump(fields.field_myfieldname) }}
print object(stdclass)[1899] ...
{{ dump(fields.field_myfieldname.raw) }}
print null
i want raw build file path value of field.
why it's empty? there other whay raw data of field in template ?
edit : i'm trying this:
<img src="/path/to/image/{{ fields.title.raw | escape('uri')}}.jpg" />
why hard ?
instead of building stuff in twig files, should instead preprocess data , reference new variable in twig file.
for example, if custom theme called mytheme, in mytheme.theme file, add following function
function mytheme_preprocess_field(&$variables, $hook) { if( $variables['field_name'] === 'field_myfieldname') { // manipulate data , return in variable. $data = $variables['items'][0]['content']['#context']['value']; $variables['customfield'] = "whatever want, can html"; } }
then in field--node--field-myfieldname.html.twig file, call new variable
{{ customfield }}
if returning html, call new variable so
{{ customfield|raw }}
Comments
Post a Comment