home | career | drupal | java | mac | mysql | perl | php | scala | uml | unix

Drupal example source code file (node_access_test.module)

This example Drupal source code file (node_access_test.module) is included in the DevDaily.com "Drupal Source Code Warehouse" project. The intent of this project is to help you "Learn Drupal by Example".

PHP - Drupal tags/keywords

access, array, foreach, function, grants, li, nodes, output, php, result, return, test, view

The node_access_test.module Drupal example source code

<?php
// $Id: node_access_test.module,v 1.4 2010/11/20 04:33:56 webchick Exp $

/**
 * @file
 * Dummy module implementing node access related hooks to test API interaction
 * with the Node module. This module restricts view permission to those with
 * a special 'node test view' permission.
 */

/**
 * Implements hook_node_grants().
 */
function node_access_test_node_grants($account, $op) {
  $grants = array();
  if ($op == 'view' && user_access('node test view', $account)) {
    $grants['node_access_test'] = array(888);
  }
  if ($op == 'view' && $account->uid == variable_get('node_test_node_access_all_uid', 0)) {
    $grants['node_access_all'] = array(0);
  }
  return $grants;
}

/**
 * Implements hook_node_access_records().
 */
function node_access_test_node_access_records($node) {
  $grants = array();
  $grants[] = array(
    'realm' => 'node_access_test',
    'gid' => 888,
    'grant_view' => 1,
    'grant_update' => 0,
    'grant_delete' => 0,
    'priority' => 999,
    );

  return $grants;
}

/**
 * Implements hook_permission().
 *
 * Sets up permissions for this module.
 */
function node_access_test_permission() {
  return array('node test view' => array('title' => 'View content'));
}

/**
 * Implements hook_menu().
 *
 * Sets up a page that lists nodes.
 */
function node_access_test_menu() {
  $items = array();
  $items['node_access_test_page'] = array(
    'title' => 'Node access test',
    'page callback' => 'node_access_test_page',
    'access arguments' => array('access content'),
    'type' => MENU_SUGGESTED_ITEM,
  );
  $items['node_access_entity_test_page'] = array(
    'title' => 'Node access test',
    'page callback' => 'node_access_entity_test_page',
    'access arguments' => array('access content'),
    'type' => MENU_SUGGESTED_ITEM,
  );
  return $items;
}

/**
 * Page callback for node access test page.
 *
 * Page should say "No nodes" if there are no nodes, and "Yes, # nodes" (with
 * the number filled in) if there were nodes the user could access. Also, the
 * database query is shown, and a list of the node IDs, for debugging purposes.
 * And if there is a query exception, the page says "Exception" and gives the
 * error.
 */
function node_access_test_page() {
  $output = '';

  try {
    $query = db_select('node', 'mytab')
      ->fields('mytab');
    $query->addTag('node_access');
    $result = $query->execute()->fetchAll();

    if (count($result)) {
      $output .= '<p>Yes, ' . count($result) . ' nodes</p>';
      $output .= '<ul>';
      foreach ($result as $item) {
        $output .= '<li>' . $item->nid . '</li>';
      }
      $output .= '</ul>';
    }
    else {
      $output .= '<p>No nodes</p>';
    }

    $output .= '<p>' . ((string) $query ) . '</p>';
  }
  catch (Exception $e) {
    $output = '<p>Exception</p>';
    $output .= '<p>' . $e->getMessage() . '</p>';
  }

  return $output;
}

/**
 * Page callback for node access entity test page.
 *
 * Page should say "No nodes" if there are no nodes, and "Yes, # nodes" (with
 * the number filled in) if there were nodes the user could access. Also, the
 * database query is shown, and a list of the node IDs, for debugging purposes.
 * And if there is a query exception, the page says "Exception" and gives the
 * error.
 */
function node_access_entity_test_page() {
  $output = '';
  try {
    $query = new EntityFieldQuery;
    $result = $query->fieldCondition('body', 'value', 'A', 'STARTS_WITH')->execute();
    if (!empty($result['node'])) {
      $output .= '<p>Yes, ' . count($result['node']) . ' nodes</p>';
      $output .= '<ul>';
      foreach ($result['node'] as $nid => $v) {
        $output .= '<li>' . $nid . '</li>';
      }
      $output .= '</ul>';
    }
    else {
      $output .= '<p>No nodes</p>';
    }
  }
  catch (Exception $e) {
    $output = '<p>Exception</p>';
    $output .= '<p>' . $e->getMessage() . '</p>';
  }

  return $output;
}

Other Drupal examples (source code examples)

Here is a short list of links related to this Drupal node_access_test.module source code file:

new blog posts

"Drupal" is a registered trademark of Dries Buytaert.

my drupal tutorials and examples  

Copyright 1998-2016 Alvin Alexander, alvinalexander.com
All Rights Reserved.

Beginning in 2016, a portion of the proceeds from pages under the '/drupal-code-examples/' URI will be donated to charity.