0

I have a Views-based search page on my site. I've created a views-view-search--tpl.php file to customize the order of elements on the page. I have a specific block I'd like to return but ONLY if a user has already clicked the search button and results were returned.

Here's the code I'd like to return based on that condition:

  <?php if ($feed_icon): ?>
   <div class="feed-icon">
    <?php print $feed_icon; ?>
   </div>
  <?php endif; ?>

I guess I probably need an 'if' statement or something before that block but I'm not sure how to reference if results and/or if user clicked the search button...

Anyone know how to do this?

1 Answer 1

1

This should probably be done in a pre-processor rather than a template file, but I'll stick to the question as asked, for now:

<?php if (!empty($view->exposed_input) && !empty($rows)): ?>
 <?php if ($feed_icon): ?>
  <div class="feed-icon">
   <?php print $feed_icon; ?>
  </div>
 <?php endif; ?>
<?php endif; ?>
2
  • this works. 2 questions: 1) why do you think it should be done in preprocess? 2) using the above, if a user just clicks search without selecting any of the filters or typing in a search keyword they still get the download spreadsheet link allowing them to download all the nodes in the database. Is there a way to prevent this (ie. requiring them to select at least one filter and/or put in a key term)?
    – Sage
    Aug 5, 2015 at 23:53
  • 1) In general you want to keep logic out of your templates as much as possible. This is pretty simple logic so it's not so terrible, but once you make it even a bit more complex, it should go in preprocess and the decision about whether or not to show the $feed_icon could just be stored in a boolean value that you check in your template. 2) Views will add the default filter values to the querystring when you click submit, so you'd have to evaluate each submitted value and see if it matches the default for that filter to determine if the user made a meaningful change.
    – Aaron
    Aug 6, 2015 at 15:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.