-1

I have a view that displays employees and I've added an attachment that places the manager at the top of the page. I also have two exposed filters (department and name) that allow users to search the view. I'd like for the attachment to be hidden when the exposed filters are used. Any ideas? I've played with contextual filters but nothing has worked.

  • did you get it working ? Any feedback appreciated :) – Kojo Oct 1 '17 at 13:16
  • My apologies. I'm not a developer so I don't know how to implement the code you provided. I was hoping for a setting in views or something from the admin. Thanks for your help. – Kateva Oct 2 '17 at 14:14
  • I don't believe you'll find a UI solution. I added a step by step how-to. Just follow it. – Kojo Oct 2 '17 at 14:52
  • Last, my answer assumes that your attachment that places the manager at the top of the page is a view header – Kojo Oct 2 '17 at 15:28
0

I would try with hook_views_pre_render()

function CUSTOM_MODULE_NAME_views_pre_render(&$view) {
  if ($view->name == 'YOUR_VIEW_NAME') {
    //inspect the url arguments 
    //hide the view header if your exposed fields are used
  }
}

Another maybe more straightforward solution would be to use template_preprocess_views_view

function YOUR_THEME_NAME_preprocess_views_view(&$variables) {
  if ($variables['view']->name == 'YOUR_VIEW_NAME') {
    if ($variables['view']->current_display == 'YOUR_DISPLAY_NAME' 
         && !empty($variables['exposed'])) {
      $variables['header'] = '';
    }
  }
}

For the 2nd one, this is a step by step how-to :

  1. locate the folder of your current theme
  2. open the file template.php in that folder with an editor
  3. search in the code if there's already a function which name contains preprocess_views_view
  4. if it does you'll place your code into that function, before the ending bracket {

  5. if not, you'll create your own template_preprocess_views_view function, replacing template by the machine name of your theme, and write it at the bottom of the file :

    function your_theme_name_preprocess_views_view {
      //we'll place the code there
    }
    
  6. open the related view and find its machine name in the url : ie for admin/structure/views/view/frontpage/edit/page view, the view name is frontpage and the display name is page

  7. in the following code, replace view_name and display name by yours :

      if ($variables['view']->name == 'YOUR_VIEW_NAME') {
        if ($variables['view']->current_display == 'YOUR_DISPLAY_NAME' 
             && !empty($variables['exposed'])) {
          $variables['header'] = '';
        }
      }
    
  8. finally, put it all together, placing that code either inside the existing or in the new function, as seen above

  9. Clear caches, or at least theme-registry cache

Hope you'll manage it, not that difficult

  • Did I mention this is Drupal 8? There is no template.php file in the theme. The only file I can find by that name is in the vendor/twig/twig/lib/twig directory. – Kateva Oct 2 '17 at 15:55
  • Nope. Then locate YOURTHEME.theme in your theme dir – Kojo Oct 2 '17 at 16:09

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.