How To Remove Format Options / Guideliness From Comments Textarea in Drupal 7

11 Jul · by Tim Kamanin · 2 min read

The task is very common, but in Drupal 7 you do this in a bit different way than in Drupal 6. Here's how to remove format options from comments textarea in Drupal 7: UPD: Well, the most easiest way to do this is to install Better Formats module (http://drupal.org/project/better_formats). But do we need another module installed to do this simple thing? Me, not, so let 's dig into the code a bit. Basically, we need to alter comment form.

You can do this in two ways:

  1. By putting your custom code into template.php file
  2. By putting your custom code into your custom module

It's up to you which way to choose. If you want your changes to be applicable not only to the active theme, then choose the second option. Ok, now, depending on a way you've chosen, add this code to your template.php or module_name.module file:

function ModulNameOrYourThemeName_form_comment_form_alter(&$form, &$form_state, &$form_id) {
  $form['comment_body']['#after_build'][] = 'ModulNameOrYourThemeName_customize_comment_form';  
}

Option A) If you only want to hide formatting guidelines and Filter Tips link , then add this function to the file:

function ModulNameOrYourThemeName_customize_comment_form(&$form) {
  // Hide guideliness
  $form[LANGUAGE_NONE][0]['format']['guidelines']['#access'] = FALSE; // Note 'und', you may need to set your comment form language code instead
  // Hide Filter Tips
  $form[LANGUAGE_NONE][0]['format']['help']['#access'] = FALSE;
  return $form;  
}

Option B) If you want to hide all format options , then add this function to the file:

function ModulNameOrYourThemeName_customize_comment_form(&$form) {  
  $form[LANGUAGE_NONE][0]['format']['#access'] = FALSE; // Note LANGUAGE_NONE, you may need to set your comment form language code instead
  return $form;  
}

Save your file and don 't forget to clear Drupal's cache (Configuration > Development > Performance > Clear All Caches).

That's all for now, enjoy and happy drupalling!

Comments

Required for comment verification



John

This solution appears to kill tinymce editor. I used some css instead: #edit- comment-body-und-0-format { display: none; } In most circumstances it should work. I've used it before when not using the tinymce text editor.

Reply · 6 years, 5 months ago
JAScarb

Worked for me (option A), but I've no idea why. Anyone care to explain how adding a function without a call to it does anything, or point me in the direction of a resource? I guess I need to know more about hooks.

Reply · 6 years, 5 months ago
Sarah Opstaken

Hello Tim. I am working on a new Drupal site. Have been switching to Wordpress for a while because the learning curve was so steep with Drupal. Anyway, do you also charge per project/problem? I meet many developers charging per hour, which I understand but find less attractive than a fixed fee in case I run into problems. Warm regards, Sarah

Reply · 6 years, 5 months ago
sempepe

the most simplest solution i've implemented on code array( '#title' => t('Whatever'), '#type' => 'text_format', '#base_type' => 'textarea', '#description' => t('bla bla bla'), '#format' => 'filtered_html', 'format' => array('access' => FALSE), // Hiding text options '#default_value' => 'my_vaue', '#required' => TRUE, ), No hooks needed for drupal 7.15 :)

Reply · 6 years, 5 months ago
Christopher Milne

look like i mistyped the hook name. please disregard my earlier comment. this is a good solution. installing that module is overkill.

Reply · 6 years, 5 months ago
Christopher Milne

I was not able to get Drupal to call the function 'hook_form_comment_form_alter' in option A from my theme's template php file and subsequently the callback function was not called. Also, what do you mean by "Note ‘und’, you may need to set your comment form language code instead"? Drupal 7.18

Reply · 6 years, 5 months ago
R. Tayfun Gedik

It's work! Thanks..

Reply · 6 years, 5 months ago
Gabor

Great! Works perfectly, thank you!

Reply · 6 years, 5 months ago
bongo

Original post (option A) works great for me, using D7.14

Reply · 6 years, 5 months ago
charl

it seems work. .

Reply · 6 years, 5 months ago
stephen

Hello there! This is my first comment here so I just wanted to give a quick shout out and tell you I genuinely enjoy reading through your articles and other information u provide here. Good work.

Reply · 6 years, 5 months ago
Jason

How do I remove just the selector, but leave the quick tips in place? I tried

$form[LANGUAGE_NONE][0]['format']['format']['#access'] = FALSE;

...but now the quick tips show the tips for ALL input filters, not just those enabled for anonymous comments!

Reply · 6 years, 5 months ago
Nate Dunham

Ahh.. thanks for all the help. Had trouble finding the solution for removing the format options. It's a great help!

Reply · 6 years, 5 months ago
blackangelxl

Take this: /* * Function - yourthemename_form_comment_form_alter. * * @param array $form * @param type $form_state * @param type $form_id */ function yourthemename_form_comment_form_alter(&$form, &$form_state, &$form_id) { $form['comment_body']['#after_build'][] = 'yourthemename_customize_comment_form'; } /* * Function - yourthemename_customize_comment_form. * * @param type $form * @return boolean */ function yourthemename_customize_comment_form(&$form) { $form[LANGUAGE_NONE][0]['format']['guidelines']['#access'] = FALSE; $form[LANGUAGE_NONE][0]['format']['help']['#access'] = FALSE; $form[LANGUAGE_NONE][0]['format']['#attributes'] = array('style' => 'display: none;'); return $form; }

Reply · 6 years, 5 months ago
jkool

Works fine for comments, but how on earth do I remove this for the "text format" stuff for the node itself?

Reply · 6 years, 5 months ago
fourmi4x

Thanks for sharing this, exactly what I was looking for !

Reply · 6 years, 5 months ago
Angelo

I have no comment form but in order to "remove" the "more information about text formats" link in my admin theme I ended like this in my theme css: .filter-help{display: none;}

Reply · 6 years, 5 months ago
Kannary

Another option is to use the awesome MyHook Administration module, in which case you can inject module-level hooks anywhere: http://drupal.org/project/myhook If using this module, then add the functions like such: //Remove Formatting Tips and Links from Comments function myhook_form_comment_form_alter(&$form, &$form_state, &$form_id) { $form['comment_body']['#after_build'][] = 'myhook_customize_comment_form'; } function myhook_customize_comment_form(&$form) { // Hide guideliness $form[LANGUAGE_NONE][0]['format']['guidelines']['#access'] = FALSE; // Note ‘und’, you may need to set your comment form language code instead // Hide Filter Tips $form[LANGUAGE_NONE][0]['format']['help']['#access'] = FALSE; return $form; }

Reply · 6 years, 5 months ago
Tobias

Dear Tim, your instructions are exactly what I need, however it does not work for me! Here is the code in my module invitation: function invitation_form_comment_form_alter(&$form, &$form_state, &$form_id) { $form['comment_body']['#after_build'][] = 'invitation_customize_comment_form'; } function invitation_customize_comment_form(&$form) { $form[LANGUAGE_NONE][0]['format']['#access'] = FALSE; return $form; } Seems to me like I copied it correctly. I do not manage to get an output of the comment form via drupal_set_message in this hook, so I am struggling with the debugging. Can you advise me how I can find the mistake in my case, please?? Thanks a lot, Tobias

Reply · 6 years, 5 months ago
EvanDonovan

Was there a particular reason you used #after_build instead of just hook_form_alter? Worried about some module later in the execution order clobbering the changes?

Reply · 6 years, 5 months ago
dalin

Or the easier, more maintainable option: http://drupal.org/project/better_formats

Reply · 6 years, 5 months ago
vikky

Although it works partially, I am unable to remove "More information abut text formats". How do I remove that? Thanks a lot!!

Reply · 6 years, 5 months ago