Drupal 7 – Simple Ajax implementation @ 2

I will continue the Ajax example which we did in
Drupal 7 – Simple Ajax implementation @ 1

So this time we will add some access control on the above example. In Drupal 7 & 8 development, we could create new permissions by implementing the hook_permission(). After that we could modify the access arguments in the hook_menu().

Let’s add the hook_permission() and modified the hook_menu() as follow.
eureka_ajax.module

<?php
/**
 * Implementation of hook_init().
 */
function eureka_ajax_init() {
  // Include the js file
  drupal_add_js(drupal_get_path('module', 'eureka_ajax') . '/eureka_ajax.js');
}

/**
* Implementation of hook_permission().
*/
function eureka_ajax_permission() {
  // Restrict access to either of your new URLs.
  return array(
    'access eureka page' => array(
      'title' => t('Access eureka page'),
      'description' => t('Allow users to access eureka/page.'),
    ),
    'access eureka ajax' => array(
      'title' => t('Access eureka ajax'),
      'description' => t('Allow users to access the eureka ajax callback at eureka/ajax.'),
    ),
  );
}

/**
 * Implementation of hook_menu().
 */
function eureka_ajax_menu() {
  $items = array();

  // Create a Drupal page to display our AJAX link.
  $items['eureka/page'] = array(
    'title' => 'Test Page',
    'page callback' => 'test_page_callback',
    'access arguments' => array('access eureka page'),
    'type' => MENU_NORMAL_ITEM,
  );

  // Create a path to send our AJAX request to.
  $items['eureka/ajax'] = array(
    'title' => 'Ajax Request',
    'page callback' => 'ajax_request_callback',
    'access arguments' => array('access eureka ajax'),
    'type' => MENU_CALLBACK,
  );

  return $items;
}

/**
 * Callback function for /eureka/page.
 */
function test_page_callback() {
  return theme('ajax_link');
}

/**
 * Implementation of hook_theme().
 */
function eureka_ajax_theme() {
  return array(
    'ajax_link' => array(),
  );
}

/**
 * Implementation of theme_ajax_link().
 */
function theme_ajax_link() {
  return l(t('What is the date and time?'), 'eureka/ajax', array(
    'attributes' => array(
      'id' => 'eureka-ajax')
    )
  );
}

/**
 * Callback function for /eureka/ajax.
 */
function ajax_request_callback() {
  if ($_POST['from_js']) {
    // Return the time if the from_js request para is set to true
    $response = array(
      'message' => t('It is approximately !date', array('!date' => date('M j Y h:i:s'))),
    );
    drupal_json_output($response);
    exit;
  } else {
    // Return an empty json array for incorrect request
    drupal_json_output(array());
    exit;
  }
}

 

Clear the cache after update the eureka_ajax.module. Then you will get Access denied when you access eureka/page or eureka/ajax.

The next thing is to configure the permissions such that some roles could access those callbacks. Go to admin/people/permissions/list and allow authenticated users to access the two new permissions.

 

Now only logged in users could enjoy the Ajax feature.

Done =)

Next: Drupal 7 – Simple Ajax implementation @ 3

Reference:

About these ads

2 thoughts on “Drupal 7 – Simple Ajax implementation @ 2”

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s