Showing posts with label drupal-hook. Show all posts
Showing posts with label drupal-hook. Show all posts

Friday, 2 December 2011

Drupal 6: Enable/Disable the WYSIWYG Editor on Specific Forms

You will need your own custom module for this, in order to implement hook_form_FORM_ID_alter().

DISABLING
You can disable the WYSIWYG editor on any form by setting the FAPI (Forms API) setting #wysiwyg to FALSE e.g. for the Page node edit form, you can do this:
function MODULE_form_page_node_form_alter(&$form, &$form_state) {
    $form['body_field']['body']['#wysiwyg'] = FALSE;
}

ENABLING
  1. Some contrib modules (e.g. Ubercart) have completely disabled WYSIWYG editors on some of their textareas, and I haven't found a way around this.
  2. If you have checked the "Enable by default" option, the WYSIWYG editor should automatically be enabled for any form fields that have the "Input format" option below them.
  3. For other forms (with simple textareas that dont have the "Input format" option) you must manually enable the "Input format" option yourself. This can be done by again altering the $form object in your hook_form_FORM_ID_alter() e.g. for the Description field on the form to edit a content type:
function MODULE_form_node_type_form_alter(&$form, &$form_state) {
    $form['identity']['format'] = filter_form();
}


Update: if you're more into GUIs than fiddling in the code, you could just install the Better Formats module, which gives you loads of control on which Input formats are used for which forms. Then by having one plain-text input format, and another one associated with a WYSIWYG editor, you can easily control which forms use which.

Drupal 6: hook_form_node_form_alter() not working

I've long known about the versatile hook_form_alter() function, which allows you to alter any form before it gets displayed on the page, using your own custom module. Recently I discovered the wonderful hook_form_FORM_ID_alter() function, which is only fired for a specific form id, making it a better solution when you want to target only 1 form.

I had used this successfully a few times, and then hit a wall when I tried using it for the main node edit form (the form's id is node-form, so I called the function hook_form_node_form_alter). Half an hour later, I stumbled upon the solution: it does not want the HTML ID attribute, it wants the internal Drupal ID for the form. So in my custom module, I used this code to get the ID:

function MODULE_form_alter(&$form, &$form_state, $form_id) {
 echo "formID=".$form_id;
}

Which turned out to be page_node_form, so the final function that worked was called MODULE_form_page_node_form_alter().

REFERENCES
http://www.hashbangcode.com/blog/drupal-hookformalter-node-form-501.html

Friday, 29 October 2010

Drupal 6: Altering the help message for a particular URL

I noticed that on the "All Content" page, the help message is empty, but my theme still displays a big empty bubble, which looks like a bug. After doing some research I realised the help message on this page came from the node_help() function, and was returning just a space on purpose, as a not-null value meant that the little "more help" link would still be displayed.

SOLUTION
Implement the theme_help() hook in your theme's template.php. Note: replace THEME with your theme name.

function THEME_help() {
 $help = menu_get_active_help();
 if (arg(0)."/".arg(1)."/".arg(2)=="admin/content/node" && !arg(3))
 {
  $help = "<p>My help message.<p>".$help;
 }
 
 if ($help) return "<div class='help'>".$help."</div>";
 else return null;
}

This will work for the URL admin/content/node, but can easily be altered to work for any URL.

REFERENCES
http://stackoverflow.com/questions/1296106/drupal-6-hook-for-altering-help-field-in-a-form