0

I have 2 fields; field_a and field_b. When field_b is not empty, I need to append a hyphen after field_a.

EX: field_a - field_b

Then, when field_b is empty, it doesn't show the hyphen

EX: field_a

I have been trying THEME_preprocess_field and THEME_field in template.php, but can't get it to work. This is what I have, but its basically just a copy, but I don't need all the extra stuff, and I need it to only show up on field_a when field_b is not empty.

function THEME_field($variables) {
  $output = '';


  $output .= '<div class="field-itemsuu"' . $variables['content_attributes'] . '>';
  foreach ($variables['items'] as $delta => $item) {
    $classes = 'field-item ' . ($delta % 2 ? 'odd' : 'even');
    $output .= '<div class="' . $classes . '"' . $variables['item_attributes'][$delta] . '>' . drupal_render($item) . ',' . '</div>';
  }
  $output .= '</div>';

  // Render the top-level DIV.
  $output = '<div class="' . $variables['classes'] . '"' . $variables['attributes'] . '>' . $output . '</div>';

  return $output;
}

1 Answer 1

1

You can keep it simple and just add the following to your css

.field_b:before{
  content:"-";
}

It will only show when there is a value for the b field.

2
  • yup because we can safely ignore IE7 i guess :) quirksmode.org/css/contents.html#t20
    – tenken
    Feb 6, 2013 at 23:48
  • yup. users who fail to update their browser should be left behind. At some point we need to cut the umbilical cored to take advantage of new technology. Not that CSS Pseudo-elements are anything fancy.:) Feb 7, 2013 at 0:11

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.