Loading content with AJAX link, anywhere

There are different methods and bunch of modules to ajaxify links in drupal. There are different solutions with code, like this one
https://www.drupal.org/node/2046693
and there are more simple I would add, like this one
http://codekarate.com/blog/drupal-7-javascript-ajax-framework-example-l…

I used variation of the codekarate tutorial. To put my AJAX link anywhere. I used template, to be more precise I added a custom form element in rendered array. Like this

$content['helpful-comments'] = array(
  '#type' => 'link',
  '#title' => t('Most helpful first'),
  '#href' => 'comments/helpful/nojs/' . $node->nid,
  '#ajax' => array(
    'effect' => 'fade',
    ),
  );

Then I rendered that print render($content['helpful-comments']) to get full AJAX link.
And added that link to menu items in custom module

  $items['comments/helpful/%/%'] = array( 
    'title' => 'Ajax test callback',
    'type' => MENU_CALLBACK,
    'page callback' => 'my_module_custom_comments_callback',
    'page arguments' => array(1,2,3),
    'access arguments' => array('access content'),
    );

(dont forget to clear cache here, menu cache needs to be cleared)

Then In custom module function I did my actions. I loaded view with views_embed_view and added that content on proper div
with ajax_command_replace. Works like charm.


/**
 * Ajax callback to display the current time.
 */
function my_module_custom_comments_callback($url, $ajax, $nodeid) {

  if ($ajax == 'ajax') {
    // Check which view are we dealing with
    if ($url == "helpful"){
      $view = "entity_view_1";
      $class = "comments-most-helpful";
    }
    else if ($url == "newest"){
      $view = "entity_view_2";
      $class = "comments-newest";
    }
    // Create content variable
    $content = views_embed_view('product_reviews', $view, $nodeid);

    // Standard Ajax thing
    $commands = array();
    // Ajax command to replace the #ajax-display element with the current time.
    $commands[] = ajax_command_replace('.' . $class, "
" . $content . "

");
// Add a visual "changed" marker to the '#ajax-display' element.
$commands[] = ajax_command_changed('.' . $class);
// Menu 'page callback' and #ajax['callback'] functions are supposed to
// return render arrays. If returning an Ajax commands array, it must be
// encapsulated in a render array structure.
ajax_deliver(array('#type' => 'ajax', '#commands' => $commands));
}
}