简体   繁体   English

Drupal模块:创建文本格式

[英]Drupal module: create a text format

I've created a module which defines a new text format filter . 我创建了一个模块,该模块定义了一个新的文本格式过滤器
Now I want to define a text format using this new filter, directly from module php. 现在,我想使用此新过滤器直接从模块php定义文本格式 Drupal Administrator can do this manually from admin/config/content/formats/add admin page, but I want to avoid this step. Drupal Administrator可以从admin/config/content/formats/add admin页面手动进行此操作,但是我想避免此步骤。 What do I need to add to my_dmodule.module ? 我需要添加什么到my_dmodule.module

I'm guessing that you are trying to create a text format on module installation. 我猜您正在尝试在模块安装上创建文本格式。 If so, you could call filter_format_save(). 如果是这样,您可以调用filter_format_save()。 The details on creating the object can be found in filter.module ( Drupal API reference ). 有关创建对象的详细信息,请参见filter.module( Drupal API参考 )。

You might have to load the filter module first if you are creating the filter in hook_install(), haven't checked: 如果要在hook_install()中创建过滤器,则可能必须先加载过滤器模块,但尚未检查:

drupal_load('module', 'filter');

Inspired by Hendrik 's answer, this is my solution: 受到亨德里克答案的启发,这是我的解决方案:

function myformat_install() {
  drupal_load('module', 'filter');
  /* check already exists */
  $format_exists = (bool) db_query_range('SELECT 1 FROM {filter_format} WHERE name = :name', 0, 1, array(':name' => 'My Format'))->fetchField();
  if (!$format_exists) {
    $format = array(
        'format' => 'myformat',
        'name' => 'My Format',
        'filters' => array(
            'myformat_filter' => array(
                'weight' => 0,
                'status' => 1,
            ),
        ),
    );
    $format = (object) $format;
    filter_format_save($format);
  }
}

myformat_filter is a filter defined implementing hook_filter_info() , but it could be a filter defined in another module. myformat_filter是定义了实现hook_filter_info()的过滤器,但它可能是另一个模块中定义的过滤器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM