1

How about two different functions like below? Is it possible to combine below two Drupal Template.php functions?

function mytheme_preprocess_node(&$vars)
function mytheme_views_pre_render(&$view)

Example:
function mytheme_preprocess_node(&$vars) {
if some field has option 1 selected
}
AND
function mytheme_views_pre_render(&$view) {
if a view is empty { attach alternative view}
}
1

You can do all of this inside the views_pre_render

function mytheme_views_pre_render(&$view) {

  if ($view->name == 'my_view_name' && $view->current_display == 'block_1') {
    if ($node = menu_get_object()) { // gets current page node object
      $field_name_val = $node->field_name[LANGUAGE_NONE][0]['value']; // get field value
      if ($field_name_val == 'option 1') {
        // your empty view code conditional goes here
        // if($view->result...
      }
    }
  }

}

You can find out the current_display name look here

| improve this answer | |
  • I've tried below with Profile2 field but it's breaking my site. Any idea? – user7938179 Jun 24 '18 at 22:17
0

I've tried below with a Profile2 field but it seems like Drupal doesn't like it..

function mytheme_views_pre_render(&$view) {
    if ($view->name == ‘view_name’ && $view->current_display == 'page_1') {

    $profile = profile2_load_by_user($user, 'main');
    $field_name_val=$profile->field_custom[‘und'][0]['value']

        if ($field_name_val == ‘0’) {

                if (empty($view->result)) {
                    $attachment = views_embed_view(‘view_name’’, ‘page_2’);
                    $view->attachment_after = $attachment;
            }
        }

        if ($field_name_val == ‘1’) {

                if (empty($view->result)) {
                     $attachment = views_embed_view(‘view_name’’, ‘page_3);
                     $view->attachment_after = $attachment;
            }
        }
}
}
| improve this answer | |

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.