使用hook_ds_fields_info(),是否有一种方法可以使用标准格式化程序,以便在Manage Display



我遵循了一个很好的教程:http://previousnext.com.au/blog/creating-custom-display-suite-fields-or-how-i-learned-stop-worrying-and-use-hookdsfieldsinfo使用hook_ds_fields_info()以编程方式创建自定义字段。

在下面的代码中,我试图使用text_trim格式化器,但在UI中,我没有得到修剪格式的设置。

/**
 * Implements hook_ds_fields_info().
 */
function my_module_ds_fields_info($entity_type) {
  $fields = array();
  $fields['node']['article_footnote'] = array(
    'title' => t('Article footnote'),
    'field_type' => DS_FIELD_TYPE_FUNCTION,
    'function' => 'my_module_ds_field_article_footnote',
    'ui_limit' => array('my_content_type|*', '*|search_index'),
    'properties' => array(
      'formatters' => array(
        'text_default' => t('Default'),
        'text_plain' => t('Plain text'),
        'text_trimmed' => t('Trimmed'),
      ),
    ),
  );
  if (isset($fields[$entity_type])) {
    return array($entity_type => $fields[$entity_type]);
  }
  return;
}
/**
 * Render the article footnote field.
 */
function my_module_ds_field_article_footnote($field) {
  $content = 'All articles are composed in a permanent state of coffee frenzy.';
  return $content; 
}

您缺少的一部分是通过回调函数中的check_markup()过滤文本。否则,你只是返回一个普通的字符串。

另外,使用通过调用filter_formats()在系统中配置的默认文本格式列表也是一个好主意。

这是应该可以工作的修改后的代码。准备好代码后,尝试切换字段的格式,然后查看节点以查看不同的结果。我在字符串中添加了一些HTML,以便您可以看到差异。

<?php
/**
 * Implements hook_ds_fields_info().
 */
function my_module_ds_fields_info($entity_type) {
  $fields = array();
  // Build a list of input formats.
  $formatters = array();
  $filter_formats = filter_formats();
  foreach ($filter_formats as $format) {
    $formatters[$format->format] = $format->name;
  }
  $fields['node']['article_footnote'] = array(
    'title' => t('Article footnote'),
    'field_type' => DS_FIELD_TYPE_FUNCTION,
    'function' => 'my_module_ds_field_article_footnote',
    'ui_limit' => array('my_content_type|*', '*|search_index'),
    'properties' => array(
      'formatters' => $formatters,
    ),
  );
  if (isset($fields[$entity_type])) {
    return array($entity_type => $fields[$entity_type]);
  }
  return;
}
/**
 * Render the article footnote field.
 */
function my_module_ds_field_article_footnote($field) {
  $content = check_markup('<h1>All articles</h1> are composed in a permanent state of <strong>coffee frenzy</strong>.', $field['formatter']);
  return $content; 
}
?>

最新更新