Skip to content

Instantly share code, notes, and snippets.

@bbmatt
Last active December 24, 2015 21:29
Show Gist options
  • Save bbmatt/6865302 to your computer and use it in GitHub Desktop.
Save bbmatt/6865302 to your computer and use it in GitHub Desktop.
Drupal - get the path of an image without resorting to modules / Custom views-view--fields template
Views template API docs:
https://api.drupal.org/api/views/theme!theme.inc/group/views_templates/7
Function file_create_url:
https://api.drupal.org/api/drupal/includes%21file.inc/function/file_create_url/7
Function field_get_items:
https://api.drupal.org/api/drupal/modules%21field%21field.module/function/field_get_items/7
<?php
// Get our node id
$node = node_load($row->nid);
// Grab an array of field items for field_image
$featuredImage = field_get_items('node', $node, 'field_image');
// Create a url from our uri
$featuredImage = file_create_url($featuredImage[0]['uri']);
?>
<div>
<div style="background-image: url('<?php print $featuredImage; ?>');">
<header>
<h1><?php print $fields['title']->content; ?></h1>
<h2><?php print $fields['field_subtitle']->content; ?></h2>
</header>
</div>
</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment