0

This is a follow-up to a recent question of mine, that was solved easily. The construct is very similar, but this time I need to add conjunction characters (in my case a comma and a space: ", ") when rewriting the field.

Here's the setup: In a node type, I have three fields: (1) methods, (2) geography, (3) keywords. All three fields are textareas where comma-separated values are entered. These fields are hidden on normal node view (node type -> manage display), but there is a views block showing these values.

Now, I want all of the fields' content to appear as a single field with a label with additional commas between.

An example: a node has the following values (1) methods: "evaluation, regeneration" (2) geography: "Denmark, Germany" and (3) keywords: "rivers, restoration".

If all (or at least keywords) fields are filled, the desired result is easy to achieve in the block:

  • Methods: no label; exclude from display; no result behaviour: hide if empty, hide rewriting if empty; rewrite results: ", [methods]"
  • Geography: no label; exclude from display; no result behaviour: hide if empty, hide rewriting if empty, no result output: "[methods]"; rewrite results: ", [geography][methods]"
  • Keywords: label "Keywords"; no result behaviour: hide if empty, hide rewriting if empty, no result output: "[geography]"; rewrite results: "[keywords][geography]"

For the above example this produces

  • Keywords: rivers, restoration, Denmark, Germany, evaluation, regeneration

Thus, the required commas (between restoration and Denmark as well as between Germany and evaluation) are added correctly. This also works, if either methods or geography are empty or even both, output looks like:

  • Keywords: rivers, restoration, evaluation, regeneration
  • Keywords: rivers, restoration, Denmark, Germany
  • Keywords: rivers, restoration

Now the culprit. If keywords' field is empty, there are two scenarios.

  1. Both geography and methods are empty, also. Then the whole (rewritten keywords) value is also empty and label is not displayed.
  2. Either or both geography and method have values. Then there is a superfluous comma at the start of the value like
    • Keywords: , Denmark, Germany
    • Keywords: , evaluation, regeneration
    • Keywords: , Denmark, Germany, evaluation, regeneration

The actual question: How should I setup the view to avoid this starting comma if keywords' field is empty?

The only easy way I found is to make the keywords' field required so there is always a value present. But I'd like to set it up without marking fields mandatory and without changing the fields' types or widgets.

2 Answers 2

1

You could rewrite the output of the field with your comma (but without conditions), then exclude from display and use a global textfield to output the field again with your conditions, so if the field is empty it won't show the comma.

5
  • The problem persists. The comma will be added to geography or methods if geography or methods has a value. But the comma is only required if keywords is set as well. Of course, I could add the comma to the rewrite rule for keywords like "[keywords], [geography]" but then I would have a trailing, superfluous comma if geography is empty and keywords not.
    – Paul
    Commented Sep 24, 2014 at 13:04
  • So you say that every field of these three can be empty and also in any combination?
    – Volker
    Commented Sep 24, 2014 at 13:30
  • Correct, none, one, two or all can be empty. I'm looking for a way to cover all possibilities with correct commas.
    – Paul
    Commented Sep 24, 2014 at 13:45
  • mmh, that's tricky indeed. last idea i can offer is to try to show the fields as comma separated inline fields and ad the label somehow otherwise or check out this module: drupal.org/project/views_delimited_list
    – Volker
    Commented Sep 24, 2014 at 14:32
  • I am pursuing a totally different approach using CSS right now, if that won't work, I will try the delimited list version.
    – Paul
    Commented Sep 24, 2014 at 14:35
1

My solution to overcome the issue now is not purely through views settings but involves some CSS. First, I removed all commas and instead rewrote the fields to

<span class="methods">[methods]</span>
<span class="geography">[geography]</span>
<span class="keywords">[keywords]</span>[geography][methods]

Next, I added a single CSS rule:

span.keywords + span.geography:before,
span.keywords + span.methods:before,
span.geography + span.methods:before {
  content: ', ';
}

Et voilà, commas are only added to following elements.

1
  • That's a nice solution!
    – Volker
    Commented Sep 25, 2014 at 7:25

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.