K2 Extra fields Filter and Search Module for Joomla

latest version: v 0.95 (15-Mar-2011)

Table of Contents

Description

K2 Extra fields Filter & Search is a module for content management system Joomla 1.5.x and content construction kit K2.

I decided to write the code myself for one of my projects as the functionality to filter and search extra fields in K2 is missing and I didn't wanted to use tags to do it. Besides lot of users were asking on the forums for such thing and I felt that I can prepare the ground for other developers and users to discuss other required functions (at the moment module has limited capabilities) and contribute.

In my opinion it's essential to have such functionality when one wants to implement K2 for projects that uses directory listing (catalogs).

Module is compatible with Joom!Fish 2.1.6 (and future versions hopefully).

Module works with Joomla SEF on.

Unfortunately I haven't found any way to provide the functionality I was looking for solely using Joomla module, thus I had to add another listing method 'filter' to the K2 core. You will have to overwrite some of K2 files to use the module.

The drawback are:
  • Remember to install component first, module afterwards
  • When reinstalling K2 component, files mentioned above will be overwritten.
  • Module might not be compatible with other versions of K2 (other then 2.1). Actually I'd prefer if module would become officially included in future releases of K2 component.

If you find module helpful, you may support my work by donating small amount. This is the recognition of work I have done so far and when you do so it doesn't mean that I'll add new functionality for you, fix existing bugs and so on. I hope the K2 community and original developers of the K2 component will help out but I might (or not) do so as well depending on my spare time.

Please post your comments in the forum and let's see what we can achieve together!

What does the module do?

As the name states, you may use it to filter out and list your items by specified extra field.

Module works in 2 modes:
  1. When used in selectable mode (Drop-down selection, Multi select list or Radio buttons) module displays available options, which you may filter out and then list the items.
  2. When used a text input (Text field), you can input keyword to search for in the selected extra fields.

What module cannot do at the moment?

  • You cannot use multiple select in this version (0.9b) to filter out more then one option at a time. Note: fixed in 0.91c
  • You cannot filter/ search more extra fields at the same time (however you can use multiple instances of the module).

License

Code is released under GPL3.

Requirements

  • Joomla 1.5.x
  • K2 component

Installation instructions

  1. backup your site!
  2. install the module just like any other
  3. overwrite K2 core files by provided ones in folder /components/com_k2/

Upgrading

  1. backup your site!
  2. install new version of module over the old one (or manually overwrite files)
  3. overwrite K2 core files by provided ones in folder /components/com_k2/

Uninstalling

Uninstall the module like any other. If you want to restore original K2 component files, overwrite them back with original K2 2.1 component files.

Documentation

Users

Module parameters

K2 Extra fields Filter and Search module parameters

Module Class Suffix
just as you know it
Show legend
Show legend (name of extra field) next to the input field
Only selected Extra fields
Yes: will filter/ search only for selected Extra field
(example: search for real estate in city X; Extra field City: X, Y, Z)
No: will show results for all extra fields
(example: search for guys who speak Spanish; extra field Language 1: English, extra field Language 2: Spanish, extra field Language 3: other)
Restrict to specified category
If you want to filter/ search only in specified category
Category
If previous parameter set to yes, select the category to filter/search in
Results in all languages
Yes: If Joom!Fish installed you'll get results from all languages
Filter type
Type of filter shown in the module.
It doesn't have to be linked to your Extra fields type, but remember that if in K2 it's a Text field, Text area or Link, it doesn't make sense to use anything else the Text field again here.
On the other side, you can use Text field to search in the selectable type (Drop-down selection, Multi-select list, Radio buttons).
Select Extra field
On what you want to perform filter/ search.
other options (Search box size, etc)
just like in the K2 Tools module.
Form send method>
method, how the data will be sent (read more about Form method attribute)
If you are expecting difficulties when sending non-latin characters, try 'Post'
Set Itemid
Just like in Joomla Search module, you may assign custom menu item Itemid the results page

Example

Let's say you want an option on your web to filter out items according to place.

  1. in K2 component create Extra field 'City' as drop-down selection and add options (i.e. Český Těšín, Katowice, ...)
  2. in K2 Filter and Search module choose 'Filter type': drop-down selection (but can be any option)
  3. in 'Select Extra field' choose 'City' and Save!
Simple, isn't it? ;)

Listing options

The module is using the same engine that the K2 Search does; the output is called 'Generic list'.
you can set up the Items count in:

K2> Parameters (right side) > Layout & view options for Search, Tag & Date listings:
Item count for Search, Tag & Date listings: 10

All the settings for generic items apply for the results of the filter module (i. e. Item Introtext, Item generic image width..). Those settings are then passed to the generic template that shows the results.

K2> Parameters (right side) > Layout & view options for Search, Tag & Date listings

View Overrides

If you want to change the way your module looks like in the frontend, copy files from /modules/mod_k2_filter/tmpl/ to __your_template__/html/mod_k2_filter/ folder and play around with them.

Resources: Joomla.org > Understanding Output Overrides in Joomla! 1.5

Developers

Most important files are:

Component overwrite

(folder /components/com_k2/)
note: all added code is specified by marks
// ADDED K2FSM from here ->>
// <<- ADDED K2FSM till here

models/itemlist.php:

  • added method 'filter' at lines 105 and 356
  • added code to destroy items in the results array () which are not meeting filtered (searched for) criteria at line 248 before returning the array.

views/itemlist/view.html.php

  • added handler 'filter' at line 181

views/itemlist/view.feed.php

  • added handler 'filter' at line 91

sef_ext/com_k2.php

  • added handler 'filter'

Module body:

(folder /modules/mod_k2_filter/)

includes/filtermethod.php

At beginning I've tried to use modification of K2 Tools module in search mode, but found it insufficient. Part of that is incorporated the module.

code moved to models/itemlist.php since v0.9

helper.php

includes functions:

  • pull ($field_id, $what)
    pulls out specified information ($what) about Extra fields ($field_id) from the database
  • pullItem ($itemID)
    pulls out Extra fields from specified item ($itemID) from the database
  • extractExtraFields($extraFields)
    extracts Extra Fields info from JSON format

Troubleshooting

Module doesn't work! The results page is blank or it is showing name of the section
You didn't overwrite K2 core files. Check out the Installation instructions.
I cannot see changes made in the module configuraiton
clean your Joomla cache in Joomla menu > Tools > Clean Cache
Module is still not working!
Make sure that module is enabled. Play around with settings: switch some on and off. There might be bugs in the functionality. If you are sure that the things are not working as they should, post a reply on the K2 community forum.
In module parameters I'm getting warning like “Warning: Invalid argument supplied for foreach() in ..\libraries\joomla\html\html\select.php on line 68”
K2 component is not installed

Error: Call to undefined function json_decode() in ../modules/mod_k2_filter/helper.php on line 64
On your server you need PHP version 5.2.0 or greater

Issues, Known bugs & Todo

sh404SEF: not tested yet, I'd like to add modifications for that later on

Download

Please read the Installation instructions or Upgrading first!

Legend:

# -> Bug Fix
+ -> Addition
! -> Change
- -> Removed
! -> Note

v 0.95 for K2 v2.4.1

15-Mar-2010
  • [!] version for K2 v2.4.1
  • [+] sending form using Get or Post method
  • [!] code cleanup

Module: mod_k2v2.4.1_filter_0.95.zip
(MD5: 01eb2aa5122a9432fd8d12542288d82b)

K2 component files to overwrite: com_k2v2.4.1_filter_overwrite_0.95.zip
(MD5: e30242a8b05eecc0e789cbe6d169c854);

v 0.92 for K2 v2.2

28-Feb-2010
  • version for K2 v2.2
  • cleaned up the code
  • I accidently switched files in com_k2v2.2_filter_overwrite_0.92.zip and com_k2v2.2_filter_originalfiles.zip; It has been corrected but I kept the version number.

Module: mod_k2v2.2_filter_0.92.zip
(MD5: c8d9ba942d7c066f634136c026417760)

K2 component files to overwrite: com_k2v2.2_filter_overwrite_0.92.zip
(MD5: b9f269077abf54c62e9ee71ed476fd81)

v 0.91c for K2 v2.1

20-Dec-2009
  • fixed language bug for 'FILTER BY: ' and 'SEARCH FOR: '

Module: mod_k2_filter_0.91c.zip
(MD5: e1c2db9af50fcc9d0ea078dc0f31419b)

K2 component files to overwrite: com_k2_filter_overwrite_0.91c.zip
(MD5: a1e29813b0f5e5bb462a8af8d62934ec)

v 0.91b for K2 v2.1

05-Dec-2009
  • full support for Multi-select type
  • fixed autoredirect when search button disabled and SEF on
  • added Hyperlink view

Module: mod_k2_filter_0.91b.zip
(MD5: 2f01b58cbb70b5c651d17efdae58a174)

K2 component files to overwrite: com_k2_filter_overwrite_0.91b.zip
(MD5: a1e29813b0f5e5bb462a8af8d62934ec)

v 0.9b for K2 v2.1

01-Dec-2009
  • first public version

Module: mod_k2_filter_0.9b.zip
(MD5: a318da089de6a06d8e5ee1b0a1188833)

K2 component files to overwrite: com_k2_filter_overwrite_0.9b.zip
(MD5: 7c7ae81d1725a1a9b69fa00d68685874)

Restoring

original K2 2.4.1 component files: com_k2v2.4.1_filter_originalfiles.zip
(MD5: 73d7b2eab56b3a8c5b5dde3f0ecff6bc)

original K2 2.2 component files: com_k2v2.2_filter_originalfiles.zip
(MD5: b773760585004e7e210561313d5acf33)

original K2 2.1 component files: com_k2_filter_originalfiles.zip
(MD5: 5530886d170f2a5e6d88aa24ac608ca8)