domain_source_form_alter(&$form, &$form_state, $form_id)Implement hook_form_alter()
domain_source/domain_source.module, line 21
<?php
function domain_source_form_alter(&$form, &$form_state, $form_id) {
// Apply to all node editing forms, but make sure we are not on the CCK field configuration form.
// See http://drupal.org/node/186624.
if ($form['#id'] == 'node-form' && !isset($form['#node']->cck_dummy_node_form)) {
global $_domain, $user;
if (!empty($form['#node']->nid)) {
$default_source = db_result(db_query("SELECT domain_id FROM {domain_source} WHERE nid = %d", $form['#node']->nid));
}
if (!isset($default_source)) {
$default_source = $_domain['domain_id'];
}
// Prevent invalid domains from being used.
$lookup = domain_lookup($default_source);
if ($default_source != DOMAIN_SOURCE_USE_ACTIVE && empty($lookup['valid'])) {
$default_source = NULL;
}
$account = $user;
domain_get_user_domains($account);
// Only uses with 'set domain access' can assign content to all affilaites, so they get a new option.
// This option allows domain source to be ignored on a per-node basis.
$options = array();
if (user_access('set domain access')) {
$options[DOMAIN_SOURCE_USE_ACTIVE] = t('Use active domain');
}
$domains = domain_domains();
$show = FALSE;
if (user_access('set domain access')) {
$show = TRUE;
foreach ($domains as $domain) {
if ($domain['valid']) {
$options[$domain['domain_id']] = $domain['sitename'];
}
}
}
// In the case of users with limited permissions, option 3 is the "show options" value.
else if (user_access('publish to any assigned domain') && !empty($account->domain_user)) {
$show = FALSE;
// Get the user's allowed domains.
foreach ($domains as $domain) {
$key = $domain['domain_id'];
if ($domain['domain_id'] == 0) {
$key = -1;
}
if ($domain['valid'] && $user->domain_user[$key] != 0) {
$options[$key] = $domain['sitename'];
}
}
// Is this node assigned to a source that the user can control?
if (isset($form['#node']->domain_source)) {
// Transform the 0 to -1 for lookups.
$source = ($form['#node']->domain_source == 0) ? -1 : $form['#node']->domain_source;
}
else {
$source = NULL;
$show = TRUE;
}
if (!is_null($source) && isset($account->domain_user[$source])) {
if ($account->domain_user[$source] == $source) {
$show = TRUE;
}
else {
$name = ($source != -5) ? $domains[$source]['sitename'] : t('the active domain');
$form['domain']['domain_source_notes'] = array(
'#value' => '<label><b>'. t('Source domain:') .'</b></label><div class="description">'.
t('This content is assigned to %domain and cannot be reassigned.', array('%domain' => $name)) .'</div>',
);
}
}
}
// Determine how to show the form element.
if ($show) {
$form['domain']['domain_source'] = array(
'#type' => 'select',
'#title' => t('Source domain'),
'#options' => $options,
'#default_value' => $default_source,
'#description' => t('This affiliate will be used to write links to this content from other affiliates, as needed.')
);
}
// Non-privileged users just have the current domain assigned.
else {
$form['domain']['domain_source'] = array(
'#type' => 'value',
'#value' => $default_source,
);
}
}
// Integration with Domain Content
else if ($form_id == 'domain_content_form') {
global $_domain;
$options = array();
$domains = domain_domains();
if (user_access('set domain access')) {
foreach ($domains as $domain) {
$options[$domain['domain_id']] = $domain['sitename'];
}
$form['domain']['domain_source'] = array(
'#type' => 'select',
'#title' => t('Source domain'),
'#options' => $options,
'#default_value' => $_domain['domain_id'],
'#description' => t('This affiliate will be linked to from other affiliates, as needed.')
);
$form['#validate'][] = 'domain_source_validate';
$form['#submit'][] = 'domain_source_update_nodes';
}
}
}
?>