Last active
December 18, 2015 01:49
-
-
Save jmolivas/5706521 to your computer and use it in GitHub Desktop.
Second attempt to add readonly attribute to entity fields using a Field Widget for textfields
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_field_widget_info() | |
*/ | |
function readonly_field_widget_info(){ | |
return array( | |
'readonly_textfield' => array( | |
'label' => t('Text field (Read Only)'), | |
'field types' => array('text'), | |
'settings' => array('size' => 60), | |
'behaviors' => array( | |
'multiple values' => FIELD_BEHAVIOR_DEFAULT, | |
'default value' => FIELD_BEHAVIOR_DEFAULT, | |
), | |
), | |
); | |
} | |
/** | |
* Implements hook_field_widget_form() | |
*/ | |
function readonly_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, | |
$items, $delta, $element) { | |
if ($instance['widget']['type'] == 'readonly_textfield') { | |
$element += array( | |
'#type' => 'textfield', | |
'#default_value' => isset($items[$delta]['value']) ? $items[$delta]['value'] : NULL, | |
'#attributes' => array('readonly' => 'readonly'), | |
); | |
} | |
return $element; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment