Adding Contributed Fields

One of the great things about CCK is that anyone can build and add custom eld types that can be incorporated into your own content types. Let's take a quick look at an example of this. Head on over to the Drupal site and download both the Voting API and Fivestar modules—the Fivestar module provides a custom eld type that we can use to add rating facilities to content, but it requires the Voting API module to be enabled rst.

Go ahead and extract both modules ensuring to rst enable Voting API and then Fivestar. With that done, head on over to the Content types page under Content management and click edit for the content type that you would like users to be able to rate—for argument's sake, I will go with Endangered species. Click on the Add eld tab, enter a eld name (something like eld_rating), and scroll down the page till you get to the Fivestar Rating option:

Fivestar Ra^ng

Create fi-ld

Advanced Content

Choose an option and click Create field to add it to the content type, and then, ensure that you set the configuration options to something appropriate for you—each option is fairly well explained. Bear in mind that the Node ID field is the most important because it determines exactly, the node that receives the value of the rating. In this case, it's time to dust off your PHP skills and use it to dynamically select the appropriate node ID—admittedly, this isn't ideal but it does serve to illustrate the process for adding a contributed eld, regardless of what it is.

As an addendum, Fivestar actually provides a nice rating widget that can be enabled for a content type — on it's Edit page, in the list of settings categories, towards the bottom of the page:

Comment settings Fni Jt4' r jlinij ,

T-v tH* If'l» c4hli[M, «r-nbli FiVo pHr rflhrtl InlfrM. Th-IF ^rfliny I H,.: ta it* ■■ AfWiffH I if "¡ircd r-liinQ

El Enable Fiyestai rating IUiiitiIht '.l.ir1.: £

5tan.aCeis Girc(£ r^Ting w.dguL

THrif tebingj alien T«v ts if 4 iwng widgri JOIV «Hit rhi?T if* vifnifitf »nwni «r tltit Tppr. P VihJ "IJI

immii'^lelT rigitter a vote fi>r pitce of oanlant

StJir i1k[j!.iy sl^le: Direct rolinq miiliiut ji review:

Display average vote valus


Taxi -ilhplav i-tyliv

You* itfmqi 4 2,3 tJt! tfDttt l<

Both user and a-eraija vote

□ widget tide D Allow user? To undo their votes Tfcofrr display:

□ widget tide D Allow user? To undo their votes Tfcofrr display:

While not shown in the previous screenshot, there is also a Fivestar comment rating widget that can disabled, optional or required. Like the Direct rating widget, there is a preview available on the right-hand side of the page. With your choices made, click Save settings and then go post a piece of content for the type that now has the Fivestar rating enabled.

Chapter 7

For example, a post of the Endangered species type, with Fivestar enabled, looks like this:

Northern Bog Lejnmlng


Tut, fli/li/iOa 6 ■ S2! 2* - Kt6

SuerfcliHit IVmre:

Svnaptomvs boroaUs <3 neat


Add n^w comment

Whenever a user makes a vote, the widget registers it for that specific node. How these votes are tallied, displayed, and so on, can all be configured from the Fivestar and Voting API pages under Site configuration.

Don't be surprised if you click the widget only to find the vote is not registered immediately. It is possible to set the widget to update straight away, but more than likely you can leave this to update with the cron run using the Voting API section in Site configuration. By playing around with Fivestar settings, the rating widget can end up looking like this:

Northern Bog Lemming

TUe, 02/19/2306 - 22:29 —

Scientific NjmtL

syrwffamy? bcfsdlis


Tour ralinfli ; Average: 2 ■(! vote?

In this case, Fivestar actually provides dual functionality, in that it is possible to provide a voting widget for users to rate a given piece of content, as well as a contributed field type that is utilized by posters of content—this could be useful if, for example, a danger level rating should be supplied with each Endangered species post.

Of course, each contributed eld will come with its own entourage of settings and con guration parameters, so be careful to ensure that you are entirely clear on how the contrib functions work before making it available to the site's content posters.

Advanced Content

Was this article helpful?

0 0

Post a comment