Use Color Field Value in Views Field Template File I have two fields in my view:

field_background_colour and field_heading

The are both being pulled from nodes and displayed in a rotating hero banner. Each node has a different colour and different heading.

The field_background_colour is defined by the color field module and therefore can be any colour they select.

I need to add the value of the background colour in hex (e.g. #000000) to the html output of field_heading.

&lt;div style="background:[field_background_colour];"&gt;[field_heading]&lt;/div&gt;


I tried using some html and replacement patterns in the rewrite of the field but it seems that gets stripped out by views. It seems to me that I need to use the field's template file to get what I want.
In this use case, I believe inline styles are the way to go. There are methods to write a CSS file in your module or theme, for example, but since users can select, and update at any time, any color code, all situations would be very difficult or messy to manage. Inline styles are not ideal in the grand scope of things, but with multiple users, and multiple hex values reaching the millions, sometimes requirements make our choices for us. And plus, we just want to get it done.



Below assumes the view shows nodes, both fields are on the content type, and the title being styled is the node title, not the views title. This is best achieved setting the view to display content, and utilizing template files for your chosen display mode such as node--content-type--teaser.tpl.php.

node--content-type--rotating-hero.tpl.php:

&lt;section&gt;
  &lt;h1 
    title="&lt;?php print render($title); ?&gt;" 
    alt="&lt;?php print render($title); ?&gt;"
    &lt;?php if (isset($content['field_background_colour'])) : ?&gt;
      style="color: #&lt;?php print $content['field_background_colour']['#items'][0]['value']; ?&gt;;"
    &lt;?php endif; ?&gt;
  &gt;
    &lt;?php print render($title); ?&gt;
  &lt;/h1&gt;
&lt;/section&gt;


Here, each .views-row will contain an individual node display, and you can focus your javascript and CSS on those for the slider.

*multiple h1 tags on the same page need to be wrapped in a &lt;section&gt; tag, otherwise, use h2 tags.

*Templates are related to structure and usage. Views templates are for the structure and layout of content aggregation, node templates are the structure of individual entities. Wherever the field (or desired data) resides is the best place to theme template files.



Below assumes you need to style the views title itself...

will need more info about where the color field lives. If user profile, for example, you will need to load the user object to access the field value.

MYMODULE.module:

function MYMODULE_views_pre_render(&amp;$view) {
  if ($view-&gt;name == 'view_name') {
    $view-&gt;build_info['title'] = "custom title";
  }
}


template.php:

function MYTHEME_views_pre_render(&amp;$variables) {
  if (&amp;$variables['view']-&gt;name == 'view_name') {
    &amp;$variables['view']-&gt;build_info['title'] = "custom title";
  }
}