首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何添加动态字段标签?

如何添加动态字段标签?
EN

Drupal用户
提问于 2018-08-13 08:01:48
回答 1查看 328关注 0票数 0

我有一个“医疗记录”内容类型,其中嵌入了一个“医疗过程”字段(type=paragraphs包,数值无限)。在段落中,its有访问时间、诊断、治疗、图像等,因此段落可以记录每一次访问。

但是,如果我们有很多段落,在视图页面中,它看起来很难看,所以我想在每个访问记录在这里输入图像描述(段落)中添加一些标签,例如“第一次访问”、“第二次访问”、“访问%...as”--我知道字段组只能显示一个静态标签,标记是不可用的。有人知道如何实现这一点吗?

EN

回答 1

Drupal用户

发布于 2018-08-13 12:22:13

可以在呈现之前使用hook_node_view更改视图。

代码语言:javascript
复制
/**
 *
 * @param type $node
 * @param type $view_mode
 * @param type $langcode
 * Implements hook_node_view().
 * To change paragraph title in node view.
 */
function MYMODULE_node_view($node, $view_mode = 'full', $langcode = NULL) {
  if ($node->type == 'CONTENT-TYPE-ID' && $view_mode == 'full') {
    foreach ($node->content['field_medical_records'] as $key => $paragraph_values) {
      if (is_numeric($key)) {
        $item_key = key($paragraph_values['entity']['paragraphs_item']);
        $node->content['field_medical_records'][$key]['entity']['paragraphs_item'][$item_key]['field_visit_time']['#title'] = numToOrdinalWord($item_key) . ' Visit';
      }
    }
  }
}

/**
 *
 * @param type $num
 * @return string
 * Implement to get Ordinal word from number.
 */
function numToOrdinalWord($num) {
  $first_word = array('eth', 'First', 'Second', 'Third', 'Fouth', 'Fifth', 'Sixth', 'Seventh', 'Eighth', 'Ninth', 'Tenth', 'Elevents', 'Twelfth', 'Thirteenth', 'Fourteenth', 'Fifteenth', 'Sixteenth', 'Seventeenth', 'Eighteenth', 'Nineteenth', 'Twentieth');
  $second_word = array('', '', 'Twenty', 'Thirty', 'Forty', 'Fifty');
  if ($num <= 20) {
    return $first_word[$num];
  }
  $first_num = substr($num, -1, 1);
  $second_num = substr($num, -2, 1);
  return $string = str_replace('y-eth', 'ieth', $second_word[$second_num] . '-' . $first_word[$first_num]);
}

您可以根据需要更改字段名。

票数 0
EN
页面原文内容由Drupal提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://drupal.stackexchange.com/questions/267454

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档