1

I am building a custom module to set default values for entity reference fields. To avoid overwriting these defaults, i would like to set the field readonly. While I got the defaults to work, setting it readonly does not work, the field is still editable. Here is my code :

function prepopulate_references_form_alter(&$form, $form_state, $form_id) {
  $query = new EntityFieldQuery();
  $query
    ->entityCondition('entity_type', 'node')
    ->entityCondition('bundle', 'producttype')
  $result = $query->execute();
  $refobject_ids = array_keys($result['node']);
  // the next line works great:
  $form['field_testreference'][LANGUAGE_NONE]['#default_value'] = $refobject_ids;
  // this line doesn't have any effect:
  $form['field_testreference'][LANGUAGE_NONE]['#attributes']['readonly'] = TRUE;
  // ... and this one neither:
  $form['field_testreference'][LANGUAGE_NONE]['#attributes']['disabled'] = TRUE;

What is the problem here?

Edited: As Darvanen suggested, dpm() shows that $form['field_testreference']['#access'] = FALSE; works great.

1

Perhaps it's because you're referencing a language where it isn't necessary, try one of these:

$form['field_testreference']['#access'] = FALSE;

or

$form['field_testreference']['#attributes']['readonly'] = TRUE;
  • Your code does not work neither ... maybe the problem is a wrong nested array? – Madam Adam Nov 11 '14 at 0:38
  • Have you checked dpm($form) to see if you've got the arrays right? Mine usually have [0] in place of [LANGUAGE_NONE]... – Darvanen Nov 11 '14 at 3:23
  • Actually they don't have that key at all, edited answer – Darvanen Nov 11 '14 at 3:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.