2

I have a drupal view set up to show links to certain documents. These documents are grouped by department in a Grid layout and then sorted by date. What I want to do is if there are no results available for a particular group in the grid layout, display a no results text line for that. I am willing to just put php generated text in the header or footer if that is an option. I do not want to generate a page of non-sense content titled "No Results" to inject it in the grid (that's what we're doing now)...I'm trying to get it to handle itself. Hopefully this layout will demonstrate it better if it's hard to understand:

Department Documents

Department 1

04/25/2015 <--(These dates are rewritten to link to documents)

04/26/2016

Department 2

No results

...

Edit

I would rather not make this behavior global for all Grid views, if possible. I have other grid views where I do not want to display this, it is only on this one view.

2 Answers 2

0

The best to do this is using theme layer. In your case,views-view-grid.tpl.php is used to display the results.

There are different ways to alter that.

Solution A: Using template preprocessors

Add this function to template.php in your theme folder.

<?php
/**
 * Implements hook_preprocess_views_view_grid().
 *
 * Check if there is no result for this group and provide an appropraite message.
 *
 */
function phptemplate_preprocess_views_view_grid(&$variables) {
    $view = $variables['view'];
    if ($view->name == 'YOUR_VIEW_NAME') { // Only do this for a specific view
        if (empty($variables['rows'])) {
            $variables['caption'] = t('No results.');
        }
    }
}

Solution B: Overriding the template file

Copy views-view-grid.tpl.php to your theme folder and then modify it. You may also want to rename the template file so it would only apply to a specific view. (Learn how to find the appropriate template file name for Views).

<?php

/**
 * @file
 * Default simple view template to display a rows in a grid.
 *
 * - $rows contains a nested array of rows. Each row contains an array of
 *   columns.
 *
 * @ingroup views_templates
 */
?>
<?php if (!empty($title)) : ?>
  <h3><?php print $title; ?></h3>
<?php endif; ?>

<?php // We check if there is no result we print a meesage ?>
<?php if (empty($rows)) : ?>
  <?php print t('No results.'); ?>
<?php endif; ?>

<table class="<?php print $class; ?>"<?php print $attributes; ?>>
  <?php if (!empty($caption)) : ?>
    <caption><?php print $caption; ?></caption>
  <?php endif; ?>

  <tbody>
    <?php foreach ($rows as $row_number => $columns): ?>
      <tr <?php if ($row_classes[$row_number]) { print 'class="' . $row_classes[$row_number] .'"';  } ?>>
        <?php foreach ($columns as $column_number => $item): ?>
          <td <?php if ($column_classes[$row_number][$column_number]) { print 'class="' . $column_classes[$row_number][$column_number] .'"';  } ?>>
            <?php print $item; ?>
          </td>
        <?php endforeach; ?>
      </tr>
    <?php endforeach; ?>
  </tbody>
</table>
2
  • I wouldn't want to do this everywhere where there is a view that uses the grid style, as there are others. This would only be on one particular view. Commented Apr 25, 2015 at 15:00
  • The answer is updates to show how to specify the view for the changes.
    – Alexar
    Commented Apr 26, 2015 at 11:33
0

I was kind of overthinking this a good bit...I actually found a way to do this with no hand written php involved. This way, I could also change the styling from grid to html list or whatever I wanted with no trouble. The way I've done it may not be the best way, but since there are usually only a handful of entries shown (no more than 2 or 3 for each department for 4 departments), performance shouldn't be an issue.

What I did is set up a view page for each department. I overrode the filtering in each and only filtered those pages to get results for that one department. I wrote a no results behavior for each individual page. Then, I set up a master view. In the header of that, I displayed alternating titles that linked to the individual lists if people wanted to view only those (as text areas) and then the view content after each (view areas). The image below shows what that looked like in the view config. There were a lot of overrides to do, but this got me the exact list-like style I was looking for.

enter image description here

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.