Recent comments

You are here

Error message

  • Warning: count(): Parameter must be an array or an object that implements Countable in theme_table() (line 1878 of /home/lwvu15gt0rgr/public_html/Projects7/includes/
  • Warning: count(): Parameter must be an array or an object that implements Countable in theme_table() (line 1941 of /home/lwvu15gt0rgr/public_html/Projects7/includes/
  • Deprecated function: The each() function is deprecated. This message will be suppressed on further calls in menu_set_active_trail() (line 2385 of /home/lwvu15gt0rgr/public_html/Projects7/includes/

Dynamically including fivestar ratings in drupal content

While using the Fivestar module for Drupal I came across a situation where I wanted to use multiple seperate votes on a single page. Okay this can already easily be done no problem but I didnt want to clump them all together at the bottom of the page nor at the top nor in the middle. I wanted to instead use them inside my content as I was writing the content of the page. This would allow me to get votes on multiple things as they read the article. This was helpful since my article talks about multiple ways of achieving the same outcome and I wanted people to be able to vote on each as they tried them.

So lets get on with how I achieved this.

This tutorial assumes you already have the fivestar module installed if you do not then please read the directions on the Drupal project page for the Fivestar module to install it. This tutorial will not discuss the installation of the module.

First thing you need to do is enable php in your content, This feature comes with drupal when you install it however it is turned off by default for security reasons, but dont worry we will make it secure.

So to enable it first go to Administration and to the Modules area of your site
Here we will use the standard technique to enable this module. Below we can see the module named "PHP filter"

after we check it to enable it we simply go to the bottom of the page and click "Save configuration"

Ok now to make sure its set up in a secure manner
Navigate to Administration and click on "Input Formats" in the menu it should be under Site Configuration

We can now see in the list an entry that says "PHP code" and after the name it should say "No roles may use this format". This is good and exactly what we want since we only want the Administrator, which is you, to be able to use php code in there posts. We can change who can use php code if we click on configure but I highly recomend not letting ANYONE else use php code as it could lead to your site being comprimised!!!

make the votes
Ok for votes we must now make a new content type, sorry but this is the only way I could figure out to make this work. In your administration menu find "Content Types" and click on it, Now choose "Add a new content type" at the bottom.

Fill in the NAME and the TYPE for name I use Vote and for type I will put vote to tell me that it is a vote.Optionally you can add a description

Next find where it has your Fivestar settings for the page, If you dont see it on your page you need to configure Fivestar to be on this content type or on all content type, this is covered in the installation and setup of Fivestar.

Once you have found the Fivestar settings go ahead and enable it if its not enabled and change your settings to your likings. An example is below but your settings will probably be different.

Once done you need to hit the "Save Content Type" button below.

Now go to the "Manage Fields" tab on the top we should now delete all field that we can cause we only want our vote on this page.
We also must add a field of the "fivestar rating" type like I show below

If we navigate back to the Manage fields tab for the Vote content type we can Configure the field at any time with different settings I also suggest moving its position just below the Title. Again make sure you have saved if you change anything.

Create a vote
Ok now for each Vote you will need to make a new content of the type Vote, which is the content type we just created , maybe you named it differently, So Click on "Create Content" and Choose "Vote" or whatever you named it.

Now you should see a page similaur to this

Name it something to do with what type of vote it is for example if it was a vote on cherries ui would name it "cherryvote"
Once you have named it go ahead and Save it

Make sure its only in the menu if you want it to be and make sure you have comments disabled and do not publish it to the front page or sticky it

Now Save it but dont navigate away!!!!!!

Collecting the nid #

Once you have saved the new content it will show you the content we have just made It is very important that you look at the number at the end of the url this is the NID # we must write this down ! in my case the nid # was 32 and the end of the url looked like this

inserting into a page
Ok now that we have a vote setup we can dynamically insert a code into any content to make it display. This even works in the middle on the page or in multiple spots throughout the page and you can make more votes and add many votes throughout a single page.

you need to make sure you choose PHP code under Input format.

Now in your content when you wish to add the five star rating simply add the code

NOTE: Where I have the number 32 you must put the nid number of the vote you want inserted!!!!

You can use this code to add as many votes within your content as you want. The votes will show the correct amounts and will function fully allowing people to use them to vote. Here is some code and the resulting page.

Please feel free to leave any questions or comments below :) Hope this helps some of you with your drupal sites.

Theme by Danetsoft and Danang Probo Sayekti inspired by Maksimer