3

I have a View that displays books and authors of each book.

BOOK 1

  • Author 1, Author 2, Author 3, Author 4, Author 5

BOOK 2

  • Author 6, Author 7

What I want to do is to show only first 3 authors of each book and add an ellipsis [...] if there is more authors. It's easy to display first 3 items (Multiple Field Settings) but how to add an ellipsis to it?


BOOK 1

  • Author 1, Author 2, Author 3 ...

BOOK 2

  • Author 6, Author 7


How can I do that?

  • 1
    did you get a chance to try my answer out? – No Sssweat Aug 20 '16 at 23:45
2

I have not tested this, so consider this an educated guess:

Go to /sites/all/themes/yourtheme/ folder and in template.php add the following code:

function yourtheme_preprocess_views_view_fields($vars){

  if ($vars['view']->name == "view_name" && $vars['view']->current_display == "block_1") {
    if (isset($vars['fields']['field_example'][3]->raw)){
      $vars['fields']['field_example']->content = $vars['fields']['field_example'][0]->raw . ', ' . $vars['fields']['field_example'][1]->raw . ', ' . $vars['fields']['field_example'][2]->raw . '...';  
    }
    else { 
      $vars['fields']['field_example']->content = $vars['fields']['field_example']->content;  
    }
  }
  • replace yourtheme with the actual name of your theme
  • replace view_name with the name of your view.
  • replace block_1 with the machine name of your view.
  • replace example with the name of your field.
| improve this answer | |
  • Thanks for the answer and idea. I did what I want with a easier way. Sorry couldn't write the answer until now, I'm adding it right soon. – herci Aug 21 '16 at 4:35
2

After thinking a lot, I found an "easy" way to do it :)

In my views,

  • I added the author field as normal.

  • I enabled the Exclude from display option.

enter image description here

  • In MULTIPLE FIELD SETTINGS, I set it to display only 1 value, starting from 0.

enter image description here

Then I added the author field again, for 3 times with the settings above.

For each, I changed the Multiple Field Settings as below:

    1. author: display 1 value, starting from 1.
    1. author: display 1 value, starting from 2.
    1. author: display 1 value, starting from 3.

For the 2. and 3. authors I enabled the Rewrite the output of this field option and this is the rewrite text for author 2: , [field_author_1] and this is for author 3: , [field_author_2].

I also enabled the Rewrite the output of this field for the 4. author and this is the rewrite text: ...

As a final step I added a Global: Custom text to views and this the text:

[field_author][field_author_1][field_author_2][field_author_3]

It's done!

  • If there is only one author it displays Author 1

  • If there is two authors it displays as: Author 1, Author 2

(remember the rewrite:, [field_author_1]).

  • If there is three authors it displays as: Author 1, Author 2, Author 3

  • And if there is more than three authors it displays as: Author 1, Author 2, Author 3 ...

(remember the rewrite for author 4:...)

| improve this answer | |
  • 2
    Sometimes simple solutions are the best right? but: that last "field_author_1" in your "global: custom text" seems bizarre to me. either it's some typo, or otherwise I think you should explain why it is not a typo ... – Pierre.Vriens Aug 21 '16 at 7:30
  • @Pierre.Vriens yes, you're right: it was a typo. Thanks. – herci Aug 21 '16 at 7:40
  • 2
    Aha, that is what I also thought! You see, "some users" actually READ answers like these, and even QA-read them ... If I may ask 1 more reated question (to make it easier to digest): how come that first field_author has nothing similar to field_author_0 or something like that? I probaby have to read your answer a few more times before I actually find the answer to that remaining question I have (unless you edit it 1 more time to also make that clear)??? – Pierre.Vriens Aug 21 '16 at 7:44
  • @Pierre.Vriens, the naming field_author_1 etc. comes from the Drupal itself. I just added the author field 4 times and it adds the numbers to the end of the machine name of the field. :) Is this the answer of your question or did I misunderstand the question? – herci Aug 21 '16 at 10:09
  • 1
    Nice Views UI non-coding solution, this didn't cross my mind. I will keep this in my back pocket. – No Sssweat Aug 21 '16 at 23:50
0

Even though those authors are probably not a term reference, you may want to try an approach similar to what is suggested in post installation issue "Trimming a Views Term reference field", using a combination of "Multiple field settings" and "Rewrite results" Here is a quote from it:

Select the field in the list, it will be in the form "Content: Tags", where tags is the field name (do no use )

With Formatter set to "Plain text" (if you do not see formatter, skip this) and "Link this field" unchecked

We are going to use a combination of "Multiple field settings" and "Rewrite results"

Set "Multiple field settings" so it only display some terms, I am going to limit to 4 (we will call this max terms). I also have the separator set to ", "

Now under "Rewrite results" check "Trim this field to a maximum length", I also unchecked "Trim only on a word boundary" and "Add an ellipsis". Pick a value for maximum length, note this is per tag. Lets pick 3.

So total length = (max terms) * (maximum length + length of separator)

In this example this becomes (4) * ( 3 + 2 ) [separator is a comma and space] for a total of twelve.

If you add ellipsis's the calculation becomes total length = (max terms) * (maximum length + length of separator + length of ellipsis)

Note the Multiple field settings (as in your question) in the above suggestion ...

| improve this answer | |
  • Thanks for the answer. In this case, the authors are Entity Reference Fields (nodes). I tried this but it didn't work, unfortunately. – herci Aug 16 '16 at 13:58

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.