summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDmitry Zhgenti2011-09-21 09:17:35 (GMT)
committerDmitry Zhgenti2011-09-21 09:17:35 (GMT)
commitcbb1b5f97f6da11d7b788b04d28c9dba1635ed7e (patch)
treeec7e5ed85ffa54fdd4b610a0e4783a5f832ca0ec
parentb4aa1ae0a03425917b9e947fdc8363f1cd5a4094 (diff)
Issue #506936 by Zhgenti: Render API is used instead of just theming function. plus1_get_cleared_destination() has been added. Code style corrections.
-rw-r--r--plus1.css40
-rw-r--r--plus1.module180
-rw-r--r--theme/theme.inc20
3 files changed, 118 insertions, 122 deletions
diff --git a/plus1.css b/plus1.css
index 0563671..5a3e35f 100644
--- a/plus1.css
+++ b/plus1.css
@@ -35,46 +35,6 @@ div.plus1-widget .plus1-undo-vote a {
margin-bottom: 10px;
}
-.plus1-comment-abuse {
- background: url(images/abuse.png) no-repeat 0 0;
- padding-left: 18px;
- padding-top: 1px;
-}
-
-.plus1-comment-abuse-checked {
- background: url(images/abuse.png) no-repeat 0 -16px;
- padding-left: 18px;
- padding-top: 1px;
-}
-
-a.plus1-comment-abuse:hover {
- background: url(images/abuse.png) no-repeat 0 -16px;
-}
-
-a.plus1-comment-abuse-checked:hover {
- background: url(images/abuse.png) no-repeat 0 0;
-}
-
-.plus1-comment-vote {
- background: url(images/thumbs_up.png) no-repeat 0 0;
- padding: 2px 0 0 18px;
-
-}
-
-.plus1-comment-vote-checked {
- background: url(images/thumbs_up.png) no-repeat 0 -20px;
- padding: 2px 0 0 18px;
-
-}
-
-a.plus1-comment-vote:hover {
- background: url(images/thumbs_up.png) no-repeat 0 -20px;
-}
-
-a.plus1-comment-vote-checked:hover {
- background: url(images/thumbs_up.png) no-repeat 0 0px;
-}
-
.plus1-comment-score {
color: black;
padding-right: 2px;
diff --git a/plus1.module b/plus1.module
index 99b8a4e..5b97215 100644
--- a/plus1.module
+++ b/plus1.module
@@ -275,90 +275,117 @@ function plus1_get_score($entity_type, $entity_id, $tag = 'plus1_node_vote') {
/**
* Create voting widget to display on the webpage.
*/
-function plus1_node_jquery_widget($entity_id, $tag = 'plus1_node_vote') {
+function plus1_build_node_jquery_widget($entity_id, $tag = 'plus1_node_vote') {
global $user;
$node = node_load($entity_id);
$score = plus1_get_score('node', $node->nid, $tag);
$logged_in = $user->uid > 0;
$is_author = $node->uid == $user->uid;
$voted = plus1_get_votes('node', $node->nid, $user->uid, $tag);
- $variables = array(
- 'entity_type' => 'node',
- 'entity_id' => $node->nid,
- 'tag' => $tag,
- 'score' => $score,
- 'logged_in' => $logged_in,
- 'is_author' => $is_author,
- 'voted' => $voted,
- 'vote_link' => 'plus1/vote/node/' . $node->nid . '/' . $tag,
- 'undo_vote_link' => 'plus1/undo-vote/node/' . $node->nid . '/' . $tag,
- 'link_query' => array('token' => drupal_get_token($node->nid), drupal_get_destination()),
- 'can_vote' => user_access('plus1 vote on node'),
- 'can_undo_vote' => variable_get('plus1_node_undo_vote', 0),
- 'undo_vote_text' => check_plain(variable_get('plus1_node_undo_vote_text', '')),
- 'voted_text' => check_plain(variable_get('plus1_node_voted_text', t('You voted'))),
- 'vote_text' => check_plain(variable_get('plus1_node_vote_text', t('Vote'))),
+ $build = array(
+ '#theme' => 'plus1_widget__node__' . $tag,
+ '#weight' => (int) variable_get('plus1_node_widget_weight', '100'),
+ '#entity_type' => 'node',
+ '#entity_id' => $node->nid,
+ '#tag' => $tag,
+ '#score' => $score,
+ '#logged_in' => $logged_in,
+ '#is_author' => $is_author,
+ '#voted' => $voted,
+ '#vote_link' => 'plus1/vote/node/' . $node->nid . '/' . $tag,
+ '#undo_vote_link' => 'plus1/undo-vote/node/' . $node->nid . '/' . $tag,
+ '#link_query' => array('token' => drupal_get_token($node->nid), plus1_get_cleared_destination()),
+ '#can_vote' => user_access('plus1 vote on node'),
+ '#can_undo_vote' => variable_get('plus1_node_undo_vote', 0),
+ '#undo_vote_text' => check_plain(variable_get('plus1_node_undo_vote_text', '')),
+ '#voted_text' => check_plain(variable_get('plus1_node_voted_text', t('You voted'))),
+ '#vote_text' => check_plain(variable_get('plus1_node_vote_text', t('Vote'))),
);
- return theme('plus1_widget__node__' . $tag, $variables);
+ if (variable_get('plus1_add_js', 1)) {
+ $build['#attached']['js'][] = drupal_get_path('module', 'plus1') . '/jquery.plus1.js';
+ }
+ if (variable_get('plus1_add_css', 1)) {
+ $build['#attached']['css'][] = drupal_get_path('module', 'plus1') . '/plus1.css';
+ }
+ drupal_alter('plus1_widget', $build, $node);
+ return $build;
}
/**
* Create voting widget to display on the webpage.
*/
-function plus1_comment_jquery_widget($entity_id, $tag = 'plus1_comment_vote') {
+function plus1_build_comment_jquery_widget($entity_id, $tag = 'plus1_comment_vote') {
global $user;
$comment = comment_load($entity_id);
$score = plus1_get_score('comment', $comment->cid, $tag);
$logged_in = $user->uid > 0;
$is_author = $comment->uid == $user->uid;
$voted = plus1_get_votes('comment', $comment->cid, $user->uid, $tag);
- $variables = array(
- 'entity_type' => 'comment',
- 'entity_id' => $comment->cid,
- 'tag' => $tag,
- 'score' => $score,
- 'logged_in' => $logged_in,
- 'is_author' => $is_author,
- 'voted' => $voted,
- 'vote_link' => 'plus1/vote/comment/' . $comment->cid . '/' . $tag,
- 'undo_vote_link' => 'plus1/undo-vote/comment/' . $comment->cid . '/' . $tag,
- 'link_query' => array('token' => drupal_get_token($comment->cid), drupal_get_destination()),
- 'can_vote' => user_access('plus1 vote on comment'),
- 'can_undo_vote' => variable_get('plus1_comment_undo_vote', 0),
- 'undo_vote_text' => check_plain(variable_get('plus1_comment_undo_vote_text', "")),
- 'voted_text' => check_plain(variable_get('plus1_comment_voted_text', t('You voted'))),
- 'vote_text' => check_plain(variable_get('plus1_comment_vote_text', t('Vote'))),
+ $build = array(
+ '#theme' => 'plus1_widget__comment__' . $tag,
+ '#weight' => (int) variable_get('plus1_comment_widget_weight', '100'),
+ '#entity_type' => 'comment',
+ '#entity_id' => $comment->cid,
+ '#tag' => $tag,
+ '#score' => $score,
+ '#logged_in' => $logged_in,
+ '#is_author' => $is_author,
+ '#voted' => $voted,
+ '#vote_link' => 'plus1/vote/comment/' . $comment->cid . '/' . $tag,
+ '#undo_vote_link' => 'plus1/undo-vote/comment/' . $comment->cid . '/' . $tag,
+ '#link_query' => array('token' => drupal_get_token($comment->cid), plus1_get_cleared_destination()),
+ '#can_vote' => user_access('plus1 vote on comment'),
+ '#can_undo_vote' => variable_get('plus1_comment_undo_vote', 0),
+ '#undo_vote_text' => check_plain(variable_get('plus1_comment_undo_vote_text', "")),
+ '#voted_text' => check_plain(variable_get('plus1_comment_voted_text', t('You voted'))),
+ '#vote_text' => check_plain(variable_get('plus1_comment_vote_text', t('Vote'))),
);
- return theme('plus1_widget__comment__' . $tag, $variables);
+ if (variable_get('plus1_add_js', 1)) {
+ $build['#attached']['js'][] = drupal_get_path('module', 'plus1') . '/jquery.plus1.js';
+ }
+ if (variable_get('plus1_add_css', 1)) {
+ $build['#attached']['css'][] = drupal_get_path('module', 'plus1') . '/plus1.css';
+ }
+ drupal_alter('plus1_widget', $build, $comment);
+ return $build;
}
/**
* Create voting widget to display on the webpage.
*/
-function plus1_taxonomy_term_jquery_widget($entity_id, $tag = 'plus1_taxonomy_term_vote') {
+function plus1_build_taxonomy_term_jquery_widget($entity_id, $tag = 'plus1_taxonomy_term_vote') {
global $user;
$term = taxonomy_term_load($entity_id);
$score = plus1_get_score('taxonomy_term', $term->tid, $tag);
$logged_in = $user->uid > 0;
$voted = plus1_get_votes('taxonomy_term', $term->tid, $user->uid, $tag);
- $variables = array(
- 'entity_type' => 'taxonomy_term',
- 'entity_id' => $term->tid,
- 'tag' => $tag,
- 'score' => $score,
- 'logged_in' => $logged_in,
- 'is_author' => FALSE,
- 'voted' => $voted,
- 'vote_link' => 'plus1/vote/taxonomy_term/' . $term->tid . '/' . $tag,
- 'undo_vote_link' => 'plus1/undo-vote/taxonomy_term/' . $term->tid . '/' . $tag,
- 'link_query' => array('token' => drupal_get_token($term->tid), drupal_get_destination()),
- 'can_vote' => user_access('plus1 vote on taxonomy_term'),
- 'can_undo_vote' => variable_get('plus1_taxonomy_term_undo_vote', 0),
- 'undo_vote_text' => check_plain(variable_get('plus1_taxonomy_term_undo_vote_text', '')),
- 'voted_text' => check_plain(variable_get('plus1_taxonomy_term_voted_text', t('You voted'))),
- 'vote_text' => check_plain(variable_get('plus1_taxonomy_term_vote_text', t('Vote'))),
+ $build = array(
+ '#theme' => 'plus1_widget__taxonomy_term__' . $tag,
+ '#weight' => (int) variable_get('plus1_taxonomy_widget_weight', '100'),
+ '#entity_type' => 'taxonomy_term',
+ '#entity_id' => $term->tid,
+ '#tag' => $tag,
+ '#score' => $score,
+ '#logged_in' => $logged_in,
+ '#is_author' => FALSE,
+ '#voted' => $voted,
+ '#vote_link' => 'plus1/vote/taxonomy_term/' . $term->tid . '/' . $tag,
+ '#undo_vote_link' => 'plus1/undo-vote/taxonomy_term/' . $term->tid . '/' . $tag,
+ '#link_query' => array('token' => drupal_get_token($term->tid), plus1_get_cleared_destination()),
+ '#can_vote' => user_access('plus1 vote on taxonomy_term'),
+ '#can_undo_vote' => variable_get('plus1_taxonomy_term_undo_vote', 0),
+ '#undo_vote_text' => check_plain(variable_get('plus1_taxonomy_term_undo_vote_text', '')),
+ '#voted_text' => check_plain(variable_get('plus1_taxonomy_term_voted_text', t('You voted'))),
+ '#vote_text' => check_plain(variable_get('plus1_taxonomy_term_vote_text', t('Vote'))),
);
- return theme('plus1_widget__taxonomy_term__' . $tag, $variables);
+ if (variable_get('plus1_add_js', 1)) {
+ $build['#attached']['js'][] = drupal_get_path('module', 'plus1') . '/jquery.plus1.js';
+ }
+ if (variable_get('plus1_add_css', 1)) {
+ $build['#attached']['css'][] = drupal_get_path('module', 'plus1') . '/plus1.css';
+ }
+ drupal_alter('plus1_widget', $build, $term);
+ return $build;
}
/**
@@ -368,10 +395,7 @@ function plus1_node_view($node, $view_mode) {
// Only show the voting widget in allowed content types.
if (in_array($node->type, variable_get('plus1_node_types', array()))) {
if (($view_mode == 'teaser' && variable_get('plus1_node_in_teaser', 0)) || ($view_mode == 'full' && variable_get('plus1_node_in_full_view', 1))) {
- $node->content['plus1_widget'] = array(
- '#markup' => plus1_node_jquery_widget($node->nid),
- '#weight' => (int) variable_get('plus1_node_widget_weight', '100'),
- );
+ $node->content['plus1_widget'] = plus1_build_node_jquery_widget($node->nid);
}
}
}
@@ -392,10 +416,7 @@ function plus1_node_delete($node) {
*/
function plus1_taxonomy_term_view_alter(&$build) {
if (variable_get('plus1_taxonomy_term_widget_show', 0) && in_array($build['#term']->vid, variable_get('plus1_taxonomy_vocabularies', array()))) {
- $build['plus1_taxonomy_widget'] = array(
- '#markup' => plus1_taxonomy_term_jquery_widget($build['#term']->tid),
- '#weight' => (int) variable_get('plus1_taxonomy_widget_weight', '100'),
- );
+ $build['plus1_taxonomy_widget'] = plus1_build_taxonomy_term_jquery_widget($build['#term']->tid);
}
}
@@ -403,7 +424,7 @@ function plus1_taxonomy_term_view_alter(&$build) {
* Implements hook_taxonomy_term_delete().
*/
function plus1_taxonomy_term_delete($term) {
- $criteria['entity_id'] = $term->nid;
+ $criteria['entity_id'] = $term->tid;
$criteria['entity_type'] = 'taxonomy_term';
$votes = votingapi_select_votes($criteria);
votingapi_delete_votes($votes);
@@ -414,10 +435,7 @@ function plus1_taxonomy_term_delete($term) {
*/
function plus1_comment_view($comment, $view_mode, $langcode) {
if (variable_get('plus1_comment_widget_show', 0) && !isset($comment->in_preview)) {
- $comment->content['plus1_widget'] = array(
- '#markup' => plus1_comment_jquery_widget($comment->cid),
- '#weight' => (int) variable_get('plus1_comment_widget_weight', '100'),
- );
+ $comment->content['plus1_widget'] = plus1_build_comment_jquery_widget($comment->cid);
}
}
@@ -432,7 +450,7 @@ function plus1_comment_delete($comment) {
}
/**
- * Implements hook_votingapi_metadata_alter()
+ * Implements hook_votingapi_metadata_alter().
*/
function plus1_votingapi_metadata_alter(&$data) {
// Document several custom tags for rating restaurants and meals.
@@ -457,6 +475,32 @@ function plus1_votingapi_metadata_alter(&$data) {
}
/**
+ * If widget is returned by ajax, and there are ajax_html_ids in the $_GET,
+ * then we need to clear destination parameter, reduce query length.
+ */
+function plus1_get_cleared_destination() {
+ $clear_destination = &drupal_static(__FUNCTION__);
+ if (isset($clear_destination)) {
+ return $clear_destination;
+ }
+ if (isset($_GET['destination'])) {
+ $path = $_GET['destination'];
+ }
+ else {
+ $path = $_GET['q'];
+ $query = $_GET;
+ unset($query['ajax_html_ids']);
+ unset($query['ajax_page_state']);
+ $query = drupal_http_build_query(drupal_get_query_parameters($query));
+ if ($query != '') {
+ $path .= '?' . $query;
+ }
+ }
+ $clear_destination = array('destination' => $path);
+ return $clear_destination;
+}
+
+/**
* This hook is called when user votes on some content.
* @param $vote_type
* Vote type, "vote" or "undo_vote"
diff --git a/theme/theme.inc b/theme/theme.inc
index 018d65b..2670dd7 100644
--- a/theme/theme.inc
+++ b/theme/theme.inc
@@ -49,34 +49,26 @@ function template_preprocess_plus1_widget(&$variables) {
// User is eligible to vote.
$variables['widget_message'] = l($vote_text, $vote_link, array('query' => $link_query, 'attributes' => array('class' => array('plus1-link'))));
}
-
- // Load the JavaScript and CSS files.
- // You can disable loading of these files and create your own instead of them.
- if (variable_get('plus1_add_js', 1)) {
- drupal_add_js(drupal_get_path('module', 'plus1') . '/jquery.plus1.js');
- }
- if (variable_get('plus1_add_css', 1)) {
- drupal_add_css(drupal_get_path('module', 'plus1') . '/plus1.css');
- }
}
/**
* Theming function for json response.
* @param $variables
* @return array
- * Returns an array of variables which will be send back to browser, after ajax request.
- * @see plus1_vote() and plus1_undo_vote()
+ *
+ * Returns an array of variables which will be send back to browser, after ajax request.
+ * @see plus1_vote(), plus1_undo_vote()
*/
function theme_plus1_json_response($variables) {
switch ($variables['entity_type']) {
case 'node':
- return array('widget' => plus1_node_jquery_widget($variables['entity_id'], $variables['tag']));
+ return array('widget' => drupal_render(plus1_build_node_jquery_widget($variables['entity_id'], $variables['tag'])));
break;
case 'taxonomy_term':
- return array('widget' => plus1_taxonomy_term_jquery_widget($variables['entity_id'], $variables['tag']));
+ return array('widget' => drupal_render(plus1_build_taxonomy_term_jquery_widget($variables['entity_id'], $variables['tag'])));
break;
case 'comment':
- return array('widget' => plus1_comment_jquery_widget($variables['entity_id'], $variables['tag']));
+ return array('widget' => drupal_render(plus1_build_comment_jquery_widget($variables['entity_id'], $variables['tag'])));
break;
}
}