In order to make my custom views with the "rewrite the output of this field" work with translation, I would like not only show the field data but also the field "label".

Example :

When I print my view without the 'rewrite output of this field' I have this :

Class location: England
Event Start: 02/26/2011 - 08:04
Class Price: 299.00 $

When I do choose to rewrite the output, and add the following variables:

[field_classlocation_value]
[field_classevent_value]
[field_classprice_value]

I have this :

France
02/26/2011 - 08:04
299.00 $

So, you noticed that when I use the rewrite option, the field 'Label' is not shown. I cannot hardcode the field Label in the rewrite option since they won't get translated.

Is there any variable I can add to display the field label prior to the field value ?

Any help would be appreciated.
Best regards,

Comments

WillHall’s picture

Instead of using the rewrite option, you could instead use a views template.

Check out "row style theming"

http://mustardseedmedia.com/podcast/episode30

arcall’s picture

Thanks for this great tutorial. So i was able to create a views-views-field..... template and managed to see the result whem I click on the Preview. However, I can only see my change when i am in the views editor. When I load the view outside the editor, it ignore the template. Weird, it's working inside the views editor.

Someone has any ideas ?

Best regards,

mcfilms’s picture

You can mix text and those tokens in the rewrite field. So why not just put:
Class Location: [field_classlocation_value]
...etc

EDIT --Yeesh nevermind. I see you want it to be translatable. (Note to self, next time read the WHOLE post.)

A list of some of the Drupal sites I have designed and/or developed can be viewed at motioncity.com

ehsankhfr’s picture

Thank you

eidoscom’s picture

As I can understand, you need to have all your info inside a custom div.

<div class="my_class">
Class location: England
Event Start: 02/26/2011 - 08:04
Class Price: 299.00 $
<div>

(I don't add the markup for your fields, only the div wrappper to simplify the solution.)

I do this adding two Custom text fields:

  1. The content is: <div class="my_class">
  2. The content is: </div>

I rearrange the fields and put the first one above the first field I want to be wrappered with my custom class (Class location: England) and the second one under the last one (Class Price: 299.00 $).
Use the field formatting with your needs like a normal field (label and content).
On display options uncheck "Provide default field wrapper elements" to avoid printing divs and other default views markup, almost in the Custom text fields.

Apply you CSS and here it is ;)

Works for Drupal 7 and Views 3.0

eidoscom’s picture

On display options uncheck "Provide default field wrapper elements" is a must in order to work

jiakomo’s picture

This solution is simple but it does not work for me. It seems like drupal is closing the

automatically.

I did not find "Provide default field wrapper elements" either. Maybe now it is called "add default classes"?

frederickjh’s picture

@eidoscom solution still works however the controls in views have changed.

Custom text field > Style Settings

  • Untick
    • Add default classes
  • Tick
    • Customize field and label wrapper HTML
      • Set Wrapper HTML element to
        • None
davidpauljunior’s picture

Not sure how this works with the translation issue, but it's handy for adding the labels in.

Rewrite the individual fields and include a manual 'label' in the rewritten version.
For example:
<span class="label">Class Location: </span>[field_classlocation_value]

Exclude each of these fields from display.

On the final field you can rewrite the output and use the replacement patterns to display the hidden fields. Because those hidden fields have been rewritten themselves (in the step above), outputting the field here spits out the rewritten version, which includes the manually entered label.

<div class="container">
  [field_classlocation_value]
  [field_classevent_value]
  [field_classprice_value]
</div>

You may be required to add the final field twice. Once to rewrite it individually, then another time to spit out the rewritten version along with the other rewritten fields.

PaperWeight’s picture

Really good solution davidpauljunior, thanks for that.

This works when you are referencing a single value, as above, but when referencing multiple values the label is repeated for each one.

So, in my case, I am outputting tags, and with the label set to 'Tagged as', the output is:

Tagged as monkeys, Tagged as bananas, Tagged as elephants

I still cant find a way to make the label output once, THEN loop through the values, without being stuck with:

Tagged as

When there aren't any results!

davidpauljunior’s picture

I'm not sure if I've understood this correctly PaperWeight, but I think the following may help with your situation:

  • Add all the fields you require to the display and exclude them from display
  • Add the field 'Global: Custom Text'
  • On that field you can add a label to it, and then rewrite the results to display your excluded fields
  • Above the rewrite results section, there's a 'no results behaviour' section. Just tick, 'hide if empty'.

So you'll create your label in the UI of the global custom text field, the rewrite the output like so:

[field_monkeys_value]
[field_bananas_value]
[field_elephants_value]

OR...

If you're actually talking about spitting out the taxonomy tags, not separate fields then you could just use the 'Content: All taxonomy terms' field. It gives you the option to customise the label, and again you can tick the 'hide if empty' in the no results behaviour section.

I hope that helps!

hellogeli’s picture

Wow, GREAT solution! Thanks, davidpauljunior!

capfive’s picture

This unfortunately doesn't provide a final solution, however it is great, what happens if you change the field label in the content type? you would need to go into the view and change it there :(

Would love if anyone has a solution that would print the label name from the database!

However thanks for the interim solution davidpauljunior!

thommyboy’s picture

Old issue but still no good solution. It would be great if the whole output of a field would be made available via REPLACEMENT PATTERNS too.
In addition to the mentioned "what happens if I change the label of the field" other usecases are support of empty fields ("Hide rewriting if empty") which would still render labels if you put them into the rewrite...

melonangie’s picture

davidpauljunior solution doesn't get translated either.