0

Update: My current solution is to use the Field formatter conditions module, to suppress output of the fullname field when a first name is present. It seems like it should be very simple to replace the value of the fullname field with the contents of the two name fields and hide those fields, but my node.tpl.php file seems not to do anything now that I'm using display suite and I can't figure out how to accomplish what I want in template.php. Some help with that would still be greatly appreciated.

Note: I would like not to create a module just for the purposes of wrangling a couple of fields. I am still looking for an answer to this, or a solid explanation of why a module is definitely the right way to go.

I'm probably taking the wrong approach to this. With everything I try to do, I seem to try every wrong way first before throwing up my hands and asking the internet to tell me the right way, and invariably the right way feels wrong.

Today, I'm trying to replace the value of one field in a paragraphs bundle item with the values of two others. Here is the relevant code from template.php:

function enviroscope_preprocess_paragraphs_items(&$variables, $hook) {
    $element = &$variables['element'];
    foreach ($element['#items'] as $delta => $item) {
        if ($element[$delta]['entity']['paragraphs_item'][$item['value']]['#bundle'] == 'doc_other_contrib_pb') {
            $person = &$element[$delta]['entity']['paragraphs_item'][$item['value']];
            if (isset($person['field_pb_doc_other_first_name']) && isset($person['field_pb_doc_other_family_name'])) {
                $person['field_pb_doc_other_fullname'][0]['#markup'] = $person['field_pb_doc_other_first_name'][0]['#markup'] . ' ' . mb_strtoupper($person['field_pb_doc_other_family_name'][0]['#markup']);
            }
        }
    }
}

I verified that the #markup element of the fullname field is updated, but everything still shows up the same. Is there anything I can do to rerender?

6
  • Is that a content type or user entity? – Shubham Mar 17 '16 at 9:33
  • It is a content type – UltraBob Mar 17 '16 at 9:34
  • You can change content of one field on display part using HOOK_node_view. Let me know if this solves your problem, I will post a complete solution for other users. – Shubham Mar 17 '16 at 9:36
  • Shubham could you provide a bit more e-mail or a link? – UltraBob Mar 17 '16 at 9:37
  • Note: this is a theme, not a module, if that makes a difference for using hook_node_view – UltraBob Mar 17 '16 at 9:42
0

If I need to manipulate the text and/or display based on some PHP logic, I would do it using hook_node_view(). Implement this HOOK in you custom module. Use of this module will be like.

function MYMODULE_node_view($node, $view_mode = 'full', $langcode = NULL) {
    if( $view_mode == 'full' ){
        if( $node->type == 'YourContentType' ){
            $node->content['field_pb_doc_other_first_name'] = array(
                '#markup' => $node->content['field_pb_doc_other_first_name']['#markup'] . ' ' . mb_strtoupper($node->content['field_pb_doc_other_family_name']['#markup']);
        }
    }
}

My logic may be wrong, you need to check the $node variable inside this function for all the fields value. The above code is just a sample code to demonstrate use of HOOK_node_view.

4
  • It doesn't seem correct to me, that I would need to write a module to show one field if two other fields don't exist, but show a combination of those two fields if they do. If this is really the best way, please provide some explanation as to why. – UltraBob Mar 17 '16 at 9:51
  • The theme and preprocess functions sometimes not get called on the cached pages because of cached renderable array, so it is best to use custom module and implementing HOOK_node_view. – Shubham Mar 17 '16 at 9:56
  • If it is a cached page, then getting my cached content is what I would expect and would hope would happen. I would not like drupal to have to rereplace these fields everytime someone views the page. – UltraBob Mar 17 '16 at 9:58
  • Drupal Cached the only database contents. If you want to update cache, than you need to do it manually by updating the cache using particular entity cache controlelr. – Shubham Mar 17 '16 at 9:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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