1

I have a view that extarct a specific content type, this view shows the teaser view mode for rendering fields and not fields format : enter image description here

the teaser view mode is managed with display suite module to order content type's fields in custom regions.

Im using hook_node_view to add a new field conditionally :

function MYMODULE_node_view($node, $view_mode, $langcode){
    global $user;
    $uuid = $user->uid;
    switch ($node->type){
        case "rewards" : 

            if(!empty($node->field_reward_users) && isset($node->field_reward_users[LANGUAGE_NONE][0]['target_id'])){
                if($uuid == $node->field_reward_users[LANGUAGE_NONE][0]['target_id']){
                    $node->content['get_reward_link'] = array(
                        '#type' => 'markup',
                        '#markup' => l('Click Here', '/', array('attributes'=>array('class'=>'my-link-class'))),
                        '#weight' => 100,

                    );
                }
            }
           // dpm($node);
            break;
    }
}

when i debug i can see the new field in the $node object, but it's not rendered. Have you any idea how to add fields conditionally in my case?

Update

I added a new preprocess field ("pre_rewards") via display suite, like montionned in the answer of Reynold, then I added the following prerocess function :

function MYMODULE_preprocess_node(&$vars){

    if(isset($vars['preprocess_fields'])){
        foreach ($vars['preprocess_fields'] as $key => $pre){
            if($pre == 'pre_rewards' && isset($vars['content']['get_reward_link'])){
                $reward_link = drupal_render($vars['content']['get_reward_link']);
                $vars['preprocess_fields'][$key] = $reward_link;

            }
        }
    }
    dpm($vars);
}

and the same thing the element won't be rendered! and it exists in the $vars variable when i debug!

also the new field created exists in manage field page of my content type :

enter image description here So why it's always not rendering the content of this field in my page!?

Thanks in advance.

1

Seeing that you are using Display Suite, you should add the field via Display Suite as node.tpl.php is no longer being used. Display Suite has 4 Custom fields that you can add via the interface. You should look into the 'preprocess field'.

You need to define a preprocess field in Display Suite

enter image description here

and the machine name of the preprocess field

enter image description here

should correspond with the name of the field that you provide in your preprocess_node function:

function MYMODULE_preprocess_node(&$vars) {
  $vars['my_new_field'] = "some html or rendered field";
}

You are of course not limited to creating your field value in the preprocess_node function, you could just hand the value over like this:

function custom_preprocess_node(&$vars) {
  $vars['my_new_field'] = drupal_render($vars['content']['get_reward_link']);
}

The only limitation (AFAIK) is that you have to render the field before you let Display Suite pick it up.

Once you have defined your preprocess field like this, Display Suite will make it available in the manage display tab for the node type.

Also look at Theming with Display Suite and Preprocess Functions

  • thanks Reynolds for your clear answer, but the same thing with this solution, the new element exists when i debug the $vars variable, but not rendering any element! I'll update my question to add for you the new updated code. – Boussetta moez Jan 6 '15 at 9:02
  • 1
    You are not doing it correctly. Your DS preprocess machine name, and your $vars key must be the same: as in $vars['my_new_field'] in preprocess function must have machine name of 'my_new_field' in DS preprocess field. Where do you get $vars['preprocess_fields']. if you use that, then the machine name that you use in DS must be 'preprocess_fields' – J. Reynolds Jan 6 '15 at 10:59
  • I found in $vars a new element 'preprocess_fields', that's what i thaught that i must assign my needed markup to this array element... Anyway thank you very much you resolved my problem ;) – Boussetta moez Jan 6 '15 at 13:32
0

I think you need this extra field to the node type. Look for "hook_field_extra_fields()" and "hook_node_view()"

  • im using hook_node_view like montionned in the question, but my problem is why the rendered elements via this hook is not shown in the page! can you clarify more your answer ? – Boussetta moez Jan 6 '15 at 8: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.