domain_toggle
| Project: | Domain Access |
| Version: | 6.x-1.2 |
| Component: | Code |
| Category: | feature request |
| Priority: | normal |
| Assigned: | falcon |
| Status: | closed |
I have a modified version of domain_strict where administrators can choose if a subdomain shall be open og closed. In closed subdomains users must be logged in to view content(domain_strict rules apply). In open subdomains anonymous users can view content(normal domain access rules apply.)
The code:
<?php
// $id$
/**
* @file
* Introduces open and private domains. In private domains users are forced to be assigned to a
* domain in order to view content on that domain. In open domains everyone can view the content.
*/
/**
* Implementation of hook_domainconf()
*
* Adding a checkbox to open and close the domain on the domain settings page.
*/
function domain_toggle_domainconf() {
global $_domain;
$res = db_query("SELECT open FROM {domain} WHERE domain_id = %d", $_domain['domain_id']);
$def_value = db_fetch_object($res)->open;
$form['open_domain'] = array(
'#type' => 'checkbox',
'#title' => t('Open'),
'#default_value' => $def_value,
'#description' => t('If content on this page shall be open for all users?')
);
$form['#submit'][] = 'domain_toggle_toggle_submit';
return $form;
}
/**
* Handles the open checkbox in the domain settings form.
*
* @param $form - copy of the form array
* @param $form_state - link to the form state array
*/
function domain_toggle_toggle_submit($form, &$form_state) {
global $_domain;
$sql = "UPDATE {domain} SET open = '%s' WHERE domain_id = %d";
db_query($sql, $form_state['values']['open_domain'], $_domain['domain_id']);
}
/**
* Implementation of hook_domaingrants()
*
* In Domain Toggle, we only let users see content on open domains and private domains that
* they are registered with. So we check the $user object in order
* to set our grants rather than using the default module grants.
*
* @ingroup strict
*/
function domain_toggle_domaingrants(&$grants, $account, $op) {
global $_domain;
$res = db_query("SELECT open FROM {domain} WHERE domain_id = %d", $_domain['domain_id']);
$open = db_fetch_object($res)->open;
if ($open == '1') {
return;
}
// Code from the domain strict module:
// Erase the default domain_id grants.
unset($grants['domain_id']);
$domains = (isset($account->domain_user)) ? $account->domain_user : array();
if (!empty($domains)) {
foreach ($domains as $key => $value) {
// The -1 is the root domain, since 0 cannot be stored by checkboxes.
($value == -1) ? $id = 0 : $id = $value;
// If the user has access to the current domain, set that grant.
if (abs($value) > 0 && $id == $_domain['domain_id']) {
$grants['domain_id'][] = $id;
}
}
}
}
Install file:
<?php
// $Id$
/**
* @file
* Install file.
*/
/**
* Implements hook_install()
*/
function domain_toggle_install() {
$status = array();
db_add_field($status, "domain", "open", array('type' => 'varchar', 'length' => 1, 'not null' => true, 'default' => '0'));
}
/**
* Implements hook_uninstall()
*/
function domain_toggle_uninstall() {
$res = array();
db_drop_field($res, "domain", "open");
}
.info file:
; $id$
name = Domain Toggle
description = Introduces open and private domains. In private domains users are forced to be assigned to a domain in order to view content on that domain. In open domains everyone can view the content.
package = Domain Access
dependencies[] = domain_conf
core = 6.x

#1
If this functionality doesn't already exist it would be nice if it could be included in DA somehow.
#2
This sort of functionality should be released as a stand-alone module.
Some notes:
1) You don't want to use hook_domainconf() for this, as that introduces a dependency for the Domain Conf module. Better to use hook_form_alter on the domain_edit form to add your setting to the domain creation/editing screen.
2) Create a separate table for your storage. I particularly don't like the add/drop column piece.
3) When using a separate table, remember to use hook_domainupdate.
4) Use hook_domainview() to add this data to the Domain list table.
No new features are going in to 6.x.1.2. If you want a proper patch, file against HEAD. But this is special usage that no one has requested, so I think you should release it separately.
#3
Thank you for the notes. I have tried to implement the changes you suggested in the module. Module is attached as .txt files.
Will try to release it as a stand-alone module as you suggested.
#4
Looks good, but I have not tested.
/me loves it when the API allows this kind of thing.
#5
Yes, the API is great!
I finally got my cvs application approved. domain_toggle is now released.
#6
I will add to the front page
#7
Automatically closed -- issue fixed for 2 weeks with no activity.