forked from vijaycs85/fivestar
-
Notifications
You must be signed in to change notification settings - Fork 1
/
fivestar.module
712 lines (646 loc) · 23.5 KB
/
fivestar.module
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
<?php
/**
* @file
* A simple n-star voting widget, usable in other forms.
*/
include_once dirname(__FILE__) . '/includes/fivestar.field.inc';
use Drupal\Component\Utility\Unicode;
/**
* Implementation of hook_help().
*/
function fivestar_help($path, $arg) {
$output = '';
switch ($path) {
case 'admin/config/content/fivestar':
$output = t('This page is used to configure site-wide features of the Fivestar module.');
break;
}
return $output;
}
/**
* Implements hook_microdata_suggestions().
*/
function fivestar_microdata_suggestions() {
$mappings = array();
// Add the review mapping for Schema.org
$mappings['fields']['fivestar']['schema.org'] = array(
'#itemprop' => array('aggregateRating'),
'#is_item' => TRUE,
'#itemtype' => array('http://schema.org/AggregateRating'),
'average_rating' => array(
'#itemprop' => array('ratingValue'),
),
'rating_count' => array(
'#itemprop' => array('ratingCount'),
),
);
return $mappings;
}
/**
* Implementation of hook_theme().
*/
function fivestar_theme() {
return array(
// Fivestar theme functions.
'fivestar' => array(
'function' => 'theme_fivestar',
'render element' => 'element',
'file' => 'includes/fivestar.theme.inc',
),
'fivestar_select' => array(
'function' => 'theme_fivestar_select',
'render element' => 'element',
'file' => 'includes/fivestar.theme.inc',
),
'fivestar_static' => array(
'function' => 'theme_fivestar_static',
'variables' => array('rating' => NULL, 'stars' => 5, 'tag' => 'vote', 'widget' => array('name' => 'default', 'css' => '')),
'file' => 'includes/fivestar.theme.inc',
),
'fivestar_static_element' => array(
'function' => 'theme_fivestar_static_element',
'variables' => array('star_display' => NULL, 'title' => NULL, 'description' => NULL),
'file' => 'includes/fivestar.theme.inc',
),
'fivestar_summary' => array(
'function' => 'theme_fivestar_summary',
'variables' => array('user_rating' => NULL, 'average_rating' => NULL, 'votes' => 0, 'stars' => 5, 'microdata' => array()),
'file' => 'includes/fivestar.theme.inc',
),
// fivestar.admin.inc.
'fivestar_preview' => array(
'function' => 'theme_fivestar_preview',
'variables' => array('style' => NULL, 'text' => NULL, 'stars' => NULL, 'unvote' => NULL, 'title' => NULL),
'file' => 'includes/fivestar.theme.inc',
),
'fivestar_preview_widget' => array(
'function' => 'theme_fivestar_preview_widget',
'variables' => array('css' => NULL, 'name' => NULL),
'file' => 'includes/fivestar.theme.inc',
),
'fivestar_preview_wrapper' => array(
'function' => 'theme_fivestar_preview_wrapper',
'variables' => array('content' => NULL, 'type' => 'direct'),
'file' => 'includes/fivestar.theme.inc',
),
// 'fivestar_settings_form' => array(
// 'function' => 'theme_fivestar_settings_form',
// 'render element' => 'form',
// 'file' => 'includes/fivestar.theme.inc',
// ),
// 'fivestar_color_form' => array(
// 'function' => 'theme_fivestar_color_form',
// 'render element' => 'form',
// 'file' => 'includes/fivestar.theme.inc',
// ),
'fivestar_formatter_default' => array(
'function' => 'theme_fivestar_formatter_default',
'render element' => 'element',
'file' => 'includes/fivestar.theme.inc',
),
'fivestar_formatter_rating' => array(
'function' => 'theme_fivestar_formatter_rating',
'render element' => 'element',
'file' => 'includes/fivestar.theme.inc',
),
'fivestar_formatter_percentage' => array(
'function' => 'theme_fivestar_formatter_percentage',
'render element' => 'element',
'file' => 'includes/fivestar.theme.inc',
),
);
}
function _fivestar_variables() {
return array('fivestar', 'fivestar_unvote', 'fivestar_style', 'fivestar_stars', 'fivestar_comment', 'fivestar_position', 'fivestar_position_teaser', 'fivestar_labels_enable', 'fivestar_labels', 'fivestar_text', 'fivestar_title', 'fivestar_feedback');
}
/**
* Internal function to handle vote casting, flood control, XSS, IP based
* voting, etc...
*/
function _fivestar_cast_vote($entity_type, $id, $value, $tag = NULL, $uid = NULL, $skip_validation = FALSE, $vote_source = NULL) {
global $user;
$tag = empty($tag) ? 'vote' : $tag;
$uid = isset($uid) ? $uid : $user->uid;
// Bail out if the user's trying to vote on an invalid object.
if (!$skip_validation && !fivestar_validate_target($entity_type, $id, $tag, $uid)) {
return array();
}
// Sanity-check the incoming values.
if (is_numeric($id) && is_numeric($value)) {
if ($value > 100) {
$value = 100;
}
// Get the user's current vote.
$criteria = array('entity_type' => $entity_type, 'entity_id' => $id, 'tag' => $tag, 'uid' => $uid);
// Get the unique identifier for the user (IP Address if anonymous).
if ($vote_source != NULL) {
$user_criteria = array('vote_source' => $vote_source);
$limit = 1;
}
else {
$user_criteria = votingapi_current_user_identifier();
$limit = 0;
}
$user_votes = votingapi_select_votes($criteria + $user_criteria, $limit);
if ($value == 0) {
votingapi_delete_votes($user_votes);
}
else {
$votes = $criteria += array('value' => $value);
votingapi_set_votes($votes);
}
// Moving the calculationg after saving/deleting the vote but before getting the votes.
votingapi_recalculate_results($entity_type, $id);
return fivestar_get_votes($entity_type, $id, $tag, $uid);
}
}
/**
* Utility function to retrieve VotingAPI votes.
*
* Note that this should not be used for general vote retrieval, instead the
* VotingAPI function votingapi_select_results() should be used, which is more
* efficient when retrieving multiple votes.
*
* @param $entity_type
* The Entity type for which to retrieve votes.
* @param $id
* The ID for which to retrieve votes.
* @param $tag
* The VotingAPI tag for which to retrieve votes.
* @param $uid
* Optional. A user ID for which to retrieve votes.
* @return
* An array of the following keys:
* - average: An array of VotingAPI results, including the average 'value'.
* - count: An array of VotingAPI results, including the count 'value'.
* - user: An array of VotingAPI results, including the user's vote 'value'.
*/
function fivestar_get_votes_multiple($entity_type, $ids, $tag = 'vote', $uid = NULL) {
global $user;
if (!isset($uid)) {
$uid = $user->uid;
}
$criteria = array(
'entity_type' => $entity_type,
'entity_id' => $ids,
'value_type' => 'percent',
'tag' => $tag,
);
$votes = array();
// Initialize defaults.
foreach($ids as $id) {
$votes[$entity_type][$id] = array(
'average' => array(),
'count' => array(),
'user' => array(),
);
}
$results = votingapi_select_results($criteria);
$user_votes = array();
if(!empty($results)) {
foreach(votingapi_select_votes($criteria += array('uid' => $uid)) as $uv) {
$user_votes[$uv['entity_type']][$uv['entity_id']] = $uv;
}
}
foreach ($results as $result) {
if ($result['function'] == 'average') {
$votes[$result['entity_type']][$result['entity_id']]['average'] = $result;
}
if ($result['function'] == 'count') {
$votes[$result['entity_type']][$result['entity_id']]['count'] = $result;
}
if ($uid) {
if (!empty($user_votes[$result['entity_type']][$result['entity_id']])) {
$votes[$result['entity_type']][$result['entity_id']]['user'] = $user_votes[$result['entity_type']][$result['entity_id']];
$votes[$result['entity_type']][$result['entity_id']]['user']['function'] = 'user';
}
}
else {
// If the user is anonymous, we never bother loading their existing votes.
// Not only would it be hit-or-miss, it would break page caching. Safer to always
// show the 'fresh' version to anon users.
$votes[$result['entity_type']][$result['entity_id']]['user'] = array('value' => 0);
}
}
return $votes;
}
function fivestar_get_votes($entity_type, $id, $tag = 'vote', $uid = NULL) {
$multiple = fivestar_get_votes_multiple($entity_type, array($id), $tag, $uid);
return $multiple[$entity_type][$id];
}
/**
* Check that an item being voted upon is a valid vote.
*
* @param $entity_type
* Type of target.
* @param $id
* Identifier within the type.
* @param $tag
* The VotingAPI tag string.
* @param $uid
* The user trying to cast the vote.
*
* @return boolean
*/
function fivestar_validate_target($entity_type, $id, $tag, $uid = NULL) {
if (!isset($uid)) {
$uid = $GLOBALS['user']->uid;
}
$access = module_invoke_all('fivestar_access', $entity_type, $id, $tag, $uid);
foreach ($access as $result) {
if ($result == TRUE) {
return TRUE;
}
if ($result === FALSE) {
return FALSE;
}
}
}
/**
* Implementation of hook_fivestar_access().
*
* This hook is called before every vote is cast through Fivestar. It allows
* modules to allow voting on any type of entity, such as nodes, users, or
* comments.
*
* @param $entity_type
* Type entity.
* @param $id
* Identifier within the type.
* @param $tag
* The VotingAPI tag string.
* @param $uid
* The user ID trying to cast the vote.
*
* @return boolean or NULL
* Returns TRUE if voting is supported on this object.
* Returns NULL if voting is not supported on this object by this module.
* If needing to absolutely deny all voting on this object, regardless
* of permissions defined in other modules, return FALSE. Note if all
* modules return NULL, stating no preference, then access will be denied.
*/
function fivestar_fivestar_access($entity_type, $id, $tag, $uid) {
// Check to see if there is a field instance on this entity.
$fields = field_read_fields(array('module' => 'fivestar'));
foreach ($fields as $field) {
if ($field['settings']['axis'] == $tag) {
$params = array(
'entity_type' => $entity_type,
'field_name' => $field['field_name'],
);
$instance = field_read_instances($params);
if (!empty($instance)) {
return TRUE;
}
}
}
}
/**
* Implementation of hook_form_comment_form_alter().
*
* This hook removes the parent node, together with the fivestar field, from
* the comment preview page. If this is left in, when the user presses the
* "Save" button after the preview page has been displayed, the fivestar widget
* gets the input rather than the comment; the user's input is lost. Based on a
* suggestion by ChristianAdamski in issue 1289832-3.
*/
function fivestar_form_comment_form_alter(&$form, &$form_state, $form_id) {
$fivestar_field_keys = array();
if (isset($form['comment_output_below'])) {
foreach($form['comment_output_below'] as $key => $value) {
if (is_array($value) && !empty($value['#field_type']) && $value['#field_type'] == 'fivestar') {
$fivestar_field_keys[] = $key;
}
}
}
if ($fivestar_field_keys) {
foreach ($fivestar_field_keys as $key) {
unset($form['comment_output_below'][$key]);
}
}
}
/**
* Implements hook_form_FORM_ID_alter().
*/
function fivestar_form_field_ui_field_edit_form_alter(&$form, $form_state) {
/** @var Drupal\field\FieldStorageConfigInterface $field */
$field = $form['#field'];
if ($field->getType() == 'fivestar') {
// Multiple values is not supported with Fivestar.
$form['field']['cardinality']['#access'] = FALSE;
$form['field']['cardinality']['#value'] = 1;
// Setting "default value" here is confusing and for all practical purposes
// with existing widgets provided by fivestar (and anything else available
// in contrib) meaningless.
$form['instance']['default_value_widget']['#access'] = FALSE;
}
}
/**
* Implementation of hook_fivestar_widgets().
*
* This hook allows other modules to create additional custom widgets for
* the fivestar module.
*
* @return array
* An array of key => value pairs suitable for inclusion as the #options in a
* select or radios form element. Each key must be the location of a css
* file for a fivestar widget. Each value should be the name of the widget.
*/
function fivestar_fivestar_widgets() {
$widgets = &drupal_static(__FUNCTION__);
if (!isset($widgets)) {
$cache = \Drupal::cache()->get(__FUNCTION__);
if ($cache) {
$widgets = $cache->data;
}
}
if (!isset($widgets)) {
$widgets_directory = drupal_get_path('module', 'fivestar') . '/widgets';
$files = file_scan_directory($widgets_directory, '/\.css$/');
foreach ($files as $file) {
if (strpos($file->filename, '-rtl.css') === FALSE) {
$widgets[$file->uri] = Unicode::ucfirst(str_replace('-color', '', $file->name));
}
}
\Drupal::cache()->set(__FUNCTION__, $widgets);
}
return $widgets;
}
/**
* Form builder; Build a custom Fivestar rating widget with arbitrary settings.
*
* This function is usually not called directly, instead call
* drupal_get_form('fivestar_custom_widget', $values, $settings) when wanting
* to display a widget.
*
* @param $form_state
* The form state provided by Form API.
* @param $values
* An array of current vote values from 0 to 100, with the following array
* keys:
* - user: The user's current vote.
* - average: The average vote value.
* - count: The total number of votes so far on this content.
* @param $settings
* An array of settings that configure the properties of the rating widget.
* Available keys for the settings include:
* - content_type: The type of content which will be voted upon.
* - content_id: The content ID which will be voted upon.
* - stars: The number of stars to display in this widget, from 2 to 10.
* Defaults to 5.
* - autosubmit: Whether the form should be submitted upon star selection.
* Defaults to TRUE.
* - allow_clear: Whether or not to show the "Clear current vote" icon when
* showing the widget. Defaults to FALSE.
* - required: Whether this field is required before the form can be
* submitted. Defaults to FALSE.
* - tag: The VotingAPI tag that will be registered by this widget. Defaults
* to "vote".
*/
function fivestar_custom_widget($form, &$form_state, $values, $settings) {
$form = array(
'#attributes' => array(
'class' => array('fivestar-widget')
),
);
$form['#submit'][] = 'fivestar_form_submit';
$form_state['settings'] = $settings;
$form['vote'] = array(
'#type' => 'fivestar',
'#stars' => $settings['stars'],
'#auto_submit' => isset($settings['autosubmit']) ? $settings['autosubmit'] : TRUE,
'#allow_clear' => $settings['allow_clear'],
'#allow_revote' => $settings['allow_revote'],
'#allow_ownvote' => $settings['allow_ownvote'],
'#required' => isset($settings['required']) ? $settings['required'] : FALSE,
'#widget' => isset($settings['widget']) ? $settings['widget'] : array('name' => 'default', 'css' => 'default'),
'#values' => $values,
'#settings' => $settings,
'#description' => $settings['description'],
);
$form['fivestar_submit'] = array(
'#type' => 'submit',
'#value' => t('Rate'),
'#attributes' => array('class' => array('fivestar-submit')),
);
return $form;
}
/**
* Submit handler for the above form (non-javascript version).
*/
function fivestar_form_submit($form, &$form_state) {
// Cast the vote.
_fivestar_cast_vote($form_state['settings']['content_type'], $form_state['settings']['content_id'], $form_state['values']['vote'], $form_state['settings']['tag']);
// Set a message that the vote was received.
if ($form_state['values']['vote'] === '0') {
drupal_set_message(t('Your vote has been cleared.'));
}
elseif (is_numeric($form_state['values']['vote'])) {
drupal_set_message(t('Thank you for your vote.'));
}
}
/**
* AJAX submit handler for fivestar_custom_widget
*/
function fivestar_ajax_submit($form, $form_state) {
if (!empty($form_state['settings']['content_id'])) {
$entity = entity_load($form_state['settings']['entity_type'], array($form_state['settings']['entity_id']));
$entity = reset($entity);
_fivestar_update_field_value($form_state['settings']['content_type'], $entity, $form_state['settings']['field_name'], $form_state['settings']['langcode'], $form_state['values']['vote']);
$votes = _fivestar_cast_vote($form_state['settings']['content_type'], $form_state['settings']['content_id'], $form_state['values']['vote'], $form_state['settings']['tag']);
}
$values = array();
$values['user'] = isset($votes['user']['value']) ? $votes['user']['value'] : 0;
$values['average'] = isset($votes['average']['value']) ? $votes['average']['value'] : 0;
$values['count'] = isset($votes['count']['value']) ? $votes['count']['value'] : 0;
// We need to process the 'fivestar' element with the new values.
$form['vote']['#values'] = $values;
// Also need to pass on form_state and the complete form, just like form_builder() does.
$new_element = fivestar_expand($form['vote'], $form_state, $form_state['complete form']);
// Update the description. Since it doesn't account of our cast vote above.
// TODO: Look into all this, I honestly don't like it and it's a bit weird.
$form['vote']['vote']['#description'] = isset($new_element['vote']['#description']) ? $new_element['vote']['#description'] : '';
return array(
'#type' => 'ajax',
'#commands' => array(
array(
'command' => 'fivestarUpdate',
'data' => drupal_render($form['vote']),
),
),
);
}
/**
* An #element_validate function for "fivestar" elements.
*/
function fivestar_validate($element, &$form_state) {
if ($element['#required'] && (empty($element['#value']) || $element['#value'] == '-')) {
form_error($element, t('@name field is required.', array('@name' => $element['#title'])));
}
}
/**
* Implementation of hook_votingapi_metadata_alter().
*/
function fivestar_votingapi_metadata_alter(&$data) {
foreach (fivestar_get_tags() as $tag) {
// Add several custom tags that are being used by fivestar.
$data['tags'][$tag] = array(
'name' => t($tag),
'description' => t('@tag used by fivestar.', array('@tag' => $tag)),
'module' => 'fivestar',
);
}
}
function fivestar_get_tags() {
$tags_txt = \Drupal::config('fivestar.settings')->get('tags', 'vote');
$tags_exploded = explode(',', $tags_txt);
$tags = array();
$got_vote = FALSE;
foreach ($tags_exploded as $tag) {
$tag_trimmed = trim($tag);
if ($tag_trimmed) {
$tags[$tag_trimmed] = $tag_trimmed;
if ($tag_trimmed == 'vote') {
$got_vote = TRUE;
}
}
}
if (!$got_vote) {
$tags['vote'] = 'vote';
}
return $tags;
}
/**
* Implements hook_fivestar_target_info().
*/
function fivestar_fivestar_target_info($field, $instance) {
$entity_type = $instance['entity_type'];
$bundle = $instance['bundle'];
$options = array(
'none' => array(
'title' => t('None'),
),
);
$module_handler = \Drupal::moduleHandler();
// Add node_referrence support.
if ($module_handler->moduleExists('node_reference')) {
$field_names = array_keys(field_read_fields(array('module' => 'node_reference')));
if (!empty($field_names)) {
$field_instances = field_read_instances(array('entity_type' => $entity_type, 'bundle' => $bundle, 'field_name' => $field_names));
if (!empty($field_instances)) {
foreach ($field_instances as $field_instance) {
$options[$field_instance['field_name']] = array(
'title' => t('Node reference: @field', array('@field' => $field_instance['field_name'])),
'callback' => '_fivestar_target_node_reference'
);
}
}
}
}
// Add entityreference support.
if ($module_handler->moduleExists('entityreference')) {
$field_names = array_keys(field_read_fields(array('module' => 'entityreference')));
if (!empty($field_names)) {
$field_instances = field_read_instances(array('entity_type' => $entity_type, 'bundle' => $bundle, 'field_name' => $field_names));
if (!empty($field_instances)) {
foreach ($field_instances as $field_instance) {
$options[$field_instance['field_name']] = array(
'title' => t('Entity reference: @field', array('@field' => $field_instance['field_name'])),
'callback' => '_fivestar_target_entityreference'
);
}
}
}
}
// Add user reference support.
if ($module_handler->moduleExists('user_reference')) {
$field_names = array_keys(field_read_fields(array('module' => 'user_reference')));
if (!empty($field_names)) {
$field_instances = field_read_instances(array('entity_type' => $entity_type, 'bundle' => $bundle, 'field_name' => $field_names));
if (!empty($field_instances)) {
foreach ($field_instances as $field_instance) {
$options[$field_instance['field_name']] = array(
'title' => t('User reference: @field', array('@field' => $field_instance['field_name'])),
'callback' => '_fivestar_target_user_reference'
);
}
}
}
}
// Add comment module support.
if ($instance['entity_type'] == 'comment') {
$options['parent_node'] = array(
'title' => t('Parent Node'),
'callback' => '_fivestar_target_comment_parent_node'
);
}
return $options;
}
/**
*
* @return array
* array('entity_type', 'entity_id').
*/
function _fivestar_target_node_reference($entity, $field, $instance, $langcode) {
$target = array();
$node_reference = $instance['settings']['target'];
if (isset($entity->{$node_reference}[$langcode][0]) && is_numeric($entity->{$node_reference}[$langcode][0]['nid'])) {
$target['entity_id'] = $entity->{$node_reference}[$langcode][0]['nid'];
$target['entity_type'] = 'node';
}
return $target;
}
/**
* @return (array) array('entity_type', 'entity_id')
*/
function _fivestar_target_entityreference($entity, $field, $instance, $langcode) {
$target = array();
$entityreference = $instance['settings']['target'];
// Retrieve entity settings for the referenced field.
$field_info = field_info_field($entityreference);
if (isset($entity->{$entityreference}[$langcode][0]) && isset($entity->{$entityreference}[$langcode][0]['target_id']) && is_numeric($entity->{$entityreference}[$langcode][0]['target_id'])) {
$target['entity_id'] = $entity->{$entityreference}[$langcode][0]['target_id'];
$target['entity_type'] = $field_info['settings']['target_type'];
}
return $target;
}
/**
*
* @return array
* array('entity_type', 'entity_id').
*/
function _fivestar_target_user_reference($entity, $field, $instance, $langcode) {
$target = array();
$user_reference = $instance['settings']['target'];
if (isset($entity->{$user_reference}[$langcode][0]) && is_numeric($entity->{$user_reference}[$langcode][0]['uid'])) {
$target['entity_id'] = $entity->{$user_reference}[$langcode][0]['uid'];
$target['entity_type'] = 'user';
}
return $target;
}
function _fivestar_target_comment_parent_node($entity, $field, $instance, $langcode) {
return array(
'entity_id' => $entity->nid,
'entity_type' => 'node',
);
}
/**
* Implements hook_library_info_build().
*/
function fivestar_library_info_build() {
$libraries = [];
$module_path = drupal_get_path('module', 'fivestar');
foreach (\Drupal::moduleHandler()->invokeAll('fivestar_widgets') as $path => $widget) {
// @todo ok, that's wierd.
$path = str_replace($module_path . '/', '', $path);
$name = Unicode::strtolower($widget);
$libraries["fivestar.{$name}"] = [
'css' => [
'component' => [
$path => [],
],
],
];
}
return $libraries;
}