disable_field.module in Disable Field 7
Same filename and directory in other branches
Defines a checkbox field to disable by admin in Field Edit page.
File
disable_field.moduleView source
<?php
/**
* @file
* Defines a checkbox field to disable by admin in Field Edit page.
*/
/**
* Implements hook_permissions().
*/
function disable_field_permission() {
return array(
'disable textfield module' => array(
'title' => t('Disable TextField'),
'description' => t('Perform administration tasks for disable Textfield.'),
),
);
}
/**
* Implements hook_form_alter().
*
* Disabled the textfield in commerce product form.
* Adds checkbox in field edit page.
*/
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');
}
}
}
}
/**
* Implements hook_form_validate().
*/
function disable_field_disable_validate($form, &$form_state) {
// Checking User Access.
if (user_access("disable textfield module")) {
$add_disable = $form_state['values']['field']['add_disable']['add_disable'];
$edit_disable = $form_state['values']['field']['edit_disable']['edit_disable'];
$roles_add = $form_state['values']['field']['roles_add'];
$roles_edit = $form_state['values']['field']['roles_edit'];
// For add checkbox;
if (strlen($add_disable) == 11) {
if (empty($roles_add)) {
form_set_error("roles_add", t('Select Atleast one Role to disable add.'));
}
}
// For Edit checkbox.
if (strlen($edit_disable) == 12) {
if (empty($roles_edit)) {
form_set_error("roles_edit", t('Select Atleast one Role to disable edit.'));
}
}
}
}
/**
* Implements submit function.
*/
function disable_field_disable_submit($form, &$form_state) {
// Checking User Access.
if (user_access("disable textfield module")) {
$add_disable = $form_state['values']['field']['add_disable']['add_disable'];
$edit_disable = $form_state['values']['field']['edit_disable']['edit_disable'];
$field_name = $form_state['values']['instance']['field_name'];
$bundle_type = $form_state['values']['instance']['bundle'];
$entity_type = $form_state['values']['instance']['entity_type'];
$roles_add = $form_state['values']['field']['roles_add'];
$roles_edit = $form_state['values']['field']['roles_edit'];
if (!empty($roles_add)) {
$roles_add = array_keys($roles_add);
}
if (!empty($roles_edit)) {
$roles_edit = array_keys($roles_edit);
}
// Unchecked box.
if (strlen($add_disable) == 1) {
db_delete('disable_field')
->condition('entity_type', $entity_type)
->condition('bundle', $bundle_type)
->condition('field_name', $field_name)
->condition('disable_page', 'add_disable')
->execute();
}
if (strlen($edit_disable) == 1) {
db_delete('disable_field')
->condition('entity_type', $entity_type)
->condition('bundle', $bundle_type)
->condition('field_name', $field_name)
->condition('disable_page', 'edit_disable')
->execute();
}
// For add checkbox;
if (strlen($add_disable) == 11) {
db_merge('disable_field')
->key(array(
'entity_type' => $entity_type,
'bundle' => $bundle_type,
'field_name' => $field_name,
'page' => 'add',
'disable_page' => 'add_disable',
))
->fields(array(
'status' => 1,
'roles' => json_encode($roles_add),
))
->execute();
}
// For Edit checkbox.
if (strlen($edit_disable) == 12) {
db_merge('disable_field')
->key(array(
'entity_type' => $entity_type,
'bundle' => $bundle_type,
'field_name' => $field_name,
'page' => 'edit',
'disable_page' => 'edit_disable',
))
->fields(array(
'status' => 1,
'roles' => json_encode($roles_edit),
))
->execute();
}
}
}
/**
* Implements hook_form_node_form_alter().
*
* Disabled the textfield if checked in entity page.
*/
function disable_field_form_node_form_alter(&$form, &$form_state, $form_id) {
$node_type = $form['type']['#value'];
$data = field_info_instances("node", $node_type);
if (isset($form['#node']->nid)) {
$page = 'edit';
}
else {
$page = 'add';
}
// 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($node_type, $key, $page, 'node');
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;
}
}
}
}
/**
* Generate a count of status using all values.
*/
function disable_field_check_disable($node_type, $key, $page, $entity) {
$disable_query = db_select('disable_field', 'dt');
$disable_query
->fields('dt', array(
'roles',
));
$disable_query
->condition('dt.bundle', $node_type, '=');
$disable_query
->condition('dt.entity_type', $entity, '=');
$disable_query
->condition('dt.field_name', $key, '=');
$disable_query
->condition('dt.page', $page, '=');
$disable_result = $disable_query
->execute()
->fetchAll();
return $disable_result;
}
Functions
Name | Description |
---|---|
disable_field_check_disable | Generate a count of status using all values. |
disable_field_disable_submit | Implements submit function. |
disable_field_disable_validate | Implements hook_form_validate(). |
disable_field_form_alter | Implements hook_form_alter(). |
disable_field_form_node_form_alter | Implements hook_form_node_form_alter(). |
disable_field_permission | Implements hook_permissions(). |