4

I am trying to alter field values in a page display. I can get the field values easily using field_get_items(), but depending on the values of other fields, I may need to manipulate the text and/or display based on some PHP logic in my template file. I am trying to do it using either hook_page_alter() or hook_preprocess_HOOK().

I cannot get the text to be altered when the page is displayed using hook_page_alter() or hook_preprocess_HOOK(). I can confirm those 2 functions are working and that I can pull the values from the DB ($page['content']), but I cannot override the variable to set them. For example:

function standard_submission_page_alter(&$page) {
    $node = menu_get_object();
    $page['content']['system_main']['nodes'][$node->nid]['field_title_nickname']['#items'][0]['value'] = 'A new nickname title!';
}

function standard_submission_preprocess_page(&$vars) {
    $vars['node']->field_title_nickname['und'][0]['value'] = 'A new nickname title 2x';
}

Any ideas on why my altering of this field_title_nickname text field value isn't being accepted and displayed? It keeps reverting back to whatever is originally in the database.

9

If I need to manipulate the text and/or display based on some PHP logic, I would do it using hook_node_view() in a custom module, not in a template file.

function MYMODULE_node_view($node, $view_mode = 'full', $langcode = NULL) {
    if( $view_mode == 'full' ){
        if( some conditions here ){
            $node->content['field_title_nickname'] = array(
                '#markup' => '<div>A new nickname title 2x</div>'
            );
        }
    }
}
  • Unfortunately hook_node_view() doesn't even run in my module. Even something as simple as die() inside hook_node_view() should cause a white screen of death, but that doesn't even get run. I have also tried hook_view, and that doesn't do anything either. And yes, I did remember to clear all caches and disable and reenable my module every time a change is made. I am trying to alter a content type I made in Drupal. My module does not create a new content type. Does this matter? – user785179 Jan 17 '13 at 18:00
  • Duh! My bad. It's called hook_node_VIEW for a reason. I kept checking on the entry form to add a new content. When I view an existing content then the hook_node_view is being invoked and working. – user785179 Jan 17 '13 at 18:18
3

You can do that by implementing template_preprocess_field(&$variables, $hook) in your template.php

function THEMENAME_preprocess_field(&$variables, $hook) {
  if ($node = menu_get_object()) {
    if ($node->type == 'car') {
      if($variables['element']['#field_name'] == 'field_price') {
        $variables['items']['0']['#markup'] = $new_value; //new value;
      }
    }
  }
}

Check here: http://tassaf.moriamedia.com/change-field-value-in-node-view/

  • Whilst this may theoretically answer the question, it would be preferable to include the essential parts of the answer here, and provide the link for reference. – mpdonadio Feb 2 '14 at 16:57
2

If use Display Suite implement hook_ds_pre_render_alter() to alter entity render array.

Example:

/**
 * Implements hook_ds_pre_render_alter().
 */
function MYMODULE_ds_pre_render_alter(&$layout_render_array, $context, &$vars) {
  $entity_type = $context['entity_type'];
  if ($entity_type == 'node') {
    $layout_render_array['ds_content']['Foo']['#markup'] = 'Bar';
  }
}

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.