簡體   English   中英

使用hook_ds_fields_info(),是否可以使用標准格式化程序,以便在“管理顯示” UI中使用這些格式化程序的選項?

[英]Using hook_ds_fields_info(), is there a way to use standard formatters so the options for those formatters are available in the Manage Display UI?

我在這里遵循了一個很好的教程: http : //previousnext.com.au/blog/creating-custom-display-suite-fields-or-how-i-learned-stop-worrying-and-use-hookdsfieldsinfo以編程方式創建習俗具有hook_ds_fields_info()的字段。

在下面的代碼中,我嘗試使用text_trimmed格式化程序,但是在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; 
}
?>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM