4

I am very new to drupal and have been playing around with lots of different modules and core features. I have now come to an issue I have been trying to solve for the pass 48hrs without finding the right solution.

I am building a forum, using the core module, but my problem is when users post a new topic / comment, they are given the option of selecting which 'text format' they want to use. I want drupal to take it as given that filtered HTML is the only option and not offer the use a 'plain text' format.

I can just target the fieldset's id with 'display:none' in CSS and that does work, will not display any more, but I would like to know how to remove the fieldset code itself.

I have tried this,

function THEMENAME_form_alter(&$form, $form_state, $form_id)  {
  unset($form_id('edit-body-und-0-format');
} 

The 'edit' id is the id on the fieldset for the text options, but all this does is give me the white screen of death....

So what I am doing wrong?

Many thanks for any help given....

Glenn.

1 Answer 1

2

There's a wee bit of confusion in your PHP code there; you're calling a function with the same name as the string passed in $form_id, and passing 'edit-body-und-0-format' as a parameter.

What you need to do is change the relevant field in the $form array, e.g.

function THEMENAME_form_alter(&$form, $form_state, $form_id)  {
  $form['edit-body-und-0-format']['#access'] = FALSE;
}

Using #access is preferred to unset(); there might be something in a validation/submit handler that's relying on the existence of those fields, whether or not values have been submitted this time round, and removing it completely might break those (or cause some unpleasant warnings).

If you haven't already installed it, the Devel module is a must for development; you can use the dpm() function to inspect PHP elements on the front end. If you add dpm($form); to your current function, for example, when you visit the page you'll see a nice structured output of the $form variable making it easy to find the element(s) you want to alter.

Update...

If you're trying to hide the text format options there's a slightly different approach. You need to attach an after build function to the textarea, and remove the format options in there. For example:

function THEMENAME_form_alter(&$form, $form_state, $form_id)  {
  if ($form_id == 'some_form') {
    $form['body']['#after_build'][] = 'MYTHEME_body_after_build';
  }
}

function MYTHEME_body_after_build($element) {
  $element[LANGUAGE_NONE][0]['format']['#access'] = FALSE;
  return $element;
}

or something very similar.

7
  • Many thanks for the reply + help. I used the code you give me, although it is not given me the white screen of death anymore, it still displaying the 'text format options fieldset'. What I also tried was to add, ['fieldset'] before the 'edit-...' id, but again nothing changed. And yes I did clear the cache. I have been playing around with the devel module but it does not seem to do anything? I put dpm($form) inside my function, but it does not output anything at all? Commented Apr 30, 2013 at 17:38
  • Ah. The text format options are a different, altogether more fun problem. Give me 10 minutes and I'll chuck some more code in that should help. As for Devel, are you logged in as admin? If not you'll need to give the right permissions to access devel output (temporarily of course if you enable it for anon users)
    – Clive
    Commented Apr 30, 2013 at 17:39
  • Many thanks for helping with this... this was starting to drive me up the wall..... Commented Apr 30, 2013 at 17:40
  • 1
    No worries, Drupal's a maze when you're just getting started
    – Clive
    Commented Apr 30, 2013 at 17:49
  • Just try your code, and still displaying the bloody format options! I think the main issue is drupal will not let me remove the 'plain text' option, looks like its fixed! This is one of my 1st drupal sites, also the 1st time I built a responsive designed site! Its ok for now, I just target the fieldset with CSS! Could you recommend any good books /sites that deal with the hook coding, I have access to training that looks into what I can do with drupal but not really going into hook coding... Many thanks for your help any way.. Commented Apr 30, 2013 at 18:02

Your Answer

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

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