diff --git a/plus1.module b/plus1.module index 99b8a4e..a0fa4d3 100644 --- a/plus1.module +++ b/plus1.module @@ -275,90 +275,129 @@ 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), 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'))), ); - return theme('plus1_widget__node__' . $tag, $variables); + if (variable_get('plus1_add_js', 1)) { + $build['#attached']['js'] = array( + drupal_get_path('module', 'plus1') . '/jquery.plus1.js', + ); + } + if (variable_get('plus1_add_css', 1)) { + $build['#attached']['css'] = array( + 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), 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'))), ); - return theme('plus1_widget__comment__' . $tag, $variables); + if (variable_get('plus1_add_js', 1)) { + $build['#attached']['js'] = array( + drupal_get_path('module', 'plus1') . '/jquery.plus1.js', + ); + } + if (variable_get('plus1_add_css', 1)) { + $build['#attached']['css'] = array( + 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), 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'))), ); - return theme('plus1_widget__taxonomy_term__' . $tag, $variables); + if (variable_get('plus1_add_js', 1)) { + $build['#attached']['js'] = array( + drupal_get_path('module', 'plus1') . '/jquery.plus1.js', + ); + } + if (variable_get('plus1_add_css', 1)) { + $build['#attached']['css'] = array( + drupal_get_path('module', 'plus1') . '/plus1.css', + ); + } + drupal_alter('plus1_widget', $build, $term); + return $build; } /** @@ -368,10 +407,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 +428,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); } } @@ -414,10 +447,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); } } diff --git a/theme/theme.inc b/theme/theme.inc index 018d65b..d8a67af 100644 --- a/theme/theme.inc +++ b/theme/theme.inc @@ -49,15 +49,6 @@ 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'); - } } /** @@ -70,13 +61,13 @@ function template_preprocess_plus1_widget(&$variables) { 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; } }