Last active
December 18, 2015 01:48
-
-
Save jmolivas/5706224 to your computer and use it in GitHub Desktop.
First attempt to add readonly attribute to entity fields using after_build callback
This file contains 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 | |
/** | |
* Implements hook_form_alter(). | |
*/ | |
function readonly_form_alter(&$form, &$form_state, $form_id) { | |
// TODO set form using UI | |
$readonly_field_form_id = 'ENTITY_NAME_node_form'; | |
// TODO set fields using UI | |
$readonly_fields = array('FIELD_NAME_FOO','FIELD_NAME_BAR'); | |
// TODO get language from form, also set defaul language instead of und | |
$language = 'und'; | |
if ($readonly_field_form_id==$form_id) { | |
foreach ($readonly_fields as $readonly_field) { | |
if (isset($form[$readonly_field][$language][0])) { | |
$form[$readonly_field][$language][0]['#after_build'][] = | |
'readonly_after_build_element_readonly'; | |
} | |
} | |
} | |
} | |
/** | |
* Process after_build | |
* Make element readonly | |
* #after_build callback for field elements in ENTITY_NAME_node_form. | |
*/ | |
function readonly_after_build_element_readonly($element, &$form_state) { | |
//Add attributes array if not set | |
if (!isset($elements['value']['#attributes'])) { | |
$elements['#attributes'] = array(); | |
} | |
$element['value']['#attributes']['readonly'] = 'readonly'; | |
return $element; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment