Hi,

I am trying to render the Fivestrar widget programmatically. I have tried this:

$settings=array('content_type'=>'parce', 'content_id'=>$nid,'stars'=> 5, 'autosubmit'=>TRUE, 'allow_clear'=>FALSE, 'required'=> FALSE, 'tag'=>'vote');
print drupal_render(drupal_get_form ('fivestar_custom_widget', $field_rating['0'], $settings));
 

But no succes, i do get the Widget form to print but on submit the votes doesn't register, plus with this way i do not have the vote summary.

Am i missing something ?

Comments

gbernier’s picture

Hi silkAdmin,
I've been having a very frustrating battle tracking down everything to make this wrok as well. I thought I was going insane because no matter what I did the votes were not showing up even though I could see the ratings array was being populated.

I've been playing with the field settings this evening, but what happens it goes to the static HTML display showing the average vote and then you can vote on it, or if you are logged in you'll see just your vote. The one thing I found was setting the text in the settings array was still showing the proper votes, even though it wasn't be displayed in the stars when written as a form

Here's what I've got
$settings = array(
'content_type' => 'node',
'content_id' => $node->nid,
'stars' => 5,
'autosubmit' => TRUE,
'allow_clear' => FALSE,
'text' => 'smart', // options are none, average, smart, user, and dual
'tag' => 'vote',
'style' => 'dual', // options are average, dual, smart, user
'widget' => array('name' => 'default', 'css' => 'default'),
);

print render(drupal_get_form('fivestar_custom_widget', $node->field_resource_rating["und"][0], $settings));

The more detailed outline of the how the settings effect what themes are applied are in "fivestar_expand"

I've got alpha1 installed and I think it's a bug that it will not show the entity's average and still let you vote, as it seems to be one or the other.

If you figure it out let me know

ericduran’s picture

This isn't needed. There's already an issue about this. This is a dupe.

ericduran’s picture

EvaldsUrtans’s picture

In order this to work on node and be able to submit values, you will need additional fields.

Drupal 7:

<?php
$settings = array(
							'content_type' => 'profile2',
							'content_id' => $profileView->pid,
							'entity' => $profileView,
							'stars' => 5,
							'field_name' => 'field_profile_rating',
							'autosubmit' => TRUE,
							'allow_clear' => FALSE,
							'langcode' => 'und',
							'text' => 'none', // options are none, average, smart, user, and dual
							'tag' => 'vote',
							'style' => 'average', // options are average, dual, smart, user
							'widget' => array( 'name' => 'oxygen', 'css' => drupal_get_path('module', 'fivestar') . '/widgets/oxygen/oxygen.css' )
						);
						
						print render(drupal_get_form('fivestar_custom_widget', $profileView->field_profile_rating['und'][0], $settings));
?>
fruitiondenver’s picture

I don't think that is correct. Here's how I was able to programatically insert a fivestar widget anywhere in code:

$settings = array(
	'content_type' => 'node',
	'content_id' => $node->nid,
	'entity' => $node,
	'stars' => 5,
	'field_name' => 'field_star_rating',
	'autosubmit' => TRUE,
	'allow_clear' => FALSE,
	'langcode' => $node->language,
	'text' => 'none', // options are none, average, smart, user, and dual
	'tag' => 'vote',
	'style' => 'average', // options are average, dual, smart, user
	'widget' => array( 'name' => 'oxygen', 'css' => drupal_get_path('module', 'fivestar') . '/widgets/oxygen/oxygen.css' )
	);
global $user;
$fivestar_values = fivestar_get_votes('node', $node->nid, 'vote', $user->uid);
$fivestar_form = render(drupal_get_form('fivestar_custom_widget', $fivestar_values, $settings));
wluisi’s picture

Issue summary: View changes

Thanks for this, very helpful for my use case. You can also use a custom widget in a custom form. First define your widget in your module using hook_fivestar_widget. Then set the widget type in your form element.

function hook_fivestar_widgets() {
  //define custom widget
  $widgets = array(
    '/path/to/file.css' => 'name_of_widget',
  );

  return $widgets;
} 
  
  $form['section_4']['professor']['rating'] = array(
    '#type' => 'fivestar',
    '#input' => TRUE,
    '#stars' => 5,
    '#widget' => array('name' => 'name_of_widget', 'css' => drupal_get_path('module', 'module_name') . '/path/to/file.css'),
    '#title' => t('Overall Rating'),
  );
nDigitHQ’s picture

I was able to reproduce this with taxonomy as well:

$term_data = taxonomy_term_load($data['entity_id']);
$settings = array(
    'content_type' => 'taxonomy',
    'content_id' => $term_data->tid,
    'entity' => $term_data,
    'stars' => 5,
    'field_name' => 'field_expert_rating',
    'autosubmit' => TRUE,
    'allow_clear' => FALSE,
    'langcode' => 'en',
    'text' => 'Expert Rating', 
    'tag' => 'expert rating',
    'style' => 'average', // options are average, dual, smart, user
    'widget' => array( 'name' => 'oxygen', 'css' => drupal_get_path('module', 'fivestar') . '/widgets/oxygen/oxygen.css' )
    );
global $user;
$fivestar_values = fivestar_get_votes('taxonomy', $term_data->tid, 'expert rating', $user->uid);
print render(drupal_get_form('fivestar_custom_widget', $fivestar_values, $settings));

The $data['entity_id'] refers to the taxonomy term TID (or entity) value. Have yet to figure out how to get the label to work though. Digging through the code in fivestar_custom_widget.module to try to find this.