0

You can see the question I am reffering here

I wrote a .tpl file to override the output of some fields. I wanted the field will be printed with html like below (and succeed on doing so). here is the output, when I am printing the field as a panel, through panels:

<div class="field field-name-field-preptime field-type-number-integer field-label-hidden">
<div id="md11" class="field-items">    
          <div itemprop="prepTime" class="field-item even"></div>
      <time datetime="PT3H45M" itemprop="prepTime">3 hours 45 min</time>
  </div>

For some reason, when I am printing this fields regualry or with panels, the output is fine. but, when I am printing them through a content-pane of views, the output is:

<div class="field-content">25 min</div>
</div>
    <span class="views-label views-label-field-preptime">Prep Time: </span>
<div class="field-content">25 min</div>
</div>

as you can see, views printing the field with "itemprop="prepTime" and without datetime="PT3H45M".

Why is that happening, and how can I fix it?

Thanks for your help!


UPDATE:
After I used @FiNeX tip, and checked the option "Use field template", it helped me solve some issues. Now the prepTime and cookTime fields are using the field template perfectly.

But, the totalTime template, which get the value from a computed field, print the output differently between views, and the regular way.

In the regular way the output is:

<div id="md15" class="field-items">    
          <div itemprop="totalTime" class="field-item even"></div>
      <time datetime="PT1H35M" itemprop="totalTime">1 hour 35 min</time>
  </div>

As you can see, the "datetime=PT1H35M" and the hour ("1 hour 35 min") are printed perfectly.

But, with views, the output is:

<div id="md5" class="field-items">    
          <div itemprop="totalTime" class="field-item even"></div>
      <time datetime="P" itemprop="totalTime">5700</time>
  </div>

As you can see, Views now is using the template, but the output of the template is:

  • datetime="P" and not datetime="PT1H35M"
  • "5700" and not "1 hour 35 min" (by the way 5700 seconds are equal to 1 hour 35 min)

Why views is changing the output of the template after using it?

Here is the part of the template, that is creating the "Date time" and the time as I want to print it:

<?php
function time_to_iso8601_duration($time) { 
    $units = array(
        "Y" => 365*24*3600,
        "D" =>     24*3600,
        "H" =>        3600,
        "M" =>          60,
        "S" =>           1,
    );
$str = "P";
$istime = false;

foreach ($units as $unitName => &$unit) {
    $quot  = intval($time / $unit);
    $time -= $quot * $unit;
    $unit  = $quot;
    if ($unit > 0) {
        if (!$istime && in_array($unitName, array("H", "M", "S"))) { // There may be a better way to do this
            $str .= "T";
            $istime = true;
        }
        $str .= strval($unit) . $unitName;
    }
}

return $str;
}
 ?>


<div class="<?php print $classes; ?>"<?php print $attributes; ?>>
  <?php if (!$label_hidden): ?>
    <div class="field-label"<?php print $title_attributes; ?>><?php print $label ?>:&nbsp;</div>
  <?php endif; ?>
  <div class="field-items"<?php print $content_attributes; ?>>    
    <?php foreach ($items as $delta => $item): ?>
    <?php $time = strtotime(render($item), 0); ?>
      <div class="field-item <?php print $delta % 2 ? 'odd' : 'even'; ?>"<?php print $item_attributes[$delta]; ?>></div>
          <time datetime=<?php print time_to_iso8601_duration($time); ?> itemprop="prepTime"><?php print render($item); ?></time>
    <?php endforeach; ?>
  </div>
</div>

1 Answer 1

1

views renders fields with his own template. I imagine you're using a view which show "fields", right? On each field settings inside the view you can check the following option:

"Use field template"

If you select this option the view will use the field template instead of the view template.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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