9

I want to be able to set a different default input text format per content type and per content field.

For example, the format type choices might be "Plain text" and "Rich text editor" and in some cases I want the format to default to "Rich text editor" but keep "Plain text" as a choice in the dropdown. I know that I can change the order of the text formats to make "Rich text editor" the first choice, but this method changes all of them, that's not what I want to do.

0

3 Answers 3

9

In the absence of a stable release of Better Formats module, you can create a custom module to do this for specific content types or fields.

Create a module (modulename.info and modulename.module, inside a folder 'modulename'). Example: default_text_format.module:

<?php
/**
* Implements hook_element_info_alter().
*
* Sets the text format processor to a custom callback function.
* This code is taken from the Better Formats module.
*/
function default_text_format_element_info_alter(&$type) {
  if (isset($type['text_format']['#process'])) {
    foreach ($type['text_format']['#process'] as &$callback) {
      if ($callback === 'filter_process_format') {
        $callback = 'default_text_format_filter_process_format';
      }
    }
  }
}

/**
* Callback for MODULENAME_element_info_alter().
*/
function default_text_format_filter_process_format($element) {
  $element = filter_process_format($element);
  // Change input format to "Filtered HTML" for body fields of article nodes
  if ($element['#bundle'] == 'article' && $element['#field_name'] == 'body') {
    $element['format']['format']['#default_value'] = 'filtered_html';
  }
  return $element;
}

And default_text_format.info:

name = Default text format
description = Adapt the module code to set a default format for a content type.
package = Custom modules
core = "7.x"

Put these files in a folder 'default_text_format' in sites/all/modules/custom.

Change bundle name as appropriate for your content type. And you can substitute 'body' with your own 'field_contenttype_fieldname'. (After this comment / better_formats code.)

3
  • 1
    I tried this and altered it to work so it worked for me. ` foreach ($type['text_format']['#process'] as $key => $callback) { if ($key == 'filter_process_format') { $type['text_format']['#process'][] = 'MODULE_NAME_default_text_formats_filter_process_format'; }`
    – awm
    Oct 28, 2013 at 16:43
  • I can confirm @awm's solution. The original answer does not work for me also, because it overrides the default callback. awm's solution fixes that by adding the callback, instead of overriding. Dec 9, 2014 at 0:46
  • Update - I take back my last comment. The original answer does work and is preferred. It didn't work for me because I had Better Formats module installed, which has overridden my callback. Ideally, you want to disable it before adding this function. Now - in the case where you do add a second callback to the array (as the comment above me suggests), callback #1 will be processed first, thus altering data before it hits your callback. Dec 9, 2014 at 1:56
3

Use the Better Formats module:

Better formats is a module to add more flexibility to Drupal's core input format system. It will allow you to set the default format per content type and much more.

1
  • Thanks, looks promising but I'm using D7 on a production website so I'm not at this time going to use this module because It's still in development.
    – J-Fiz
    Nov 23, 2011 at 19:28
3

I just bumped into this problem myself, but I didn't want to use a beta module (Better Formats), and I had to extend the functionality and automate it, so that such settings are not hardcoded, but set from the back-office.

So I did the following:

  • I added a setting on the Edit Settings form of the field that I need the default text format for
  • I used the code provided the above and modified it slightly so that it gets sets the default text format as saved in the settings of the field
  • I used Features to export the content type so that I have my settings preserved in code

Field Edit Settings part

/**
 * Implements hook_form_FIELD_UI_FIELD_EDIT_FORM_alter().
 */
function MY_MODULE_form_field_ui_field_edit_form_alter(&$form, &$form_state) {
  if ($form['#field']['type'] == 'text_long') {
    $instance = $form['#instance'];
    // Fieldset for Default Formats settings.
    $filters = filter_formats();
    $options = array('_none' => t('None'));
    foreach ($filters as $key => $filter) {
      $options[$key] = $filter->name;
    }
    $form['instance']['settings']['default_filter'] = array(
      '#type' => 'fieldset',
      '#title' => t('Default Filter Settings'),
      '#collapsible' => FALSE,
      '#collapsed' => FALSE,
    );
    $form['instance']['settings']['default_filter']['wysiwyg_profile'] = array(
      '#type' => 'select',
      '#title' => t('Select a default format for this field'),
      '#description' => t('The selected text format will influence the button and plugin configuration of WYSIWYG.'),
      '#default_value' => isset($instance['settings']['default_filter']['wysiwyg_profile'])
          ? $instance['settings']['default_filter']['wysiwyg_profile'] : '_none',
      '#options' => $options,
    );
  }
}

So this part of the code should evident enough...it adds a fieldset and adds a select list in it, which is populated by the WYSIWYG profiles existing on your site. Those WYSIWYG profiles are tied to the text formats, so when someone selects a text format/filter, it actually selects the profile that is configured.

Now the 2nd part is the same code as provided above by another user, and which is taken from the Better Formats module.

/**
 * Implements hook_element_info_alter().
 *
 * Sets the text format processor to a custom callback function.
 * This code is taken from the Better Formats module.
 */
function MY_MODULE_element_info_alter(&$type) {
  if (isset($type['text_format']['#process'])) {
    foreach ($type['text_format']['#process'] as &$callback) {
      if ($callback === 'filter_process_format') {
        $callback = 'MY_MODULE_filter_process_format';
      }
    }
  }
}

/**
 * Callback for MY_MODULE_element_info_alter().
 *
 * Alters the default text format of fields.
 */
function MY_MODULE_filter_process_format($element) {
  $element = filter_process_format($element);
  // Configuration array that specifies the fields that need to be altered.
  $field_info = field_info_instance($element['#entity_type'],
                                    $element['#field_name'], 
                                    $element['#bundle']);
  // Change input format to configured value.
  if (isset($field_info['settings']['default_filter']['wysiwyg_profile']) && $field_info['settings']['default_filter']['wysiwyg_profile'] != '_none') {
    $element['format']['format']['#default_value'] = $field_info['settings']['default_filter']['wysiwyg_profile'];
  }
  return $element;
}

So the settings are saved, therefore Features Export is possible or whatever method you use to save your settings.

I hope this helps anyone else who encountered this problem!

Your Answer

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

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