View source
<?php
module_load_include('inc', 'required_by_role', 'required_by_role.date');
function required_by_role_help($path, $arg) {
switch ($path) {
case 'admin/help#required_by_role':
$output = '';
$output .= '<p>' . t('The Required by role module provides an easy and flexible way for developers and site builders to determine whether a field (instance) should be <em>required</em> by all users or just for selected user roles.') . '</p>';
return $output;
}
}
function required_by_role_field_info_alter(&$info) {
foreach ($info as $field_type => $field_type_info) {
$info[$field_type]['instance_settings'] += array(
'required_by_role' => array(),
);
}
}
function required_by_role_field_widget_form_alter(&$element, &$form_state, $context) {
if (!$context['instance']['required']) {
global $user;
$account = $user;
$settings = $context['instance']['settings'];
$is_required = required_by_role_is_required($account, $settings, $context, $form_state);
_required_by_role_set_property($element, $is_required, $context, $form_state);
drupal_alter('required_property', $element, $field, $is_required);
}
}
function _required_by_role_set_property(&$element, $is_required, $context, $form_state) {
if ($context['delta'] === 0) {
if (isset($element[0]['#required'])) {
$element =& $element[0];
}
if (isset($element['value']['#required'])) {
$element =& $element['value'];
}
if (isset($element[0]['value']['#required'])) {
$element =& $element[0]['value'];
}
if (isset($element['email']['#required'])) {
$element =& $element['email'];
}
$element['#required'] = $is_required;
if ($element['#type'] == 'select') {
$element = form_process_select($element);
}
}
}
function required_by_role_is_required($account, $settings, $context, $form_state) {
$is_required = NULL;
$roles = $settings['required_by_role'] ? $settings['required_by_role'] : array();
$required_callback = _required_by_role_get_callback();
if (is_callable($required_callback)) {
$is_required = $required_callback($account, $roles, $context, $form_state);
}
return $is_required;
}
function _required_by_role_get_callback() {
$required_callback = module_invoke('required_by_role', 'required_by_role_callback');
drupal_alter('required_by_role_callback', $required_callback);
return $required_callback;
}
function required_by_role_required_by_role_callback() {
return '_required_by_role_roles_intersect';
}
function _required_by_role_roles_intersect($account, $roles, $context, $form_state) {
$match = array_intersect(array_keys($account->roles), $roles);
$is_required = !empty($match);
return $is_required;
}
function required_by_role_form_field_ui_field_edit_form_alter(&$form, $form_state) {
$field_name = $form['#field']['field_name'];
$required_by_role = $form['#instance']['settings']['required_by_role'];
$label = t('Required by role');
$description = t('Select this option if you want this field to be required for ALL ROLES, otherwise choose specific roles in the checkboxes, below, under %name', array(
'%name' => 'Required by role',
));
$roles = user_roles();
unset($roles[DRUPAL_AUTHENTICATED_RID]);
$header = array(
'name' => t('Role'),
);
foreach ($roles as $rid => $role) {
$options[$rid] = array(
'name' => $role,
);
}
$module_path = drupal_get_path('module', 'required_by_role');
$attached = array(
'js' => array(
$module_path . '/required_by_role.js',
),
);
$form['instance']['settings']['required_by_role'] = array(
'#prefix' => '<label>' . $label . '</label>',
'#type' => 'tableselect',
'#header' => $header,
'#options' => $options,
'#default_value' => $required_by_role,
'#weight' => $form['instance']['required']['#weight'] + 1,
'#attached' => $attached,
'#empty' => t('No roles available.'),
'#attributes' => array(
'class' => array(
'tableselect-required-by-role',
),
),
);
$form['instance']['required']['#description'] = $description;
$default_value_widget =& $form['instance']['default_value_widget'][$field_name][LANGUAGE_NONE];
if (isset($default_value_widget[0]['#required'])) {
$default_value_widget[0]['#required'] = FALSE;
}
else {
$default_value_widget['#required'] = FALSE;
}
if (isset($default_value_widget[0]['value']['#required'])) {
$default_value_widget[0]['value']['#required'] = FALSE;
}
}