0

I had this issue before which I solved: Add field date to node tpl

Now I need to add the date field in the views.tpl.php

Here's the code:

enter image description here

I want to replace "$date->node_created" with the field_date.

Here's what I know: I can't use $content['field_date'] because the $content array doesn't exist in this template file.

Thank you

  • Logic like this should be done in a preprocess function not your tpl. Then you can simply add it to your $vars and render it in your tpl. – sareed Feb 26 '16 at 21:30
  • 1
    After reading this a second time I am wondering what exactly you are trying to do. It seems to me there should be a much better way to handle what I am seeing. Can you elaborate on exactly what you are trying to do and what you have tried? – sareed Feb 26 '16 at 21:36
0

To know what fields you have available to your View, you can use a custom module, and there you write the following:

function hook_views_pre_render(&$view) {
     if($view->name == "YOUR_VIEW_NAME"){
         foreach($view->result as $key => $r){
             echo "<pre>"; print_r($view->result[$key]); echo "</pre>";
     ....
         } // foreach
      } // if view->name
} // function
  • This is what I found: i.imgur.com/6Jk6aJq.jpg I tried "$date->field_date["und"][0]["value"]" but it says: Notice: Undefined property: stdClass::$field_date in include() – Drii7 Mar 1 '16 at 10:23
  • Try with $data["field_date"][LANGUAGE_NONE][0]["value"] – Cesar Mar 1 '16 at 11:20
  • By the way, you are re-writing $date in line 10. That is definitely not a good idea. – Cesar Mar 1 '16 at 11:22
  • It doesn't work. I managed to display the field as you can see here i.imgur.com/8A2vzrs.jpg but it appears "node" and not the date... (btw, I changed the field_date to field_dateofevent) where can I change this value? – Drii7 Mar 1 '16 at 11:35
  • I am not sure of what you pretend to do. You are mixing concepts and you must definitely know if you want to use or modify your data in the *.tpl, in the module or where, and know what data you have available at each moment. If you are using a view but do not include the field in that view, it will not render. – Cesar Mar 1 '16 at 11:45

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.