Skip Navigation

Drupal - Restricting access to containers or categories

3 May 2007 17:26 by steve.temple

I've been using the Drupal Category Module for a website. Basically the site is structured so that the tree like navigation is built using containers, and when you are down to the actual content they are implemented as categories. The owner of the site want people to register for free on the site to view the content but to be able to still navigate through the site so you get an idea of what content is on the site first.

The simplest way to do this would be to restrict anonymous access to categories but still allow it for containers. Initially I've looked at cac_lite, but these are fairly complex, and do a lot more than I need, plus I couldn't get it to work correctly. So what I've done is modify the category module to include some more granular permissions.

First of all I added the new permissions to the category_perm function, modifying the existing function to look like this:

function category_perm() {
  $base_types = variable_get('category_base_nodetypes',
  array('category_cat' => 'category_cat',
  'category_cont' => 'category_cont'));

  $perms_list = array();

  if ($base_types['category_cat']) {
    $perms_list[] = 'create categories';
    $perms_list[] = 'access categories';
  }
  if ($base_types['category_cont']) {
    $perms_list[] = 'create containers';
    $perms_list[] = 'access containers';
  }
  if ($base_types['category_cat'] || $base_types['category_cont']) {
    $perms_list[] = 'edit all categories';
    $perms_list[] = 'edit own categories';
  }
  $perms_list[] = 'administer categories';

  return $perms_list;
}

This adds the permissions to the access control section, next we have to actually restrict access to the containers or categories. The module uses a helper function to get the permissions for access control called _category_privileged. I added these cases into that function, one for container and one for category:

case 'access categories':
   return user_access('access categories') || user_access('administer categories');
case 'access containers' :
   return user_access('access containers') || user_access('administer containers');

This will also allow users who can administer containers/categories access as well as users with our new permission.

Then the final step is to actually modify the category_access function to restrict access when viewing containers or categories. To do this I added a if statement to the end of the category_access function:

if ($op == 'view') {
   $type = $node->type;
   if ($type == 'category_cont') {
      return _category_privileged('access containers');
   } else {
      return _category_privileged('access categories');
   }
}

This simply checks the node type and then returns whether the user has been granted access to the relevant type. That should be it, you can then use the access control section to grant/deny permissions to view categories or containers.

Drupal - Adding content to the sidebar without using a block

1 May 2007 17:25 by steve.temple

When creating a new Drupal module, I'd been trying to add a couple of links to a sidebar when viewing a user's details. I could of done this using a block but that would rely upon the block being setup to only appear on the user's page, and would also include all the markup that comes with a block. This is how I got around all that.

First thing I did was include the code that will add any code set to appear in the sidebars actually into the sidebar. This is done by the _phptemplate_variables function in template.php in your skin's directory. I already had a _phptemplate_variables function so I just added a new case for page's in my existing function so it looked something like this:

 

function _phptemplate_variables($hook, $vars = array()) {
    switch ($hook) {
      case 'node':
        // removed for clarity
        break;
      case 'page':
        foreach (array('left','right') as $region) {
          $vars[$region] = drupal_get_content($region);
        }
        break;
    }
  
    return $vars;
}

All this does is make sure the code that the code is only run for pages and then get any content set for the region and add it to the relevant template variables, it then returns the variables. This is the code that does the business:

 
foreach (array('left','right') as $region) {
  $vars[$region] = drupal_get_content($region);
}

If you wanted to include more regions that just the left and right you could add those into the array. Now in your module you can use the drupal_set_content function to put content into either the left or right region like this:

 

$output = l('some link text','node/1234');
drupal_set_content('left', $output);

This link should now appear in the left sidebar whenever the function this is in is called. I included something similar in my hook_user for my module to include a link that will only appear on my user pages.

Customising/theming the search box in drupal

20 February 2007 17:30 by steve.temple

I've been doing a lot of work with drupal recently. The documentation is somewhat unreliable on their site. I've found quite a few instances where there has been code that doesn't work or at least now in my version of drupal. If you're having issues with customising the search box as demonstrated in this article, try using this code instead in your theme (search-box.tpl.php):

 

<label for="search_theme_form_keys">Search</label>
<input type="text" maxlength="128" name="search_theme_form_keys"
 id="edit-search_theme_form_keys"  size="25" value=""
 title="Enter the terms you wish to search for." class="form-text" />
<input type="submit" name="op" value="Search"  />
<input type="hidden" name="form_id" id="edit-search-theme-form" value="search_theme_form" />
<input type="hidden" name="form_token" id="a-unique-id"
 value="<?php print drupal_get_token('search_theme_form'); ?>" />