Restricting content types per domain with Domain Access

UPDATE: With the help of Jbomb on drupal.org we have taken my method and turned it into a proper module which can be downloaded from drupal.org The module is called Domain Content Types

The domain access module is a great tool to create affiliated sites without the hassle of multi-site. However one severely lacking feature in the 6.x branch which will not be fixed is the ability to restrict which content types can be posted to a domain.

By creating a small module we can restrict the access to content types on a per domain basis. There are two areas to consider when restricting a content type

Restricting a content type from being created while in a domain

In order to restrict a content type from being added we have to use a hook_menu_alter to change the permission based on the domain the user is in. In this example I am making it so that the "story" content type can't be added for a certain domain.

<?php
function mymodule_menu_alter(&$items) {
 
$items["node/add/story"]['access callback'] = 'mymodule_domain_check';
 
$items["node/add/story"]['access arguments'] = array('story');
}
?>

Next we create the mymodule_domain_check function

<?php
function mymodule_domain_check($content_type) {

  global

$_domain;
 
$id = $_domain['domain_id'];

  if (

user_access("create $content_type content")) { // Checks if the user has permission to create the content type in the first place
   
if ($content_type == "story" && $id == 1) { // replace with the id of the domain you wish to exclude
     
return FALSE;
    }
    return
TRUE;
  } else {
    return
FALSE;
  }
}
?>

Be sure to flush your menu cache when making changes to hook_menu_alter!

The basic idea is that I check if the user has access to the add a story, if they do then we check which domain they are in, if it's a domain we don't want them to create stories in we return false, otherwise we can return true. The ID value of a domain can be determined by looking at the url of the domain edit links on the domain configuration pages.

So now if a user is on domain 1 and tries to add a story they will get an access denied, but on other domains they will be able to add them no problem. This method will even remove the content type from the /node/add page and any other menu items that refer to /node/add/story for that domain.

Removing the domain in the affiliate publishing options for a content type

We have already prevented a user from adding stories on the domain of our choice, but if a user can publish to more than one domain we also want to remove that domain from the publishing options of that content type. To do so we need to perform a hook_form_alter.

<?php
function mymodule_form_alter(&$form, &$form_state, $form_id) {   
  if (
$form['type']['#value'] == 'story') {
    unset(
$form['domain']['domains']['#options'][1]);
  }
}
?>

Here we unset the option in the node add/edit form to remove the domain 1 for the story content type. So that users can't publish the story content type on domain 1. You may need to use dsm or print_r on the $form object to see exactly which #options item you want to remove.

So this is how I've used hook_form_alter and hook_menu_alter to not allow content types on certain domains. The only situation this doesn't address is if a user has access to the "send to all affiliates" checkbox. But I have disabled that on my sites as I don't think it's an option that many people should have.

Tags: