3

Standard behavior: I have a view that has some filters exposed to the user. When the user first visits the page, the exposed filters are empty and the view just shows everything. When the user modifies a filter, the view shows a subset of the results based on their query.

Ideal behavior: When the user first visits the page, and have not performed a search using the exposed filters yet, I'd like the view to show a predetermined set of results... like a group of highlighted* items that I'd want the user to see. Then they perform a search, and the view acts normally.

Is this possible?


*These "highlighted" items would be determined by a field in the node.

0

All you need to do is:

  1. Create a Boolean field to mark highlights (Let's say field_highlights)

  2. Add a views filter for field_highlight with the following detail:

    2.1. Not exposed

    2.2. Operation: Is one of

    2.3. Options: Select both 'On' and 'Off'

  3. Write a bit of magic code which:

    3.1. Checks if the user has not used the exposed filters yet

    3.2. Alters the options for our filter to only show 'On' values

Last part sounds difficult? Actually it's not:

    <?php

    /**
     * Implements hook_views_pre_view().
     *
     * Applys a custom filter if there is no filter applyed by user
     *
     */
    function YOURMODULE_views_pre_view(&$view, &$display_id, &$args) {
        // Make sure we are altering the right view
        if ($view->name == 'YOUR_VIEW_NAME') {
            // Get the user input for exposed filters
            $user_input = $view->get_exposed_input();
            // Check if there is no user input 
            if (empty($user_input)) {
                // The filter name for selecting highlighted items
                $filter_name = 'field_highlighted_value'; // Figure the filter name by looking at the exported views
                // Getting the filter array
                $filter = $view->get_item($view->current_display, 'filter', $filter_name);
                // Setting the value to TRUE
                $filter['value'] = array(1=>'1');
                // Saving the changes to the filter
                $view->set_item($view->current_display, 'filter', $filter_name, $filter);
            }
        }
    }
0

Use jQuery

Or you could create a simple jQuery script that is only loaded on this View which hides your results and shows an EVA or a block or mini panel, or whatever where your highlighted nodes are showing. Give your blocks etc the right classes and then look for when the user actually keys in values, selects an option or ticks a box. Once that happens, with jQuery hide the highlighted nodes and reveal the search results or better, let the results page slide in so that the highlighted nodes stay visible below the search results. I'll try it myself and let you know how it went.

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.