various field types for K2

If you need help let me know through the contact form.

If you use k2fields, please post a rating and a review at the Joomla! Extensions Directory.

Description

I am sure that many that have used K2 to any extent other than putting up blogs have wondered why the extra fields are so limited.

I needed to add various types of extra fields for a classified ad project and found K2 being inflexible in this regard.

The only way I saw I could achieve this w/o hacking the K2 core was by developing a plugin that used existing extra field types to provide the new ones. The way it is made is by developing a syntax for how the name should be written and by developing some JS code for parsing and generating the UI and also mangle the user data, and corresponding manipulation is done on the server side by the plugin. The basic syntax is k2f---options---Name. For more on the syntax and use in the Usage section below.

Features:

  • map using the excellent Googlemaps Plugin found here
  • date/datetime using Joomla's widget (old and ugly but it's Joomla:s and it works)
  • pre-configured validated input fields for alpha, alphanum, number, real, integer, email, url
  • if those are not enough use your own regular expression
  • construct a list of the above field types and you can limit the number of elements in the list
  • a list can be conditioned, meaning one would be able to provide other than the value a description of the value stated (ex. if you have a price field with a list of 3 values and the varying prices have a number of conditions associated, such as, 1 night: 150USD, > 1night but < 1week: 120USD, > 1 week: 100USD)
  • list conditions can be simple text fields or select lists (values of which you will provide)

In addition we have nuked the existing select list so that:

  • users can add values to the list
  • values added can be made available for all users or only to the user adding the value

As usual a picture says more ...

k2fields in action on a demo setup

Note:

  • Be aware that k2fields have yet not been used on any production/live website and is still under development, but have of course been developed and now made available as a fully functional plugin. So be advised and make necessary testing before using it in production.
Demo

A simple demo is found here

Download

 k2fields-0.4.8.zip 

Install/uninstall
  • Do install and activate the plugin as you would with any other Joomla plugin
  • At update (uninstall/install) your defined fields will remain untouched as k2fields manipulate the results only at run time
  • If you are going to use the map type please don't forget to install and activate the Googlemaps plugin located here
Usage

k2fields is constructed by using a Text Field of K2's extra field.

The syntax to put in the Name field of your Text Field is as follows:

k2f---options---Name

where:

  • k2f is a constant
  • options as per the table below and are put up as pair of the option and its value: ex. valid=date and are separated by ":::" (omitting the double-quotation-character)
  • Name is the name of the field just as you would put it in your stock K2 extra field

The "---" is called field separator and can be changed in your k2fields.php file as well as the separator used for the options, namely ":::" (omitting the double-quotation-character).

Available options are as follows and can be combined to create the desired k2fields type (I have not been consistent in describing the various options wherever I've found it self-explanatory):

Option Option values Description
valid
  • map
  • date
  • datetime
  • alpha a-zA-Z -_
  • alphastrict a-zA-Z
  • alphanum allowing a-zA-Z0-9 -_
  • alphanumstrict allowing only a-zA-Z0-9
  • number
  • real
  • integer
  • email
  • url
  • exp=regular-expression

Default: none, all types of input are allowed (just as stock K2 Text Field)

Usage: valid=date gives a date field with the stock Joomla date widget

Note:

  • number is same as real
  • if valid is map then please make also sure to install Googlemaps Plugin. No field configuration options are available as all settings are handled in the plugin. While editing the field the syntax is lat,lon,tooltip,text (comma spearated implying than none of the values should contain comma character)
interval [min,max] The interpretation of interval varies with the valid type as per above:
  • date, datetime = min and max interval can be absolute or relative to current date by prefixing the values with -/+ year
  • alpha, alphastrict, alphanum, alphanumstrict = least and maximum number of characters in user input
If you want to limit single side of the interval:
  • date, datetime, alpha, alphastrict, alphanum, alphanumstrict = give -1 for the one not to be limited
  • number, real and integer = give -Infinity for a non-limited lower and Infinity for non-limited upper side

Usage: for an alphanum k2field interval=[3,25] allows minimum of 3 characters and maximum 25

Usage: for a date k2field interval=[0,+3] renders a widget that starts from current year and goes 3 years forward

Usage: for a number field interval=[30,Infinity] allows a minimum number of 30

required
  • true
  • false
Default: false
list
  • normal
  • conditional

Default: none (no list is allowed)

Description: With conditional list one would be able to provide other than the value a description of the value stated (ex. if you have a price field with a list of 3 values and the varying prices have a number of conditions associated, such as, 1 night: 150USD, > 1night but < 1week: 120USD, > 1 week: 100USD)

listmax A number

Default: 10

Description: Maximum number of list items allowed

conditions val1%%val2%%....valN Description: A list of values that will constitute the drop down list of the condition separated by %%
block
  • true
  • false

Default: false

Description: If true then submission of K2 item is not allowed as long as any of the k2fields contain invalid input

Complete example to get you going (pictures tell so much more than words so pls. click on the pics below to see how you should setup the fields with the options as per the above table and in the following picture you will see the rendered result):

 

k2fields-configuration-saved
How to configure your k2fields
k2fields-rendered-saved
How those configurations are rendered while editing a K2 item

In addition the plugin provides you an extended version of the stock select K2 field with the following options available:

  • editable=true|false, if true users can add values to the list
  • save=true|false, if true values added by users are made available for all users

Thus if you want to make your list editable you would put the following the name field of your extra field of the type Select:

k2f---editable=true:::save=true---Country
This example would give you a select list named with Country and that is editable by the user and all saved values are shared among users, if you don't want values shared just change save=true to save=false.
FAQ

FAQ

Roadmap
  1. pre/post fixes of values
  2. multiselect is yet to be supported
  3. browser compatibility
  4. study how well the plugin plays with other extensions working on extra fields, such as "K2 Extra fields Filter and Search Module for Joomla" and the jextended Finder search module, and make necessary improvements
  5. robust release version
  6. improved js code base
  7. your suggestions...
Change log
* -> Security Fix # -> Bug Fix + -> Addition ^ -> Change - -> Removed ! -> Note

2010-10-19 / v 0.4.7.

# IE / JavaScript error related to the map field preventing it from saving updates is now fixed. Thanks to a certain persistent and helpful user from USA.

2010-10-16 / v 0.4.6.

# Compatibility fix (K2 v2.4.1), thanks for the dedicated effort of 2 users from France and Italy!!!

2010-07-28 / v 0.4.4.

# Fixed a minor security hole.

# From having a date set going to an empty date field was not possible due to JS error, rectified. A wonder no one discovered it so far before a user reported it yesterday.

2010-07-28 / v 0.4.4.

# Quick v2.3 compatibility changes, will work on an improved version soon...

2010-04-07 / v 0.4.3.

# date/datetime intervals handled incorrectly

+ date/datetime based lists are now possible

2010-04-02 / v 0.4.2.

# As the documentation of the feature of extending the existing select lists with user values have been scarce I am sure no one have moved to use it, as I have not either received any request about it  until I saw a forum entry today, and making some rigourous testing I found various bugs that are now rectified in this version. I have also updated the usage instructions.

2010-04-01 / v 0.4.1.

+ Added ability to add tabular data after request from several users, with the following syntax k2f---valid=table:::csep=|:::header=true---Field Name

# Error in google map generating fixed

^ Google map plugin code is now taken from the Google map plugins parameter instead of hard-coded

2010-03-06 / v 0.4

+ Added map using the Googlemaps Plugin

# Error in generating the field UI:s led to corrupted field definitions being saved to db, is now fixed

+ Only front-end demo is setup

^ Documentation is amended

2010-03-04 / v 0.3.2

# List value rendering is changed to be inlined, now playing more in harmony with current default extra field generation

# Fixed the check on whether extra fields was in display or not in list and item view

# Erroneous JS file (k2fields/k2fields.js) loading fixed

# Erroneous calendar icon loading fixed in k2fields.js

2010-03-02 / v 0.3.1

# a missing index.html file hindered installation

^ improved usage documenation

2010-03-01 / v 0.3

+ inspired by http://keetology.com/blog/2009/01/basic-validator added 2 additional validations with strict modes, i.e. alphastrict and alphanumstrict

+ a request in the forum prompted me that users/developers actually need to enforce valid set of extra fields and not allowing submission unless valid extra field exists. This of course will circumvent the normal k2 submission but with only the change of having the validity check, which is now implemented. To achieve this you would simply need to set in your field options block=true

# initialization of the k2fields was incorrect and is now done as correct as it can be done given that I have no way of chaining the ajax based extra fields loading

# various selections accessing selected values were incorrect

2010-02-28 / v 0.2

Initial release

Attribution
  • For the purpose of validation I have used fValidator Copyright (c) 2007 Fabio Zendhi Nagao - http://zend.lojcomm.com.br
License

Free GPL

...you are of course welcome to support the effort in whatever way you see fit