0
  1. I have a View that shows the user's latest posts on his/her profile.
  2. If the user has no posts and it's own profile it show a link to encourage forum posting.
  3. If the user has no posts and it's someone else's profile, the view should not be there.

Currently the first 2 are ok, but the last one is not returning as expected. I'm getting an empty div (styled).

In the view's Empty text I have PHP code:

<?php
    global $user;
    if (something_get_username_from_url_alias() == $user->name) {
        echo '<span>You have no forum posts</span><br>';
        echo l('Go do some posting', 'forum');
    } // else scenario where block not shown
?>

If the question is not clear, please let me know. Thank you.

$handler = $view->new_display('block', 'Self Profile: User Posts', 'block_3');
$handler->override_option('fields', array(
  'subject' => array(
    'label' => '',
    'alter' => array(
      'alter_text' => 0,
      'text' => '',
      'make_link' => 0,
      'path' => '',
      'absolute' => 0,
      'link_class' => '',
      'alt' => '',
      'rel' => '',
      'prefix' => '',
      'suffix' => '',
      'target' => '',
      'help' => '',
      'trim' => 0,
      'max_length' => '',
      'word_boundary' => 1,
      'ellipsis' => 1,
      'html' => 0,
      'strip_tags' => 0,
    ),
    'empty' => '',
    'hide_empty' => 0,
    'empty_zero' => 0,
    'hide_alter_empty' => 1,
    'link_to_comment' => 1,
    'exclude' => 0,
    'id' => 'subject',
    'table' => 'comments',
    'field' => 'subject',
    'override' => array(
      'button' => 'Use default',
    ),
    'relationship' => 'none',
  ),
  'timestamp' => array(
    'label' => '',
    'alter' => array(
      'alter_text' => 0,
      'text' => '',
      'make_link' => 0,
      'path' => '',
      'absolute' => 0,
      'link_class' => '',
      'alt' => '',
      'rel' => '',
      'prefix' => '',
      'suffix' => '',
      'target' => '',
      'help' => '',
      'trim' => 0,
      'max_length' => '',
      'word_boundary' => 1,
      'ellipsis' => 1,
      'html' => 0,
      'strip_tags' => 0,
    ),
    'empty' => '',
    'hide_empty' => 0,
    'empty_zero' => 0,
    'hide_alter_empty' => 1,
    'date_format' => 'raw time ago',
    'custom_date_format' => '',
    'exclude' => 0,
    'id' => 'timestamp',
    'table' => 'comments',
    'field' => 'timestamp',
    'override' => array(
      'button' => 'Use default',
    ),
    'relationship' => 'none',
  ),
));
$handler->override_option('sorts', array(
  'timestamp' => array(
    'order' => 'DESC',
    'granularity' => 'second',
    'id' => 'timestamp',
    'table' => 'comments',
    'field' => 'timestamp',
    'override' => array(
      'button' => 'Use default',
    ),
    'relationship' => 'none',
  ),
));
$handler->override_option('arguments', array(
  'name' => array(
    'default_action' => 'default',
    'style_plugin' => 'default_summary',
    'style_options' => array(),
    'wildcard' => 'all',
    'wildcard_substitution' => 'All',
    'title' => '',
    'breadcrumb' => '',
    'default_argument_type' => 'php',
    'default_argument' => '',
    'validate_type' => 'none',
    'validate_fail' => 'empty',
    'glossary' => 0,
    'ignorecase' => 0,
    'limit' => '0',
    'case' => 'none',
    'path_case' => 'none',
    'transform_dash' => 0,
    'id' => 'name',
    'table' => 'comments',
    'field' => 'name',
    'validate_user_argument_type' => 'uid',
    'validate_user_roles' => array(
      2 => 0,
      4 => 0,
      3 => 0,
      5 => 0,
      6 => 0,
    ),
    'override' => array(
      'button' => 'Use default',
    ),
    'relationship' => 'none',
    'default_options_div_prefix' => '',
    'default_taxonomy_tid_term_page' => 0,
    'default_taxonomy_tid_node' => 0,
    'default_taxonomy_tid_limit' => 0,
    'default_taxonomy_tid_vids' => array(
      1 => 0,
      8 => 0,
      3 => 0,
      34 => 0,
      33 => 0,
      32 => 0,
      31 => 0,
      7 => 0,
      6 => 0,
    ),
    'default_argument_user' => 0,
    'default_argument_fixed' => '',
    'default_argument_php' => 'return something_get_username_from_url_alias();',
    'validate_argument_node_type' => array(
      'magento_group' => 0,
      'magento_product_4' => 0,
      'magento_product_4_global' => 0,
      'magento_product_4_website' => 0,
      'magento_store' => 0,
      'magento_website' => 0,
      'forum' => 0,
      'article' => 0,
      'blog' => 0,
      'expert' => 0,
      'ingredient' => 0,
      'page' => 0,
      'profile' => 0,
      'recipe' => 0,
      'webform' => 0,
    ),
    'validate_argument_node_access' => 0,
    'validate_argument_nid_type' => 'nid',
    'validate_argument_vocabulary' => array(
      1 => 0,
      8 => 0,
      3 => 0,
      34 => 0,
      33 => 0,
      32 => 0,
      31 => 0,
      7 => 0,
      6 => 0,
    ),
    'validate_argument_type' => 'tid',
    'validate_argument_transform' => 0,
    'validate_user_restrict_roles' => 0,
    'validate_argument_node_flag_name' => '*relationship*',
    'validate_argument_node_flag_test' => 'flaggable',
    'validate_argument_node_flag_id_type' => 'id',
    'validate_argument_user_flag_name' => '*relationship*',
    'validate_argument_user_flag_test' => 'flaggable',
    'validate_argument_user_flag_id_type' => 'id',
    'validate_argument_php' => 'return true;',
  ),
));
$handler->override_option('filters', array());
$handler->override_option('empty', '<?php
    global $user;
    if (something_get_username_from_url_alias() == $user->name) {
        echo \'<span>You have no forum posts</span><br>\';
        echo l(\'Go do some posting\', \'forum\');
    } // else scenario where block not shown
?>');
$handler->override_option('empty_format', '3');
$handler->override_option('items_per_page', 5);
$handler->override_option('row_plugin', 'fields');
$handler->override_option('row_options', array());
$handler->override_option('block_description', '');
$handler->override_option('block_caching', -1);
  • Note, you could use menu_get_object('user') to get the user object from user/uid system path and check username there – David Thomas Feb 22 '13 at 0:10
  • I'll check that out, thanks for the tip @DavidThomas... But that wont solve my issue =(... Anyone knows about this issue? – Manatax Feb 22 '13 at 0:33
1
  1. Use the No Results Behaviour (of type Text) under the Advanced section within the Views Module.

  2. In the text part of the behaviour, add your code

    if (something_get_username_from_url_alias() == $user->name) { echo 'You have no forum posts
    '; echo l('Go do some posting', 'forum'); } // else scenario where block not shown else { echo 'Nothing to see here...
    '; }

  • That would solve point 3 but null point 2 =( – Manatax Feb 22 '13 at 0:35
  • 1
    You could add a Text Area (as the No result behaviour), and add PHP code to check if the profile belongs to the current user. – user5681 Feb 22 '13 at 0:39
  • The textarea would not really change my situation, because I need for case 3 to be empty. If you see my code there, you will see that I am already validating if the the profile belongs to the current user. – Manatax Feb 22 '13 at 0:47
  • If User has posts -> View Displays If User has no posts, and is current user -> Show Text (which is PHP code to show link) Else -> Show Nothing. You paste your text into the behaviour. – user5681 Feb 22 '13 at 0:50
  • That part is working... if user has posts they are displaying just fine... the problem is that when the user has no posts, there are 2 options... Either its his own profile and he sees (Go post link) or its someone else's profile and nothing is shown (only that Views returns some white space inside a div) – Manatax Feb 22 '13 at 0:55

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.