2

I have to develop a drupal site that allows authenticated users to create personal profiles with data such as interests, personal details, educations, etc.

I created a profile2 type with all needed fields and organize the view of the data with views3. I want to create a filter (search form) that allows other users and visitors to search the name (first- and last name in one "name" single textfield, an OR'ed query), the city, a proximity field as an addt. option if the zip code is entered (by probably using the location module) and also an additional field that searches thru various other profile fields in one single textfield (all fields OR'ed).

For that reason I thought it might be a good idea to create a views custom filter to build such search form and sql queries. I can not find any way using the regular filter criteria such the way I want it described above. For example, I do not get a single textfield for the OR'ed name query, etc.

The code skeleton I have up to now does not generate any filter entry in the criteria dialog at all. The filter modue is enabled in the modules section. I have the strong feeling that the "hook_views_data()" is not defined the right way for "profile2 fields". I been looking at various filter examples, but found nothing profile2 data related.

Here is what I have up to now .. like said, just a skeleton I try to work out step by step.

xconsultantview.info

; $Id$
name = XConsultantView
description = Custom consultant filter
version = 7.x-1.x-dev
core = 7.x
dependencies[] = views
files[] = xconsultantview_handler_filter_profile.inc

xconsultantview.module

<?php

/**
 *
 */
function xconsultantview_views_api() {
    return array(
        'api' => 3,
        'path' => drupal_get_path('module', 'xconsultantview')
    );
}
?>

xconsultantview.views.inc

<?php

function xconsultantview_views_data() {
    $data = array();

    // consultant filter
    $data['node']['consultant_search_filter'] = array(
        'group' => t('Custom'),
        'title' => t('Eliteberater specific consultant filter'),
        'help' => t('Filter consultants on specific fields'),
        'filter' => array(
            'handler' => 'xconsultantview_handler_filter_view'
        ),
    );
?>

xconsultantview_handler_filter_view.inc

<?php
class xconsultantview_handler_filter_view extends views_handler_filter {

    function options_form(&$form, &$form_state) {
        parent::options_form($form, $form_state);
        // ...
    }

    function value_form(&$form, &$form_state) {
                // not quiet sure if this is needed here
        $form['xconsultantview']  = array(
            '#type' => 'textfield',
            '#title' => t('Term Name'),
            '#default_value' => NULL
        );

        return $form;
    }

    function expose_form(&$form, &$form_state) {
                // seems like here I should define my complete form 
                // ...
    }

    function query() {
        $this->ensure_my_table();
                // ... sql query ...
    }

};
?>

As I already found out the hook_views_handlers() is no more needed in D7, because the include files are defined via the files[] in the module info file.

Can anybody shed some light on my question, if such custom filter in general is possible at all or if there are other more simpler solutions. My main problem of understanding lies in the hook_views_data() fct and the profile2 fields or if I have to define a subset of the profile2 fields, at least the ones that I need to filter.

Is there any example with an custom filter filtering profile2 fields in a view?

It is my first drupal project, so my in-depth knowledge at this time is quiet limited...

Thanks so much in advance for your help!

2
  • Did you look into Views exposed filters? I have a similar site that allows proximity search of users, etc. using only exposed filters and I didn't have to write any custom code. May 5, 2012 at 5:54
  • @Patrick Kenny: Yes, before I started coding above filter skeleton I played with the exposed filters. As stated, for example I can't find a way to create an single input field that allows to query multiple OR'ed values. Instead it exposes me one field for first- and one for the lastname. I have another query that would be an OR'ed query on about 10 fields. Addt. I will need to integrate that proximity search with a Postal code. So for all that I would like to create a module. But for now I am scratching my head how to get a start on that. May 5, 2012 at 6:48

1 Answer 1

1

Use the Search API module with Views to accomplish this functionality. Here is an example.

4
  • I was reading about the Search API Module (envisioninteractive.com/drupal/…). But that would be more a fulltext search solution and I will not be able to access profile2 fields directly. This drupal thread seems to describe a similar problem I just found an hour ago: drupal.org/node/1369544 May 5, 2012 at 14:57
  • I don't believe that discussion is correct. Profile2 requires the Entity API which exposes its fields to Views and the Entity API also enables Profile2 to describe itself to the Search API. Using the Seach API there is a GUI interface that allows the administrator to add fields to a facet search widget and add fields through relationships. These fields' type is defined by the admin and cast as string, integer, boolean, date, string, fulltext, or URI before being pushed to the Search API index View. I only had a problem with the Address Field module not describing itself properly to the Entity.
    – Adam S
    May 5, 2012 at 22:37
  • Ok, it sounds somehow promising. But how would I be able to create, lets say, an advanced search form where I have multiple input query fields, such as name query (would be an OR'ed search in first- and last name), city and postal code (postal code would be combined with a proximity search) and a fulltext search field on a few profile other fields? All examples I see with the search API is one single fulltext search field and these facets/category list queries. How to solve is quiet unclear to me but I need to provide an somehow advanced search form with multiple query input fields. May 6, 2012 at 7:47
  • Maybe I would be able to use the SearchApiQueryInterface using an instance of that? That just came thru my mind while digging for more info. May 6, 2012 at 7:57

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.