My purpose is to hide completely all format options from the node edit form.

I have this code in my module:

function mymodule_form_alter(&$form, &$form_state, &$form_id) {
  $form['field_body']['#after_build'][] = 'mymodule_customize_node_form';  
}
function mymodule_customize_node_form(&$form) {
  $form[LANGUAGE_NONE][0]['format']['guidelines']['#access'] = FALSE; 
  $form[LANGUAGE_NONE][0]['format']['help']['#access'] = FALSE;
  $form[LANGUAGE_NONE][0]['format']['#theme_wrappers'] = NULL;
  return $form;  
}

As a result, I get almost all the formatting options hidden, except for Text format option as visible on the screenshot: http://img141.imageshack.us/img141/8448/drupal.png

I tried also add a more specific restriction (based on guessing) like:

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

but it does not change anything.

How to hide this option? I tried to make a more general modification like this:

function mymodule_customize_node_form(&$form) {
  $form[LANGUAGE_NONE][0]['format']['#access'] = false;
  return $form;  
}

With this code, all formatting options are hidden (good), but TinyMCE stops showing and all I get is a plain text editor (bad). So perhaps it's too general.

Comments

vasi1186’s picture

You can try this: from the "Manage fields" menu of your content type(s), click on the configure link for your Body field and select the "Plain text" for the "Text processing" option. But, if you want to use a RTE for your field, then you have to find another solution, because there you will need the Input filters.

raincloud’s picture

I need Full HTML and TinyMCE and I have this already. The problem is that the user should not see or have choice of the input format. He should have only Full HTML, period. No plain text, no enable/disable RTE. That's the thing I must hide on the form.

It doesn't mean I want to disable the input filters. I only want to hide the choice from the user, but let the filters function as normally.

vasi1186’s picture

if this is only about user interface, maybe you can just hide them from css...

raincloud’s picture

If there is no other option, I'll use the css (actually, I used it already as a temporary solution and it works). But it would be better to do it independent of css and theme, rather I planned to make it user-dependent. But thanks for the suggestion, it's practically the simplest way to kind of implement it.

mattchew61’s picture

I'm new to Drupal and would like to employ your CSS fix in order to hide the Text Format option from the user. How did you assign a CSS class to this area? I don't know my way around the files well enough to know where to look.

Thanks for your help!

raincloud’s picture

The class was assigned to this area by my theme (Seven in this case). If you are using Firebug or Chrome Inspect, you may navigate to a node and inspect what the css class is for the Text Format options, or check page source. In my case, the code responsible for this area is:

<div class="form-item form-type-select form-item-body-und-0-format">
  <label for="edit-body-und-0-format--2">Text format </label>
 <select class="filter-list wysiwyg form-select wysiwyg-processed" id="edit-body-und-0-format--2" name="body[und][0][format]"><option value="full_php">Full PHP</option><option value="filtered_html">Filtered HTML</option><option value="full_html" selected="selected">Full HTML</option><option value="plain_text">Plain text</option></select>
</div>

So I changed the css file used by my theme and added:

.form-item.form-type-select.form-item-body-und-0-format {
    display: none;
}

Dunno if this is the "right" solution, but it works.

vincentstoop’s picture

<?php
function mymodule_form_alter(&$form, &$form_state, &$form_id) {
  $form['field_body']['#after_build'][] = 'mymodule_customize_node_form'; 
}
function mymodule_customize_node_form(&$form) {
  $form[LANGUAGE_NONE][0]['format']['guidelines']['#access'] = FALSE;
  $form[LANGUAGE_NONE][0]['format']['format']['#access'] = FALSE;
  $form[LANGUAGE_NONE][0]['format']['help']['#access'] = FALSE;
  $form[LANGUAGE_NONE][0]['format']['#theme_wrappers'] = NULL;
  return $form; 
}
?>

The second line inside of mymodule_customize_node_form the trick for me...

prsnjtbarman’s picture

This code works for me to hide Text-format option of Body field for any Content Type

Argus’s picture

There is a nice module that let's you sanitize the UI for users: http://drupal.org/project/simplify

srlawr’s picture

Hi. Thank you so much for this link. The module didn't actually do what I was after unfortunately, BUT I was able to poach through the source code and found this little gem:

drupal_add_js('jQuery().ready(function(){jQuery(".filter-wrapper").hide();});', 'inline');

which I was able to stuff into my own form_alter hook, which removes the formatting guidelines perfectly!

Cheers.

rich.3po’s picture

Hi srlawr (now that name sounds familiar...?)

Another way to do it that doesn't rely on JS is to use hook_element_info_alter() (actually this is what Better Formats does). You can set a custom callback in this hook, and then revoke access to the 'formats' pane for any field of your choice. Kind of neater than doing another form_alter()

Example:

function mymodule_element_info_alter(&$type) {
	if (isset($type['text_format']['#process'])) {
		foreach ($type['text_format']['#process'] as &$callback) {
			if ($callback === 'filter_process_format') {
				$callback = 'mymodule_process_format';
			}
		}
	}
}

function mymodule_process_format($element) {
	
	// array of field names to restrict (add more here as needed)
	$fields = array(
		'field_textarea',
		'comment_body',
	);
	
	$element = filter_process_format($element);
	
	// Hide the 'text format' pane below certain text area fields.
	if (isset($element['#field_name']) && in_array($element['#field_name'], $fields)){
		$element['format']['#access'] = FALSE;
	}
	return $element;
}
srlawr’s picture

Hi Threeps. Thanks for this.. I will have to look into it. Project is on code-freeze now though, so will have to stick with the working solution we have, best practise or not. Will pencil this down to review for v2.0 though :)

invisibleink’s picture

This worked great for me. However, I made one small change.... instead of setting
$element['format']['#access'] = FALSE;

I used
$element['format']['#attributes']['style'] = 'display:none';

This preserved the WYSIWYG (using CKEditor and wysiwyg module).

Many thanks!!

fehin’s picture

This did the trick. Thanks.
Now I'm trying to get it to work with node forms.

dazweeja’s picture

Maybe a little neater to use an existing Drupal system class than an inline style:

$element['format']['#attributes']['class'][] = 'element-hidden';

er.pushpinderrana’s picture

Thank you for one more trick.

Pushpinder Rana #pushpinderdrupal
Acquia Certified Drupal Expert

fehin’s picture

How do you make it work with node forms and not just comments?
Thanks!

ehsankhfr’s picture

Thank you! It really worked for me!

Drupal Version: 7.32

simonjcarr’s picture

@srlawr - Thanks for this snippet of code. It does just what I want with the minimum fuss.

dskanth’s picture

This tweak in CSS worked for me:

.filter-wrapper { display: none; }

srlawr’s picture

I'm not entirely sure why or what I have done to my site setup, but it is absolutely impossible for me to remove the formatting selection and guidelines! It's a naff problem, the input format select box and bullet points are useless to me, especially with a wysiwyg, and I don't know why they are not simply switch-off-able!

I have tried all of these, and other solutions, DPMed my forms and hooks until I'm blue in the face, but it's going to have to be a CSS fix. Pain! :(

WorldFallz’s picture

i do this with the http://drupal.org/project/better_formats module, but i've also used hook_form_alter which does in fact work.

BrightBold’s picture

It appears that this no longer works with Better Formats after some security fixes were implemented. Now if you only allow one format for a role, the WYSIWYG editor is hidden for the field along with the text formats and hints. So this didn't help me; back to the custom module route.

HiMyNameIsSeb’s picture

You could hide it using hook form alter. It ultimately does it using css, however you can put this code into your own custom module or feature to separate it from the theme:

//for form alter above
$form[LANGUAGE_NONE][0]['format']['guidelines']['#attributes']['style'] = 'display:none;';

//D7 for undefined language
$form['field_name']['und']['#attributes']['style'] = 'display:none;';

//D7 language defined
$language= $form['language']['#value'];
$form['field_name'][$language]['#attributes']['style'] = 'display:none;';

sempepe’s picture

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 select options
        '#default_value' => 'my_vaue',
        '#required' => TRUE,
      ),

No hooks needed for drupal 7.15

:)

BrianLewisDesign’s picture

Can this be done in Drupal 6?
Where is this code located?
Is this #type a field?
Is this for one field inside a content type?

Purushothaman Chinnadurai - Drupal Geeks’s picture

Hi,

Below code allows to change the access of the text formats of the all node fields

/**
 * Implements of hook_element_info_alter().
 */
function dg_module_element_info_alter(&$type) {
  // Our process callback must run immediately after filter_process_format().
  $filter_process_format_location = array_search('filter_process_format', $type['text_format']['#process']);
  $replacement = array('filter_process_format', 'dg_module_filter_process_format');
  array_splice($type['text_format']['#process'], $filter_process_format_location, 1, $replacement);
}

/**
 * Process callback for form elements that have a text format selector attached.
 *
 * This callback runs after filter_process_format() and performs additional
 * modifications to the form element.
 *
 * @see filter_process_format()
 */
function dg_module_filter_process_format($element) {
  $element['format']['format']['#access'] = FALSE;
  $element['format']['guidelines']['#access'] = FALSE;
  $element['format']['help']['#access'] = FALSE;
  $element['format']['#type'] = 'container';
  $element['format']['#attributes']['class'][] = 'element-hidden';
  return $element;
}

I Hope above code is helpful.

alamp’s picture

@Drupal Geeks, it works like a charm, even in a custom module without a change.
In a nutshell, you're brilliant!

tajinder.minhas’s picture

You need to remove this line from code $element['format']['format']['#access'] = FALSE; because it is hiding RTE itself which is not the usecase asked in question. Rest of code is fine.

Regards
Tajinder Singh Minhas
Software Engineer
(SDG SIPL)

santoshKR’s picture

"hook_element_info_alter" worked for me !!

BrianLewisDesign’s picture

You can hack it with JS. Set the default and hide it with a css property. Use a drupal_add_js() function to add it to the page.