3

I have several fields in my view:

  1. Path (excluded from display)
  2. Image
  3. Body
  4. Button (rewritten to use [path])

I need to output the image like so (this is a requirement):

<a href='[path]' style='background: url([image]);'>TITLE</a>

I cannot do this using rewrite rules because of https://www.drupal.org/node/417956 which explains that views runs all rewrites through filter_xss_admin(). This function strips out the style attribute on the a tag.

I am attempting to implement this in a view template, specifically the Field Content: Image (ID: Image) template:

<?php

/**
 * @file
 * This template is used to print a single field in a view.
 *
 * It is not actually used in default Views, as this is registered as a theme
 * function which has better performance. For single overrides, the template is
 * perfectly okay.
 *
 * Variables available:
 * - $view: The view object
 * - $field: The field handler object that can process the input
 * - $row: The raw SQL result that can be used
 * - $output: The processed output that will normally be used.
 *
 * When fetching output from the $row, this construct should be used:
 * $data = $row->{$field->field_alias}
 *
 * The above will guarantee that you'll always get the correct data,
 * regardless of any changes in the aliasing that might happen if
 * the view is modified.
 */
?>
<a href='/' style='background-image: url(<?php print $output; ?>);'><?php print $row->node_title; ?></a>

I have tried this, [path] remains unchanged:

<a href='[path]' style='background-image: url(<?php print $output; ?>);'><?php print $row->node_title; ?></a>

and this, [path] remains unchanged:

<a href='<?php print token_replace("[path]", array('node' => $row->_field_data['nid']['entity'])); ?>' style='background-image: url(<?php print $output; ?>);'><?php print $row->node_title; ?></a>

I cannot find the value of the path anywhere in any of the variables. How can I output the value of [path] in my view template?

  • Which template views template file are you overriding? field or fields? – Collins Jun 17 '16 at 14:40
  • I think it's because you excluded it, try un-excluding your path from display. – No Sssweat Jun 18 '16 at 3:51
  • including the path (not excluding it) has no effect – Scott Joudry Jun 22 '16 at 17:18
1

Can you get the node id you are aiming for? If so, try using drupal_get_path_alias()

$linknid = $fields['field_link_to_content']->content;

$alias = drupal_get_path_alias('node/' . $linknid);

<a href='<?php print $alias; ?>' style='background-image: url(<?php print $output; ?>);'><?php print $row->node_title; ?></a>

UPDATE - this is how I did it, based on this answer:

<a href='<?php print drupal_get_path_alias('node/' . $row->nid); ?>' style='background-image: url(<?php print $output; ?>);'><?php print $row->node_title; ?></a>
  • I'm sure I could. I was hoping to avoid another db call...but I will give it a shot – Scott Joudry Jun 17 '16 at 16:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.