0

How can I access/print the raw value of a date field? (or any field for that matter). I am working in a View template and I typically use $fields['field_myfield']->content. But that gives me the HTML not just the value.

My template.php file:

 function mytheme_preprocess_views_view_fields(&$variables) { $view = $variables['view']; if($view->name == "news_main_list") { $variables['news_date'] = $variables['fields']['field_news_date_text']->content; $variables['news_type'] = $variables['fields']['field_news_type']->content; $variables['theme_hook_suggestions'][]='views_view_fields__news_main_list__block_1'; } } 

My views-view-fields--news-main-list--block-1.tpl.php file:

 <?php if (!empty($field->separator)): ?> <?php print $field->separator; ?> 
 <?php print $field->wrapper_prefix; ?> <?php print $field->label_html; ?> <p class="newsTitle"><?php print $fields['title']->content; ?></p> <p class="dateByLine"> <?php print $news_date; ?> | <?php print $news_type; ?> <?php if ($news_type == "event") : ?> <?php print 'hello'; ?> <?php endif; ?> </p> <?php print $field->wrapper_suffix; ?> 

3 Answers 3

1

You must specify which one of Views template files you are working on, If it is views-view-fields.tpl.php then $fields variable is availabe but if it is not views-view-fields.tpl.php then $view object is available. use kpr($variable) to get the $variable available properties.

You can also use hook_preprocess_views_view_fields(&$var) to set the values of fields in $var array then retrieve them in fields template file


UPDATE:

First define the following function in your template.php file

 function THEME_preprocess_views_view_fields(&$variables) { $view = $variables['view']; if($view->name == "THE_VIEW_NAME") { $variables['my_field'] =strip_tags( $variables['fields']['field_myfield']->content); $variables['title'] =strip_tags( $variables['fields']['title']->content); $variables['theme_hook_suggestions'][]='views_view_fields__SOMETHING'; } } 

By this function the $my_field and $title variables are available in the views-view-fields--SOMETHING.tpl.php file. You have to create views-view-fields--SOMETHING.tpl.php file in your theme directory and use above variables in it. views-view-fields--SOMETHING.tpl.php file is created to theme every output (record or row of columns) of your View. Make sure you are using - instead of _ when creating views-view-fields--SOMETHING.tpl.php file.

$view variable is a variable that contains every detail of the current View. you can use kpr($view) to see its results.

1
  • Comments are not for extended discussion; this conversation has been moved to chat. Commented Jul 20, 2015 at 21:11
0

if you want, you can use variable $variables. In file template, use code

dpm($variables) 

and see that.

0

If you want the raw value, and you're in a Views Row template (usually somehow overriding views-view-fields.tpl.php) you can use the $row object to get the associated fields.

In this case, you can try $row->field_field_myfield[0]['raw']['value'];

1
  • Thanks, is there another way to do this without setting UND to 0, and actually using 'raw'. I've read that this is bad practice. Commented Jul 20, 2015 at 19:42

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.