8

I have a custom install profile and I must programmatically change the body text format on the page content type to full HTML. However, I didn't manage to find how to do it.

How can I do it?

4
  • you want to change it for all content types?
    – Aboodred1
    May 6, 2013 at 19:54
  • @Aboodred1 only for page (standard type)
    – Codium
    May 6, 2013 at 21:42
  • you already created Full HTML format?
    – Aboodred1
    May 6, 2013 at 21:57
  • @Aboodred1 yes I did
    – Codium
    May 7, 2013 at 9:55

3 Answers 3

6

You can do it with hook_element_info_alter, here is a snippet.

<?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 MODULENAME_element_info_alter(&$type) {
  if (isset($type['text_format']['#process'])) {
    foreach ($type['text_format']['#process'] as &$callback) {
      if ($callback === 'filter_process_format') {
        $callback = 'MODULENAME_filter_process_format';
      }
    }
  }
}

/**
 * Callback for MODULENAME_element_info_alter().
 */
function MODULENAME_filter_process_format($element) {
  $element = filter_process_format($element);
  // Change the default text format of the 'field_company_spotlight' field to
  // 'Media HTML'. 
  if ($element['#bundle'] == 'company' && $element['#field_name'] == 'field_company_spotlight') {
    $element['format']['format']['#default_value'] = 'media_html';
  }
  return $element;
}
?>

As THIS post suggests you could try

$form['field_name'][LANGUAGE_NONE][0]['#format'] = 'full_html';
$form['field_name'][LANGUAGE_NONE][0]['#format'] = 'filtered_html';

in your hook_form_alter or in hook_FORM_ID_alter

Also there is Better Formats module

Better formats is a module to add more flexibility to Drupal's core input format system.

2

Nikhil M's 2nd answer is best -

$form['field_name'][LANGUAGE_NONE][0]['#format'] = 'full_html';  

no need for hook_element_info

1
  • This belongs in the comment of Nikhil's answer. Jul 3, 2014 at 1:20
-1

you only need one line of code

$result = db_query('UPDATE field_data_body SET body_format='full_html' WHERE bundle='page');

1
  • What is wrong with this query? I think it is a workaround... Mar 7, 2018 at 13:01

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.