0

Should be pretty straightforward but I am kinda stuck. I am prepopulating a mandatory field's value according to the URL, so I want to prevent the field from showing up, but not in a CSS level.

hide() and #access do not seem to submit the field leading to a validation error. Any clues on how would I go about doing this?

P.S. not a field of #type => hidden. An actual field. A select list if you want.

Comment trail clarifications:
1) the field is an entity field, a user field
2) the field is mandatory
3) the field is prepopulated using #value and #default_value (for safety (?))
4) the field should not be either visible or editable
5) I'd like to avoid adding a hidden and/or value field myself and write another submission/validation function since the user_register one works just fine. I want to use the value that the field has upon the form's rendering taking into consideration point 4.

9
  • Just to clarify, you don't want the input to be a hidden element, you don't want it to show on the form, but you want it to be pre-populated from a URL parameter? You're going to have to use CSS I think. Or implement your own pre-populate functionality which converts the relevant field to #type => value on the backend, so it never has to be displayed on the form itself – Clive Feb 13 '13 at 15:37
  • Yes, the element is a select list, it's prepopulated, but I do not want to show it. If I resign to css, is there any chance I can somehow prevent the user from changing the value? – magtak Feb 13 '13 at 15:38
  • 2
    If you want to stop the user changing the value then it shouldn't be a select list, you should just convert it to a #type => value in your form definition/form alter. That way it will be available server side, can be pre-populated in your form logic, and won't show to the user. Or if you're not bothered about doing it 'properly' just add #disabled => TRUE to the element and hide it with CSS – Clive Feb 13 '13 at 15:39
  • Hold on, testing. – magtak Feb 13 '13 at 15:40
  • 1
    Yes, it needs to be in a form alter. If it's a field as in an entity-field, you'll also need to make sure you're targeting the actual html element, and not the language or delta wrapper...(see drupal.stackexchange.com/questions/27767 for context) – Clive Feb 13 '13 at 15:52
2

Since Drupal 7, Field API is in core so you don't have to worry about Fields when editing a node form. In Drupal 6, you have to add an #after_build function to do this.

You need to create a hook_form_alter function to alter the user_register_form. (or hook_form_FORM_ID_alter).

I can't remember the structure of form fields. Install devel module and you can see the entire $form array nicely.

 <?php
    function mymodule_form_user_register_form_alter(&$form, &$form_state) {
      dpm($form); // Make sure you have Devel module installed. 
      $field_name = 'field_myfield';
      $value = $_GET['somekey']; // populate this var from the URL params as you mentioned in your question. 

      if (isset($form[$field_name][LANGUAGE_NONE][0])) {
        if (empty($value)) {
          $form['#access'] = FALSE;
          drupal_set_message(t('Please use correct URL'), 'warning'); // in case the value is not set, hide the form and issue a warning. 
          return;
        }
        // #options is an array like this: array('key' => 'value', 'key2' => 'value2'); 
        // Make sure you set the key of this array in $value. if your $value is a value, you'll need to array_flip and find the key instead. 
        $form[$field_name][LANGUAGE_NONE][0]['#type'] = 'value'; // this element will not be rendered at all
        $form[$field_name][LANGUAGE_NONE][0]['#value'] = $value; // this value will be passed to $form_state['values']
      }
    }

Calling the form using drupal_get_form DOES fire all form_alters as necessary. If you are trying to use this form with a direct menu router, set page callback in the menu router item to drupal_get_form and array('user_register_form'), as the page arguments

1
  • see my answer for what actually worked – magtak Feb 13 '13 at 16:35
0

For some bizzare reason, both

  $form['field_fieldname'][LANGUAGE_NONE]['#disabled'] = true;
  $form['field_fieldname'][LANGUAGE_NONE]['#type'] = 'value';

needed to be done, both in the hook_form_alter.

Doing the same work within the page callback:

$form = drupal_get_form('form_id');
//same thing
$form['field_fieldname'][LANGUAGE_NONE]['#disabled'] = true;
$form['field_fieldname'][LANGUAGE_NONE]['#type'] = 'value';
return $form;

Does not work. The field is a select list.

1
  • If anyone, can tell me why those 2 lines work in hook_form_alter, but do not work when I edit the renderable form's array I'd be greatful. – magtak Feb 15 '13 at 9:22

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.