Skip to content

Instantly share code, notes, and snippets.

@jmolivas
Last active December 18, 2015 01:49
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/5706521 to your computer and use it in GitHub Desktop.
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
<?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