0

I have a "medical records" content type which has a "medical process" field embedded (type=paragraphs bundle, number of values unlimited). In the paragraph bundle its has visit time, diagnosis, treatments, images, etc., so the paragraph can record every visit.

Everything works well, however, if we have a lot of paragraphs, in the view page, it looks ugly, so I would like to add some labels to each visit record enter image description here(paragraph), such as "the first visit", "the second visit","the % visit)...as I know the field group can only show a static label, token not available. Do anybody know how to realize this?

enter image description here

1
  • Is it in D7 or D8?
    – Ajay Reddy
    Aug 13, 2018 at 11:54

1 Answer 1

0

You can use hook_node_view to change the view before rendering.

/**
 *
 * @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]);
}

You can change field names as per your requirement.

1
  • Hi Ajay, thank you very much for your kind help! I updated the code in node.api.php, unfortunately no luck. It seems no change for anything. I cleared the cache, still not work. I cannot see anywhere to add the dynamic labels. Isn't correct that I insert the above code into the node.api.php?
    – user59482
    Aug 13, 2018 at 16:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.