Skip to content

Instantly share code, notes, and snippets.

@flashvnn
Last active August 29, 2015 14:02
Show Gist options
  • Select an option

  • Save flashvnn/7bdb49577d931e6fb8b0 to your computer and use it in GitHub Desktop.

Select an option

Save flashvnn/7bdb49577d931e6fb8b0 to your computer and use it in GitHub Desktop.
Drupal force text format for a field
<?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 bluecollar_helper_element_info_alter(&$type) {
if (isset($type['text_format']['#process'])) {
foreach ($type['text_format']['#process'] as &$callback) {
if ($callback === 'filter_process_format') {
$callback = 'bluecollar_helper_filter_process_format';
}
}
}
}
/**
* Callback for bluecollar_helper_element_info_alter().
*/
function bluecollar_helper_filter_process_format($element) {
$element = filter_process_format($element);
// Change input format to "Filtered HTML" for body fields of article nodes
if (isset($element['#bundle']) && $element['#bundle'] == 'photo' && $element['#field_name'] == 'field_text') {
$element['format']['format']['#default_value'] = 'wysiwyg_mini';
$options['wysiwyg_mini'] = $element['format']['format']['#options']['wysiwyg_mini'];
$element['format']['format']['#options'] = $options;
}
return $element;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment