5

I followed the tutorial about How to Create Custom Search Pages Using Just Views in Drupal 7.

I have selected the blocks option instead of page option in views as I want to add it as a block in the organic group homepage.

How do I restrict the search using relationship and context filtering in the Views settings so each logged in user can only search for published content of their group only (NOT SITE-WIDE)?

EDIT: I have already tried the following:

Contextual filters > (OG membership from node) OG membership: Group ID Relationships > OG membership: OG membership from Node

(Which I got the idea from the default og_content view that comes with OG Groups!)

Can this be done with hooks?

  • As per your (3rd) comment like "I personally think OG is ..." below the answer: are you aware that there is a possible alternative for OG these days? Not sure (yet) if it would help to actually build such Views based search. Let me know if you want me to post an answer about that alternative. – Pierre.Vriens Feb 16 '16 at 9:21
  • @Pierre.Vriens I know there is a "Group" module (drupal.org/project/group) as well... I am eager to learn as much as possible on different alternatives so please do share your answer! – testing1 Feb 17 '16 at 15:45
3
+25

Group module as possible OG alternative

As per your (3rd) comment like "I personally think OG is ..." below the previous answer: be aware that the Group module is a possible alternative for Organic groups for Drupal 7 these days (and soon Drupal 8 also ...).

Refer to my answer to "How to create basic Organic Groups functionality with flags and references?" for way more details about the Group module.

Specific to this question here, some more details about the Group module, as mentioned on "Group - an alternative to Organic Groups":

Permission system

Group permissions are defined by the site builder and are then passed onto every group instance. This is done by configuring the “group type”, which is comparable to a node type for nodes. So you can configure a set of permissions for different types of groups, yet don’t have to worry about group members being able to fiddle with them.

Group roles

In order to make the above work, the Group module defines a concept called group roles. These behave exactly like user roles would globally, but can be defined on the group type level. Group provides three special roles to make your life easier too: Anonymous (no account on the site), Outsider (account on the site but not a member of the group) and Member (account on the site and a member of the group).

So with the above in mind, if you're willing to consider the Group module as a possible alternative, restricting the search results as you are trying to do seems much easier (if not automatically).

Search configuration module for more configuration options

Another approach (which you might possibly want to combine with using the Group module) is to use the Search configuration module. Some details from its project page:

This module has five main functions.

  1. Alter the appearance of the core node search form.
  2. Group content types for more meaningful searching.
  3. Restrict search results by the content type. This is a role based restriction.
  4. Restrict search results from showing individual items.
  5. Alter the pager limit (aka number search item results per page).

Its project page contains many more details about what you can do with this module.

3

Editing my answer here as there seems to be a glitch in Views. Even with no contextual filters or relationships, just filtering by groups audience and exposed search doesn't work. The SQL query has both values but I don't know enough about sql to debug it.

  1. Contextual filters > (OG membership from node) OG membership: Group ID
  2. Relationships > OG membership: OG membership from Node

Instead, here's a hacky way of getting it to work by not using the search module.

  1. Do the same as above to limit results by group.
  2. If you only want to search by title for example, just add an exposed filter for the title and select "contains".
  3. If you want to search by more fields which is likely, add the fields you want to search by (and maybe hide them) eg. Node title / Body. Then create a Global: Combine fields filter (exposed) and select the above fields with "contains".

That will let you search whichever fields but just not as nicely as with the search module.

If anyone else has a solution to the exposed search problem, post please.

  • I already tried that before (see my updated question) sorry, should have mentioned... – testing1 Feb 4 '16 at 13:39
  • I've edited my answer.. Pretty strange overall but I got a hacky way of doing it. – Niall Murphy Feb 4 '16 at 15:46
  • Okay I will try that - how do I do the second step? do I need to add the title to the fields section? (I will probably just need those searchable as that will lead to the content anyway!) – testing1 Feb 4 '16 at 17:21
  • If you're only searching the title, you just need to add an exposed "contains" filter for title. Whether or not you add or display it as a field shouldn't matter. – Niall Murphy Feb 5 '16 at 4:35
  • I have added title to the 'filter criteria' as per your suggestion (i.imgur.com/peIEXoJ.png) but no results appear... - I personally think OG is a really badly made module...! – testing1 Feb 9 '16 at 12:26

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.