You are here

views_access_callback.module in Views Access Callback 6

Same filename and directory in other branches
  1. 8 views_access_callback.module
  2. 7 views_access_callback.module

File

views_access_callback.module
View source
<?php

/**
 * Implementation of hook_views_plugins().
 */
function views_access_callback_views_plugins() {
  return array(
    'access' => array(
      'parent' => array(
        'no ui' => TRUE,
        'handler' => 'views_plugin_access',
        'parent' => '',
      ),
      'views_access_callback' => array(
        'title' => t('Callback function'),
        'help' => t('Access will be granted to users based on result returned by callback function.'),
        'handler' => 'views_access_callback_plugin_access_callback',
        'uses options' => TRUE,
        'path' => drupal_get_path('module', 'views_access_callback'),
      ),
    ),
  );
}

/**
 * Intermediary access callback function getting view arguments
 * and calling real function specified in view definition.
 */
function views_access_callback_access_callback($view_name, $display_id, $access_callback) {
  global $user;

  // Init view.
  $view = views_get_view($view_name);
  $view
    ->set_display($display_id);
  $view
    ->init_handlers();

  // Find the values for any arguments embedded in the path via '%'.
  $i = 0;
  foreach (explode('/', $view->display_handler
    ->get_option('path')) as $element) {
    if ($element == '%') {
      $view->args[] = arg($i);
    }
    $i++;
  }

  // Now handle any implicit arguments from the end of the path.
  $num_arguments = count($view->argument);
  while (count($view->args) < $num_arguments) {
    $view->args[] = arg($i);
    $i++;
  }
  $result = FALSE;
  if (function_exists($access_callback)) {
    $result = call_user_func_array($access_callback, $view->args);
  }
  return $result;
}

Functions

Namesort descending Description
views_access_callback_access_callback Intermediary access callback function getting view arguments and calling real function specified in view definition.
views_access_callback_views_plugins Implementation of hook_views_plugins().