ajax_links_api.module in Ajaxify Drupal with JQuery Ajax 6
Same filename and directory in other branches
Make any links or create new links to load content to particular DIV via jQuery Ajax.
File
ajax_links_api.moduleView source
<?php
/**
* @file
* Make any links or create new links to load content to particular DIV via
* jQuery Ajax.
*/
/**
* Implements hook_init().
*/
function ajax_links_api_init() {
drupal_add_js(drupal_get_path('module', 'ajax_links_api') . '/ajax_links_api.js');
drupal_add_css(drupal_get_path('module', 'ajax_links_api') . '/ajax_links_api.css');
$selector = variable_get('ajax_links_api_selector', '#content');
$trigger = ajax_links_api_get_triggers();
$html5 = variable_get('ajax_links_api_html5', 1);
$views_pager = variable_get('ajax_links_api_vpager', 1);
drupal_add_js(array(
'ajax_links_api' => array(
'selector' => $selector,
'trigger' => $trigger,
'html5' => $html5,
'vpager' => $views_pager,
),
), 'setting');
}
/**
* Implements hook_menu().
*/
function ajax_links_api_menu() {
// Admin setting.
$items['admin/settings/ajax-links-api'] = array(
'title' => 'Ajax links API',
'description' => 'Ajax links API settings',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'ajax_links_api_admin',
),
'access arguments' => array(
'administer site configuration',
),
);
// Test page.
$items['ajax-links-api/test'] = array(
'title' => 'Ajax links API test',
'page callback' => 'ajax_links_api_test',
'access arguments' => array(
'administer site configuration',
),
'type' => MENU_CALLBACK,
);
return $items;
}
/**
* Callback function for admin setting.
*/
function ajax_links_api_admin() {
$form['ajax_links_api_trigger'] = array(
'#type' => 'textarea',
'#title' => t('Classes/IDs to trigger ajax (One per line)'),
'#default_value' => variable_get('ajax_links_api_trigger', '.ajax-link' . "\n"),
'#description' => t('Specify the class/ID of links to ajaxify that link, one per line. For example by providing ".ajax-link" will ajaxify any link with class="ajax-link"'),
);
$form['ajax_links_api_selector'] = array(
'#type' => 'textfield',
'#title' => t('Default Target DIV'),
'#default_value' => variable_get('ajax_links_api_selector', '#content'),
'#description' => t('This can be override for indivdual link by providing rel. Check Demo.'),
);
$form['ajax_links_api_html5'] = array(
'#type' => 'checkbox',
'#title' => t('Enable URL and Title change (for HTML5 Only)'),
'#default_value' => variable_get('ajax_links_api_html5', 1),
'#description' => t('Change URL and Title according to ajax content. This will work only for HTML5 supported browsers. Tested on latest Chrome,Firefox.'),
);
$form['ajax_links_api_vpager'] = array(
'#type' => 'checkbox',
'#title' => t('Remove ?ajax=1 from Views pager'),
'#default_value' => variable_get('ajax_links_api_vpager', 1),
'#description' => t('Remove ?ajax=1 from Views pager. Check check http://drupal.org/node/1907376 for details.'),
);
return system_settings_form($form);
}
/**
* Implements hook_theme().
*/
function ajax_links_api_theme() {
return array(
'ajax_links_api_link' => array(
'arguments' => array(
'title' => NULL,
'path' => NULL,
'target' => NULL,
),
),
);
}
/**
* Ajax links API.
*
* @param string $title
* Title to display.
* @param string $path
* Drupal path eg: user/login.
* @param string $target
* ID or CLASS of DIV to be replaced. eg: #content-content or .content.
*
* @return string
* a link with class ajax_link and rel=$target.
*/
function l_ajax($title, $path, $target = NULL) {
if ($target == NULL) {
$target = variable_get('ajax_links_api_selector', '#content');
}
$path = url($path, array(
'absolute' => TRUE,
));
return theme('ajax_links_api_link', $title, $path, $target);
}
/**
* Theme ajax_links_api.
*/
function theme_ajax_links_api_link($title, $path, $target) {
$link = l($title, $path, $options = array(
'attributes' => array(
'class' => 'ajax-link',
'rel' => $target,
),
));
return $link;
}
/**
* Implements hook_preprocess_page().
*/
function ajax_links_api_preprocess_page(&$vars) {
// This is used to display only $content.
if (isset($_GET['ajax']) && $_GET['ajax'] == 1) {
$vars['template_files'][] = 'page-ajax';
foreach ($vars['template_files'] as $suggestion) {
$vars['template_files'][] = $suggestion . '-ajax';
}
array_pop($vars['template_files']);
}
}
/**
* Implements hook_help().
*/
function ajax_links_api_help($path, $arg) {
if ($path == 'admin/settings/ajax-links-api' || $path == 'admin/help#ajax_links_api') {
$output = t('<h2><strong>Before start</strong></h2>
<p>In your theme page.tpl.php , <code>@content</code> should be surrounded by a div with a class
(or id).If no div ,add the div yourself.</p>
<p>Example : <code>@target</code></p>
<p>In this case, just enter #content as Default Target DIV in module config page</p>
<h2><strong>How to use Ajax links API</strong></h2>
<p><strong>Method 1</strong> : You can ajaxify any links by specifying the Class/Id in <a href="@admin">Module settings page</a>.
Target DIV will be default Target DIV defined in module config page.</p>
<p>Example : <code>@example1</code> . You can ajaxify this link by adding
<code>.test</code> in module config page.</p>
<p><strong>Method 2</strong> : Use this in your tpl => <code>echo l_ajax($title, $path, $target)</code></p>
<p>* <code>$title</code>: Title to display.<br />
* <code>$path</code> : Drupal path. <br />
* <code>$target (optional)</code>: ID or CLASS of DIV to be replaced. You can override default Target DIV defined by specifying <code>$target</code>.</p>
<p>Example : <code>l_ajax("Add Page","node/add/page","#content")</code>.</p>
<p><strong>Method 3</strong> : Add <code>class="ajax-link"</code> to any link. Target div will be Target DIV defined . You can change default Target DIV or override target by specifying <code>rel=""</code>.</p>
<p>Example : <code>@example2</code></p>
<h2><a href="@demo">DEMO</a></h2>', array(
'@content' => '$content',
'@target' => '<div class="#content"><?php print $content; ?></div>',
'@admin' => url('admin/settings/ajax-links-api'),
'@example1' => '<a class="test" href="node/add/page">Add page</a>',
'@example2' => '<a class="ajax-link" href="node/add/page" rel="#content">Add page</a>',
'@demo' => url('ajax-links-api/test'),
));
}
return isset($output) ? $output : NULL;
}
/**
* Implements hook_theme_registry_alter().
*/
function ajax_links_api_theme_registry_alter(&$theme_registry) {
$mod_path = drupal_get_path('module', 'ajax_links_api') . '/tpl';
$theme_registry_copy = $theme_registry;
// munge on a copy
_theme_process_registry($theme_registry_copy, 'phptemplate', 'theme_engine', 'pow', $mod_path);
$theme_registry += array_diff_key($theme_registry_copy, $theme_registry);
$hooks = array(
'page',
);
foreach ($hooks as $h) {
if (is_array($theme_registry[$h]['theme paths'])) {
$first_element = array_shift($theme_registry[$h]['theme paths']);
array_unshift($theme_registry[$h]['theme paths'], $first_element, $mod_path);
}
}
}
/**
* Get trigger classes/ids.
*/
function ajax_links_api_get_triggers() {
$trigger = variable_get('ajax_links_api_trigger', '.ajax-link' . "\n");
$trigger = explode("\n", $trigger);
$trigger = array_filter($trigger);
$trigger = implode(',', $trigger);
return $trigger;
}
/**
* Callback - ajax-links-api/test.
*/
function ajax_links_api_test() {
global $base_url;
$link1 = '<a href="' . $base_url . '/user" class="test" rel=".test1">Load User Page</a>';
$link2 = l_ajax('load this test page', 'ajax-links-api/test', '#content');
$link3 = '<a href="' . $base_url . '/user" class="ajax-link" rel=".test2">Load User page</a>';
$ouptut = '<h2>Method 1 : load Profile </h2>(link with class="test" and rel=".test1". You can ajaxify this link by adding this link
class .test in module config page):<br />' . $link1 . '<div class="test1"></div>';
$ouptut .= '<h2>Method 2 : load this test page </h2>(using l_ajax):<br />' . $link2 . '';
$ouptut .= '<h2>Method 3 : Load profile </h2>(link with class="ajax-link" and rel=".test2"):<br />' . $link3 . '<div class="test2"></div>';
return $ouptut;
}
Functions
Name | Description |
---|---|
ajax_links_api_admin | Callback function for admin setting. |
ajax_links_api_get_triggers | Get trigger classes/ids. |
ajax_links_api_help | Implements hook_help(). |
ajax_links_api_init | Implements hook_init(). |
ajax_links_api_menu | Implements hook_menu(). |
ajax_links_api_preprocess_page | Implements hook_preprocess_page(). |
ajax_links_api_test | Callback - ajax-links-api/test. |
ajax_links_api_theme | Implements hook_theme(). |
ajax_links_api_theme_registry_alter | Implements hook_theme_registry_alter(). |
l_ajax | Ajax links API. |
theme_ajax_links_api_link | Theme ajax_links_api. |