0

A textfield with #disabled = TRUE will appear greyed out, and be unchangeable.

A textfield with #input = FALSE will not send its value to $form_state['values'], and will be ignored by system_settings_form_submit().

Unfortunately, a textfield with #disabled = TRUE and #input = FALSE no longer appears greyed out.

I want to make a textfield that is purely for display purposes, so a person can copy a value from there. What is the best way to do this?

EDIT I: What I tried so far

To clarify, my starting point is code like this:

$form['thetext'] = [
  '#type' => 'textfield',
  '#title' => t('The text'),
  '#disabled' => true,  // This should make it greyed out - but it doesn't due to #input = false below.
  '#input' => false,  // This should make this field ignored in $form_state['values'].
  '#value' => 'xyz',  // Either #value or #default_value.
];

EDIT II:

If this wasn't clear before: I really want the html output to be a textfield element. I also want to use the regular form + theme API, so that the element gets a label and wrapper div and theming like other form elements in the same form. E.g. if I would just hardcode the textfield html, it would no longer have the same classes, wrappers etc.

3
  • One might (quite reasonably) argue that from a semantics point of view, you shouldn't be using an <input> if you don't want, well, input :) Output is arguably better left to tags/structures that suit that were designed for it. Take Digital Ocean as an example: when you hover over stuff that's useful to be copied, but can't be edited, a "copy" link appears to the right. Implementing something like that would be more pretty simple (a class and a few lines of JS), and it might, again arguably, be more idiomatic and better UI/X
    – Clive
    Nov 13, 2017 at 16:33
  • You have a point there.. but I somehow doubt that the reason why #input = false and #disabled = true don't combine well is a conscious design decision from Drupal developers :)
    – donquixote
    Nov 14, 2017 at 7:14
  • I think a good answer would start with your caveat about design considerations, then explain why #input = false seems to nullify #disabled = true, then show how it can be done despite this. Maybe by explicitly setting something in #attributes.
    – donquixote
    Nov 14, 2017 at 12:46

2 Answers 2

2

Use a markup element instead.

$form['element'] = array(
    '#type' => 'markup',
    '#markup' => 'This is the thing you want people to see. You can use <strong>html</strong>, too.',
);

You don't actually have to specify #type => 'markup' because drupal defaults to that type if you don't specify, but I do anyway as it seems to be good practice.

4
  • But this does not appear as a text field. Of course I could hardcode the html into the #markup, but then it can no longer be themed..
    – donquixote
    Nov 13, 2017 at 15:40
  • Ah, well in that case I suppose you could just do '#default_value' => 'your value' for a regular text field. You don't have to disable it, just don't do anything with the value in the submit function.
    – pixel5
    Nov 13, 2017 at 15:56
  • But I want it to appear greyed out. And I want the input to be ignored, e.g. if I use system_settings_form() instead of a custom submit function. Of course I could add an additional submit handler that would unset the value form $form_state['values'].. but the goal was not to do that :)
    – donquixote
    Nov 13, 2017 at 15:58
  • That's a pretty specific idea... If I were doing it, I would probably use the markup type form element to code it manually so that it has the appearance of a textfield but isn't in $form or $form_state as such.
    – pixel5
    Nov 13, 2017 at 16:12
0

input = FALSE is redundant if #disabled = TRUE. Whatever value is in the field will already be ignored if #disabled = TRUE.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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