Last active
August 29, 2015 14:18
-
-
Save mootari/8f670453a3fe7b086893 to your computer and use it in GitHub Desktop.
Example on how to prevent reordering of paragraphs field forms (and other forms) #drupal #form-api #tabledrag
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Removes item sort/add/remove capabilities. | |
* | |
* When called on an element during hook_field_attach_form(), this function | |
* ensures that no item can be added, removed or reordered. | |
* Currently adjusted to paragraphs. The core MV widget may required different | |
* button keys. | |
*/ | |
function EXAMPLE_lock_paragraphs_multiple_value_form(&$element) { | |
if(empty($element[$element['#language']])) { | |
// For safety only. This case might never happen. | |
return; | |
} | |
$items = &$element[$element['#language']]; | |
foreach(element_children($items) as $delta) { | |
// Disables the remove button, preventing the item from getting deleted. | |
if(isset($items[$delta]['actions']['remove_button'])) { | |
$items[$delta]['actions']['remove_button']['#access'] = false; | |
} | |
// Convert weight elements to hidden, to retain their value in | |
// $form_state['input'] without allowing user chances. | |
if(isset($items[$delta]['_weight'])) { | |
$items[$delta]['_weight'] = array( | |
'#type' => 'hidden', | |
'#value' => $items[$delta]['_weight']['#default_value'] | |
// Add back original keys, in case other modules rely on them. | |
) + $items[$delta]['_weight']; | |
} | |
} | |
// Disable the add_more button. This prevents additional items from being | |
// added. | |
if(isset($items['add_more'])) { | |
$items['add_more']['#access'] = false; | |
} | |
$items['#post_render'][] = 'EXAMPLE_disable_tabledrag'; | |
} | |
/** | |
* Disables tableDrag by removing related JS settings. | |
* | |
* If this function has been specified as #post_render callback on an element, | |
* it is fairly safe to assume that the last call to drupal_add_js() added | |
* tableDrag related settings. This function identifies these settings via | |
* the generated table id and removes them. | |
*/ | |
function EXAMPLE_disable_tabledrag($element, $children) { | |
$html_ids = drupal_static('drupal_html_id'); | |
// Per reference, as we might remove an entry | |
$js = &drupal_static('drupal_add_js'); | |
if(!empty($js['settings']['data'])) { | |
// Fetch the latest js settings entry | |
$data_last = end($js['settings']['data']); | |
// The table ID created by theme_multiple_value_form | |
$table_id = $children['#field_name'] . '_values'; | |
// Apply sanitizing from drupal_html_id(). Not very DRY, but it's not | |
// like we have a choice here. | |
$table_id = strtr(drupal_strtolower($table_id), array(' ' => '-', '_' => '-', '[' => '-', ']' => '')); | |
$table_id = preg_replace('/[^A-Za-z0-9\-_]/', '', $table_id); | |
$table_id = preg_replace('/\-+/', '-', $table_id); | |
// Verify that the html id actually exists, and append a counter if needed. | |
if(isset($html_ids[$table_id])) { | |
$table_id = $html_ids[$table_id] === 1 ? $table_id : "$table_id--" . $html_ids[$table_id]; | |
// Finally check if we guessed right and there are tableDrag settings for | |
// our table ID. | |
if(isset($data_last['tableDrag'][$table_id])) { | |
// ... and get rid of them. | |
array_pop($js['settings']['data']); | |
} | |
} | |
} | |
return $element; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment