function disable_field_form_alter in Disable Field 7
Same name and namespace in other branches
- 8 disable_field.module \disable_field_form_alter()
Implements hook_form_alter().
Disabled the textfield in commerce product form. Adds checkbox in field edit page.
File
- ./
disable_field.module, line 26 - Defines a checkbox field to disable by admin in Field Edit page.
Code
function disable_field_form_alter(&$form, &$form_state, $form_id) {
// In commerce product page.
if (strstr($form_id, 'commerce_product_ui_product_form')) {
$bundle_type = $form['#bundle'];
// Get all the fields.
$data = field_info_instances("commerce_product", $bundle_type);
$entity_type = $form['#entity_type'];
// Check pages(s) , donot using $_GET.
if (strpos($form['#action'], '/add/') !== FALSE) {
$page = 'add';
}
if (strpos($form['#action'], '/edit') !== FALSE) {
$page = 'edit';
}
// Check DB and disable field that checked by admin.
$user_roles = array_keys($GLOBALS['user']->roles);
foreach (array_keys($data) as $key) {
$check = disable_field_check_disable($bundle_type, $key, $page, $entity_type);
foreach ($check as $disable) {
$roles = json_decode($disable->roles);
$disable_check = array_diff($roles, $user_roles);
if (count($disable_check) < count($roles)) {
$form[$key]['#disabled'] = TRUE;
}
}
}
}
// Checking User Access.
if (user_access("disable textfield module")) {
// Add checkbox field in edit page of each entity(if not locked).
if ($form_id == 'field_ui_field_edit_form') {
// Check for default locked fields.
if (!isset($form['locked'])) {
$field_name = $form['#instance']['field_name'];
$bundle_type = $form['#instance']['bundle'];
$entity_type = $form['#instance']['entity_type'];
// Checking DB to set default Values.
$disable_query = db_select('disable_field', 'dt');
$disable_query
->fields('dt', array(
'page',
'roles',
));
$disable_query
->condition('dt.bundle', $bundle_type, '=');
$disable_query
->condition('dt.entity_type', $entity_type, '=');
$disable_query
->condition('dt.field_name', $field_name, '=');
$disable_result = $disable_query
->execute()
->fetchAll();
// Fetch Roles except anonymous.
$option = user_roles($membersonly = TRUE, $permission = NULL);
$form['#attached']['js'] = array(
drupal_get_path('module', 'disable_field') . '/js/disable_field.js',
);
$form['field']['add_disable'] = array(
'#type' => 'checkboxes',
'#options' => array(
'add_disable' => t('Disable this field in add content form'),
),
'#title' => t('Do you want to make this field disable?'),
'#weight' => 1,
);
$form['field']['roles_add'] = array(
'#type' => 'select',
'#options' => $option,
'#title' => t('Roles for add form.'),
'#multiple' => TRUE,
'#weight' => 2,
);
$form['field']['edit_disable'] = array(
'#type' => 'checkboxes',
'#options' => array(
'edit_disable' => t('Disable this field in edit content form'),
),
'#weight' => 3,
);
$form['field']['roles_edit'] = array(
'#type' => 'select',
'#options' => $option,
'#title' => t('Roles for edit form.'),
'#multiple' => TRUE,
'#weight' => 4,
);
// Default Values if already set.
if (!empty($disable_result)) {
foreach ($disable_result as $result) {
if ($result->page == 'add') {
$form['field']['add_disable']['#default_value'] = array(
'add_disable',
);
$add_roles = json_decode($result->roles);
$form['field']['roles_add']['#default_value'] = $add_roles;
}
if ($result->page == 'edit') {
$form['field']['edit_disable']['#default_value'] = array(
'edit_disable',
);
$edit_roles = json_decode($result->roles);
$form['field']['roles_edit']['#default_value'] = $edit_roles;
}
}
}
// Unshift default submit to call after below submit.
array_unshift($form['#submit'], 'disable_field_disable_submit');
array_unshift($form['#validate'], 'disable_field_disable_validate');
}
}
}
}