2

I want to change the color of the node titles from a view (fields on table). My first ides was to alter the title and add a span with inline css.

function views_titles_views_pre_render(&$view) {
if ($view->name == 'documents' && $view->current_display == 'page') {
    $span_open  = '<span style="color: green !important;">';
    $span_close = '</span>';
    $view->style_plugin->render_fields($view->result);
    foreach ($view->style_plugin->rendered_fields as $index => &$rendered_result) {
      // Add HTML markup icon on rendered title field
      $rendered_result['title'] = $span_open . $rendered_result['title'].$span_close;
    }
  }
} 

Although my attempt was a success, I managed to alter the title the css rule did not alter the color of the a tag. I figured out that I should go deeper into the title. As far I can tel the dpm of the views_pre_render does not show more options.

I have experience with drupal site building, but I am not in any way a php developer.

Can someone help me figure this out?

ps: I know there other more easy ways to achieve that but I want to do it with a module.

1
  • 1
    If you want to do it with a module, you can do it by simply adding a css files in a module. – Jimmy Ko Jan 25 '17 at 10:20
1

An easier (and more maintainable) way to achieve this would be to set a class in your span, instead of hardcoding the style. Something like the following:

$span_open = '<span class="my-title">"';

You can then add a CSS file to your module, so that you can better specify the color, also overriding the a tag. For instance:

.my-title, .my-title a {
  color: green;
}

For more details about adding CSS to your module, see https://api.drupal.org/api/drupal/includes!common.inc/function/drupal_add_css/7.x

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.