Skip to content

Instantly share code, notes, and snippets.

@jmolivas
Last active December 18, 2015 01:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jmolivas/5706224 to your computer and use it in GitHub Desktop.
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
<?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