I'd like to build a custom "add more" link under each group by in views. I'd also need the nid of the group by field as a token.

Is this possible? Thanks.

  • Is this "group by" views contains node content? – RajeevK Mar 7 '16 at 11:34
  • Yep. Grouping content by an entity reference field. – Niall Murphy Mar 7 '16 at 11:35
  • what does "add more" button do in this case? – oksana-c Mar 7 '16 at 15:23
  • A link that prepopulates some taxonomy terms and also an entity reference field in an entity form with the grouping node's id. – Niall Murphy Mar 7 '16 at 16:44

If you need nid of current node as token, then just add Content: Nid in field list, and check exclude from display before saving it. Move this field at the top of the list & you will be able to use it as token in any other field under Rewrite Result.

But if you want to use nid of entityreference field as token, then you need to a Relationship under Advance tab in your view. Then after follow the same process which I explained in first paragraph. But this time you will have to use your added relationship.

For custom Add More link, just add Global: Custom text in field list & put HTML for this Add More link.

| improve this answer | |
  • That adds custom text to each row but I need it after each group. However, adding a custom field and using it for grouping nr.2 gives what I need but above the group. I need it below the grouping. – Niall Murphy Mar 7 '16 at 12:00
  • @NiallMurphy - I am unable to understand your requirement about placing. Is adding something into views footer would work for you? – RajeevK Mar 7 '16 at 12:10
  • If there are 100 rows, then there are 10 groups of 10 rows. I need to add a "pseudo footer" after each group of 10 rows. – Niall Murphy Mar 7 '16 at 13:12

You can start by overriding Style Output plugin for your view by copying the default template views-view-unformatted.tpl.php to your theme's folder and renaming it to views-view-unformatted--your-view-machine-name.tpl.php (machine name of the view is used, so that only the particular view is overridden).

then you could do something like this in the template (comments included):

<?php if (!empty($title)): ?>
    <!-- H3 outputs the title of the group -->
  <h3><?php print $title; ?></h3>
<?php endif; ?>
<?php foreach ($rows as $id => $row): ?>
  <div<?php if ($classes_array[$id]) { print ' class="' . $classes_array[$id] .'"';  } ?>>
    <?php print $row; ?>
<?php endforeach; ?>

<!-- adding footer to each group --> 
<div class="view-group-footer">
        // $id is the number of the view row
        // each row will have data of the entity_refence field as it is used for grouping
        // we need to grab the target_id (node ID) of the entity that is used to form the group
        // because the target_id of each row inside the group will be the same - we only need to grab it once and then form the custom link
        // so we grab it from the last row of the group, otherwise we would grab it inside the foreach loop

        $last_row_in_group = $view->result[$id];

        // NOTE: "field_field_entity_reference" has extra 'field_' prefix at the begining followed by the actual machine name of the entity reference field
        print '<a href="your_url_'.$last_row_in_group->field_field_entity_reference[0]['raw']['target_id'].'">add more</a>';

PS. the link should be generated with l() function, I placed < a > tag in the code as a quick example.

| improve this answer | |
  • @NiallMurphy did you find this approach effective to your needs? – oksana-c Mar 11 '16 at 12:03
  • I had a look and your code looks good.. But I'm using the views_pivot module. I'm pretty inexperienced when it comes to tpl files and the views_pivot tpl looks very complicated to me. Putting it off till the weekend! – Niall Murphy Mar 11 '16 at 12:51

If anyone is interested, I just had to do this same thing - but for Drupal 8. Here's a twig version of the 'if last row' solution above, placed in the while loop of the same file name as in the example above:

{% if loop.last %}
    set row_classes = [
      default_row_class ? 'views-row-last',
   <!-- or whatever you need to do to the last row -->
| improve this answer | |

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.