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; ?>
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.

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'];

  • 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. – srodrig Jul 20 '15 at 19:42

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.