Software development is delicate.
We handle it carefully.
Recently, while I was working on a new website, and doing all sorts of experiments including access control related modules, I ended up receiving "Access denied.
You are not authorized to access this page" message.
I double checked the permissions settings in the admin pages, and they all seemed to be configured correctly.
I un-installed every module that looked suspicious, but still got the same message.
I then went another direction to search for the problem, and checked the db.
; $Id$ name = "Dynamic Products" description = "Returns a list of products according to category." core = 6.x package = Example Ajax ModulesCopy, pase & save the above code into “dynamic_products.info”. After we have our .info file created, we continue to create our .module file. This file includes the actual server side code.Go ahead and create “dynamic_products.module” file in our module’s folder.
<?php
function dynamic_products_menu() {
$items = array();
$items['products/get'] = array(
'title' => 'Dynamic Products',
'page callback' => 'dynamic_products_get_by_category_id',
'access arguments' => array('access dynamic_products content'),
'type' => MENU_CALLBACK
);
return $items;
}
This tells Drupal to intercept all calls to “http://www.example.com/?q=products/get” or http://www.example.com/products/get, and call the dynamic_products_get_by_category_id callback function.
function dynamic_products_perm() {
return array('access dynamic_products content');
}
More info about setting permissions can be found here.
function dynamic_products_get_by_category_id($cat_id){
$items = '';
switch($cat_id){
case 12:
$items = '<ul><li>Product 1</li><li>Product 2</li></ul>';
break;
case 20:
$items = '<ul><li>Product 3</li><li>Product 4</li></ul>';
break;
}
// create a JSON object. The object will contain a property named “products” that will be set with the $items variable.
return drupal_json(array('products'=>$items));
exit;
}
Our callback function, accepts a single parameter ($cat_id). Depending on its value, a different list is returned.
// $Id$
Drupal.behaviors.dynamic_products = function (context) {
$('a.categoryLink:not(.categoryLink-processed)', context).click(function () {
// This function will get exceuted after the ajax request is completed successfully
var updateProducts = function(data) {
// The data parameter is a JSON object. The “products” property is the list of products items that was returned from the server response to the ajax request.
$('#divProducts').html(data.products);
}
$.ajax({
type: 'POST',
url: this.href, // Which url should be handle the ajax request. This is the url defined in the <a> html tag
success: updateProducts, // The js function that will be called upon success request
dataType: 'json', //define the type of data that is going to get back from the server
data: 'js=1' //Pass a key/value pair
});
return false; // return false so the navigation stops here and not continue to the page in the link
}).addClass('categoryLink-processed');
}
Copy, paste and save the above code into the dynamic_products.js file.
We are done with the js file.
function dynamic_products_theme() {
return array(
'dynamic_products_javascript' => array(
'arguments' => array(),
),
);
}
function dynamic_products_init() {
theme('dynamic_products_javascript');
}
function theme_dynamic_products_javascript() {
drupal_add_js(drupal_get_path('module', 'dynamic_products') . '/dynamic_products.js');
}
Save the .module file.
<div id=”topDiv”> <a class="categoryLink" href="/products/get/12">Cat. 1</a> <a class="categoryLink" href="/products/get/20">Cat. 2</a> </div> <div id="divProducts"></div>Create your Drupal page, copy and paste the above code, and that’s it. When you view the page, if you will click one of the “categories” links, a call will be made to the server, and the page will display a list of products specific to the category clicked.