1

So in Views you can add a CSS class (under style settings) to your field. I need to print the same class in a parent div of the calendar module.

The fields are rendered to calendar content with the calendar-item.tpl.php That file looks like this:

$index = 0;
?>
<div class="<?php print !empty($item->class) ? $item->class : 'item'; ?>">
  <div class="view-item view-item-<?php print $view->name ?>">
  <div class="calendar <?php print $item->granularity; ?>view">
    <?php print theme('calendar_stripe_stripe', array('item' => $item)); ?>
    <div class="<?php print $item->date_id ?> contents">
      <?php foreach ($rendered_fields as $field): ?>
        <?php if ($index++ == 0 && (isset($item->continuation) && $item->continuation)) : ?>
        <div class="continuation">&laquo;</div>
        <?php endif;?>
        <?php print $field; ?>
      <?php endforeach; ?>
    </div>  
    <?php if (isset($item->continues) && $item->continues) : ?>
    <div class="continues">&raquo;</div>
    <?php else : ?>
    <div class="cutoff">&nbsp;</div>
    <?php endif;?>
  </div> 
  </div>   
</div>

So I need to get that class in the first DIV

<div class="<?php print !empty($item->class) ? $item->class : 'item' . ENTER-FIELDCLASS-HERE; ?>">

I've been looking in the VIEWS module and the code taht checks if you entered a CSS class should be this part in the THEME.inc file:

if ($classes = $object->handler->element_classes($view->row_index)) {
          if ($class) {
            $class .= ' ';
          }
          $class .=  $classes;
        }

A more complete part:

$previous_inline = FALSE;
  $vars['fields'] = array(); // ensure it's at least an empty array.
  foreach ($view->field as $id => $field) {
    // render this even if set to exclude so it can be used elsewhere.
    $field_output = $view->style_plugin->get_field($view->row_index, $id);
    $empty = $field->is_value_empty($field_output, $field->options['empty_zero']);
    if (empty($field->options['exclude']) && (!$empty || (empty($field->options['hide_empty']) && empty($vars['options']['hide_empty'])))) {
      $object = new stdClass();
      $object->handler = &$view->field[$id];
      $object->inline = !empty($vars['options']['inline'][$id]);

      $object->element_type = $object->handler->element_type(TRUE, !$vars['options']['default_field_elements'], $object->inline);
      if ($object->element_type) {
        $class = '';
        if ($object->handler->options['element_default_classes']) {
          $class = 'field-content';
        }

        if ($classes = $object->handler->element_classes($view->row_index)) {
          if ($class) {
            $class .= ' ';
          }
          $class .=  $classes;
        }

        $pre = '<' . $object->element_type;
        if ($class) {
          $pre .= ' class="' . $class . '"';
        }
        $field_output = $pre . '>' . $field_output . '</' . $object->element_type . '>';
      }

Not the most clear code to me. It can't be that hard. If only something like $field->class would work!

Thanks in advance!

2 Answers 2

0

You can add a class through the views interface:

  1. Go to views ui
  2. Under "Fields", click on the field you want to change
  3. Click on "Style Setting"
  4. Choose "Customize Field HTML"
  5. Click "CSS Class" and enter your new css class

Alternatively, you can also achieve this through theme pre rendering.

  • View machine name: "your_view_name"
  • Field machine name: 'yourfield'
  • New class: "your_new_css_class"

Add to the file template.php in your theme directory:

function yourtheme_views_pre_render(&$view) {

  if ($view->name == "your_view_name") {
     $view->field['yourfield']->options['element_class'] = "your_new_css_class";
  }

}

To debug, download and enable the devel module (http://drupal.org/project/devel), and write out the variables:

function yourtheme_views_pre_render(&$view) {
    if ($view->name == "your_view_name") {
        drupal_debug($view->field['yourfield']->options['element_class']);
    }
}

The output will appear in tmp/drupal_debug.txt (depending on how you configured your tmp directory).

5
  • I think you've answered the question in reverse, eg, $view->field['yourfield']->options['element_class'] is what the OP is asking about finding the value of in the template, not setting it.
    – Jimajamma
    Dec 22, 2013 at 1:02
  • Hmm, to find the value, I'd just look with firebug. @stefki Looks like the question needs clarification. I understood, that the OP wanted to print the same class on the views page as he sees on the calendar page.
    – Ursula
    Dec 22, 2013 at 1:44
  • Dear @Ursula and Jimajamma, I do apologize for the late response. I was abroad for a week, cut off from any internet connection. Happy New Year and best wishes to you all! Now to clearify my question: All I want is to print a field value as css in the code. I know of the method to do it in Views, but I need to print it a DIV-level higher. The div that is containing the field value. If something like Jimajamma suggested ($view->field['yourfield']->options['element_class']) could work that would be perfect.
    – stefki
    Jan 2, 2014 at 8:26
  • This might help: <div class="inner"> <div class="item I_WANT_TO_PRINT_THE_FIELD_VALUE_HERE "> <div class="view-item view-item-testcalendar"> <div class="calendar monthview"> <div class="calendar.1036.field_event_date.0.0 contents"> <div class="views-field views-field-field-wanneer"> <div class="field-content Field_Value">Field_Value</div> </div> </div> <div class="cutoff"> </div> </div> </div> </div> </div> @Jimajamma
    – stefki
    Jan 2, 2014 at 8:29
  • How can I print the Field_location value? Array ( [0] => stdClass Object (... [_field_data] => Array ( [nid] => Array ( [entity_type] => node [entity] => stdClass Object (... [field_location] => Array ( [und] => Array ( [0] => Array ( [value] => Lala-land [format] => full_html [safe_value] => Lala-land ) ...
    – stefki
    Jan 2, 2014 at 8:41
0

So I found the solution:

$item->entity->YOUR_FIELD['und'][0]['value']

After a month of digging trough thousands of lines of codes, I just had to add this simple line.

I don't know if it's best practice, but in my case it'll do the job. If you try to print a value from Views and need the path, try print_r $item for a change. It gave me a better view to define the path than print_r $view.

Cheers!

Your Answer

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

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