I've got a form I want to be disabled when certain criteria are met. According to the Form API I thought $form['#access'] = FALSE; would be sufficient. But no dice.

Instead, I'm doing this, which feels wrong, plus I get an error, "Warning: implode(): Invalid arguments passed in form_builder()"

foreach($form as $key => $element){
$form['#prefix'] = 'Bad user. Don\'t access this form!';

Kind of a big stick approach. What's the right way to do this?


Drupal doesn't cope with the form being removed in such a way because it is expecting certain things in the form array after you have altered it.

That's why you are getting that error, because form_builder() does this:

if (!isset($element['#id'])) {
  $element['#id'] = drupal_html_id('edit-' . implode('-', $element['#parents']));

but you have removed $element['#parents'] when you looped through unsetting everything, including #parents.

If you want to loop through all form elements are remove them the correct way would be to use element_children(), which will loop through the keys of child elements but skip properties of the current element.

So to loop though child elements you would do this:

// Remove all elements from this form.
foreach (element_children($form) as $key) {

Alternatively you could restrict access like this, which could cause you less side effects if other modules are expecting form elements that you have deleted.

// Restrict access to all elements in this form.
foreach (element_children($form) as $key) {
  $form[$key]['#access'] = FALSE;

If the form is a page though, not in a block or something, then it might be better to use hook_menu_alter() to block it as Shawn said in his answer.


There are many solutions here. One solution is to just disable the submission button:

$form['actions']['submit']['#disabled'] = true;
$form['#prefix'] = 'Bad user. Don\\'t access this form!';

A more elegant way might be to just disable access to the form altogether. For example, using hook_menu_alter to set the form page callback to redirect away from the form.

  • None of those work for me. If you disable the submit button, then the form is still visible to the user. Why leave a form that is not functional? Bad practice. My form is embedded on a node that needs to be accessed, so redirect doesn't work, either. – Jeremy John Jun 4 '15 at 14:56
  • 1
    Just a one option depending on different needs. The way I read it originally made me think it was an isolated form, not a part of the node edit form. – Shawn Conn Jun 4 '15 at 21:44
  • Word. After refactoring and using drupal_static for efficiency, it became practical to just disable the form based on logic surrounding the form. So basically I'm saying you were right. – Jeremy John Jun 5 '15 at 20:05

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.