View source
<?php
function like_and_dislike_init() {
$module_path = drupal_get_path('module', 'like_and_dislike');
drupal_add_js($module_path . "/assets/javascripts/LikeDislikeService.js");
drupal_add_js($module_path . "/assets/javascripts/like_and_dislike.js");
drupal_add_js("var like_and_dislike_path = '" . $module_path . "';", "inline");
}
function like_and_dislike_permission() {
$perms = array();
$perms['manage like dislike'] = array(
'title' => t('Manage Like/DisLike options'),
'description' => t('Manage administrative options of the like/dislike module.'),
'restrict access' => TRUE,
);
$entity_types = entity_get_info();
foreach ($entity_types as $type_name => $type_info) {
if (!in_array($type_name, Drupal\like_and_dislike\Model\Entity::$available_entity_types)) {
continue;
}
switch ($type_name) {
case 'node':
foreach ($type_info['bundles'] as $bundle_name => $bundle) {
$perms['like/dislike any ' . $bundle_name . ' nodes'] = array(
'title' => t('Add like/dislike to any %type', array(
'%type' => $bundle['label'],
)),
'description' => t('Allow users to Like/Dislike nodes from %type content type.', array(
'%type' => $bundle['label'],
)),
'restrict access' => TRUE,
);
$perms['view likes/dislikes from every ' . $bundle_name . ' nodes'] = array(
'title' => t('View likes/dislikes from every %type', array(
'%type' => $bundle['label'],
)),
'description' => t('Allow users to view Likes/Dislikes from entities of type %type.', array(
'%type' => $bundle['label'],
)),
'restrict access' => TRUE,
);
}
break;
case 'comment':
foreach ($type_info['bundles'] as $bundle_name => $bundle) {
$perms['like/dislike any ' . $bundle_name . ' comments'] = array(
'title' => t('Add like/dislike to any %type', array(
'%type' => $bundle['label'],
)),
'description' => t('Allow users to Like/Dislike entities from %type content type.', array(
'%type' => $bundle['label'],
)),
'restrict access' => TRUE,
);
$perms['view likes/dislikes from every ' . $bundle_name . ' comments'] = array(
'title' => t('View likes/dislikes from every %type', array(
'%type' => $bundle['label'],
)),
'description' => t('Allow users to view Likes/Dislikes from entities of type %type.', array(
'%type' => $bundle['label'],
)),
'restrict access' => TRUE,
);
}
break;
}
}
return $perms;
}
function like_and_dislike_theme() {
return array(
'like' => array(
'template' => 'templates/like',
),
'dislike' => array(
'template' => 'templates/dislike',
),
);
}
function like_and_dislike_entity_load($entities, $entity_type) {
foreach ($entities as $entity_id => $entity) {
$Entity = new \Drupal\like_and_dislike\Model\Entity($entity);
if ($Entity
->voteIsAvailable()) {
$entity->like = $Entity
->getLikesAmount();
$entity->dislike = $Entity
->getDislikesAmount();
}
}
}
function like_and_dislike_entity_view($entity, $type, $view_mode, $langcode) {
$Entity = new \Drupal\like_and_dislike\Model\Entity($entity);
if ($Entity
->voteIsAvailable()) {
global $user;
$can_vote = $Entity
->userCanVote($user);
if ($user->uid == 0) {
$ip = ip_address();
$likestatus = $can_vote ? $Entity
->getLikesAmount($user->uid, $ip) : 1;
$dislikestatus = $can_vote ? $Entity
->getDislikesAmount($user->uid, $ip) : 1;
}
else {
$likestatus = $can_vote ? $Entity
->getLikesAmount($user->uid) : 1;
$dislikestatus = $can_vote ? $Entity
->getDislikesAmount($user->uid) : 1;
}
if ($can_vote || $Entity
->userCanViewVotes()) {
if (isset($entity->like) && isset($entity->dislike)) {
$id = 'like-and-dislike-widget-' . $Entity->entity_type . '-' . $Entity->entity_id;
$like_widget = theme('like', array(
'entity_id' => $Entity->entity_id,
'likes' => $Entity
->getLikesAmount(),
'likestatus' => $likestatus,
'entity_type' => $Entity->entity_type,
));
$dislike_widget = theme('dislike', array(
'entity_id' => $Entity->entity_id,
'dislikes' => $Entity
->getDislikesAmount(),
'dislikestatus' => $dislikestatus,
'entity_type' => $Entity->entity_type,
));
$entity->content['like_and_dislike_widgets'] = array(
'#prefix' => '<div id="' . $id . '" class="like-and-dislike-widget clearfix">',
'#suffix' => '</div>',
'#markup' => $like_widget . $dislike_widget,
'#weight' => 100,
);
}
}
}
return $entity;
}
function like_and_dislike_votingapi_metadata_alter(&$data) {
$data['tags']['like'] = array(
'name' => t('Like'),
'description' => t('A like vote, for positive reviews.'),
);
$data['tags']['dislike'] = array(
'name' => t('Dislike'),
'description' => t('A dislike vote, for negative reviews.'),
);
}
function like_and_dislike_views_api() {
return array(
'api' => 3,
);
}