Skip to content

Instantly share code, notes, and snippets.

@mglaman
Created October 17, 2016 17:05
Show Gist options
  • Save mglaman/3c76f65c8efcd4dcdd3b45dfbefd8518 to your computer and use it in GitHub Desktop.
Save mglaman/3c76f65c8efcd4dcdd3b45dfbefd8518 to your computer and use it in GitHub Desktop.
<?php
public function postSave(EntityStorageInterface $storage, $update = TRUE) {
$rating = $this->get('rating')->first()->value;
/** @var \Drupal\forkdin_restaurant\Entity\RestaurantMenuItem $menu_item */
$menu_item = $this->get('menu_item_id')->entity;
$vote = Vote::create([
'type' => 'rating',
'entity_type' => 'checkin',
'entity_id' => $menu_item->id(),
'value' => $rating,
'value_type' => 'points',
'user_id' => $this->getOwner(),
]);
$vote->save();
/** @var \Drupal\votingapi\VoteResultFunctionManager $result_function */
$result_function = \Drupal::service('plugin.manager.votingapi.resultfunction');
$results = $result_function->getResults('checkin', $menu_item->id());
$menu_item->setRating($results['rating']['vote_average']);
$menu_item->save();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment