Answers:
如果没有稳定发行的“ 更好的格式”模块,则可以创建一个自定义模块来针对特定的内容类型或字段执行此操作。
创建一个模块(modulename.info和modulename.module,在文件夹“ modulename”内)。示例: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;
}
和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"
将这些文件放在sites / all / modules / custom中的“ default_text_format”文件夹中。
根据您的内容类型更改捆绑软件名称。您也可以用自己的“ field_contenttype_fieldname”代替“ body”。(在此注释 / better_formats代码之后。)
使用更好的格式模块:
更好的格式是为Drupal的核心输入格式系统增加更多灵活性的模块。它将允许您设置每种内容类型的默认格式等等。
我本人只是碰到了这个问题,但是我不想使用beta模块(更好的格式),并且我不得不扩展功能并使其自动化,因此此类设置不是硬编码的,而是在后台设置的。
所以我做了以下事情:
字段编辑设置部分
/**
* 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,
);
}
}
因此,这部分代码应该足够明显……它在其中添加了一个字段集并添加了一个选择列表,该列表由您网站上现有的WYSIWYG配置文件填充。这些“所见即所得”配置文件与文本格式相关联,因此,当有人选择文本格式/过滤器时,它实际上会选择已配置的配置文件。
现在,第二部分与上面另一个用户提供的代码相同,该代码取自“更好的格式”模块。
/**
* 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;
}
这样就保存了设置,因此可以进行功能导出,也可以使用任何方法保存设置。
希望这对遇到此问题的其他人有所帮助!