1

I know this is possible in views -> style settings of a field, but what if you can't customize the fields?

I would like to add css to the mini calendar from the calendar-module depending on the value of a field called 'field_location'.

So class="fri mini future has-events " becomes class="fri mini future has-events field_location"

I copied the calendar-mini.tpl.php and on line 49 I changed

< td id=" < ?php print $cell['id']; ? >" class="< ?php print $cell['class']; ? >">

into:

< td id=" < ?php print $cell['id']; ? >" class="< ?php print $cell['class'] ' ' . $node->field_location['und'][0]['value']; ? >">

If I use $node = node_load(736); it works, but it adds the 'field_location' of that node to all classes. I need to place the right 'field_location' in the right class.

  • You most probably have to look into whatever is in $cell['data'] and then based upon that determine what should be wrapping it, eg, what classes should be added. – Jimajamma Dec 4 '13 at 15:30
  • Thanks for your quick response. It took me some time to figure out what's happening in the $cell['data'] part, but I can't figure out how I could use it. I also tried replacing $node->field_location['und'][0]['value']; with < ?php foreach($nodes as $events): print $events->field_location['und'][0]['value']; ? > < ?php endforeach; ? > But then it prints all the field locations after every class. So maybe I'm a small step closer? How can I determine that it puts only the right location behind the rest of the class? – stefki Dec 5 '13 at 12:42
  • you're getting closer, you need to figure out which nodes are in each day's cell in your calendar and then apply whatever information you need about those nodes to the cell they are in. At least that's what I assume you are trying to do. You will probably have to do this inside the foreach in the tpl and reload as you go along. That should cause things to change per cell. If you do it outside that foreach in the template, then yes, this will be applied to each and every cell since you aren't changing it based upon what is inside it. – Jimajamma Dec 5 '13 at 13:42
0

Copying out the relevant portion of the template:

<tbody>
  <?php foreach ((array) $rows as $row): ?>
    <tr>
      <?php foreach ($row as $cell): ?>
        <td id="<?php print $cell['id']; ?>" class="<?php print $cell['class']; ?>">
          <?php print $cell['data']; ?>
        </td>
      <?php endforeach; ?>
    </tr>
  <?php endforeach; ?>
</tbody>

If you want the <td> wrapping your cell to have an additional class based upon what is inside that cell, you will need to do something like this:

<tbody>
  <?php foreach ((array) $rows as $row): ?>
    <tr>
      <?php foreach ($row as $cell): ?>
        <?php
          $new_class= // grab information out of $cell['data']
        ?>
        <td id="<?php print $cell['id']; ?>" class="<?php print $cell['class'] . ' ' .  $new_class; ?>">
          <?php print $cell['data']; ?>
        </td>
      <?php endforeach; ?>
    </tr>
  <?php endforeach; ?>
</tbody>

If it's not easy to do, $cell['data'] is probably HTML since it is just being printed, you do have the entire $view object to work with, and there, you can grab stuff out of $view->result and correlate that to these cells.

  • Thanks for the quick answer again! The $cell['data'] looks like some uneditable stuff, so I was going for an array or object of $view->result. But I still haven't figured it out properly. – stefki Dec 5 '13 at 15:29
  • I replaced the line '$new_class= // grab information out of $cell['data']' with: ' $type = "event"; //name of content type $nodes = node_load_multiple(array(), array('type' => $type)); foreach($nodes as $events): $new_class=$events->field_location['und'][0]['value'] ;' But this messes up the calendar. I'm sorry I just can't see the clear picture here. I feel like that labrador-meme – stefki Dec 5 '13 at 15:37
  • If I print '$new_class=$products->field_location['und'][0]['value'] ;' I get a list with locations, so this part works. I don't know what to change in this foreach since it messes up the calendar. It now repeats every day for every event(node?) I have. Say I have 5 events, my calendar looks like: 11111222223333344444... The class is added in the code, but also repeated. I can feel I'm close, but I'm still facing a wall. – stefki Dec 6 '13 at 12:56
  • without seeing your actual code, I can only guess, but adding something inside a foreach() loop shouldn't change the loop itself – Jimajamma Dec 6 '13 at 13:16
  • Sorry, I added my code as an answer for a better view. – stefki Dec 6 '13 at 13:53

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.