2

I am able to list blogs by a specific user, e.g. /blog/123 - where 123 is the uid, but when I use /blog/0 I get access denied.

Can someone advise on a config setting to enable /blog/0 to list blogs by anonymous user (i.e. uid = 0), please?

I could implement a hook or override the View or something, just wanted to check if there isn't a config setting somewhere. I couldn't see one, looking so far.

1 Answer 1

2

The blog route blog/%user_uid_optional route provided by the blog module performs a few checks in blog_page_user_access():

  // The visitor must be able to access the site's content.
  // For a blog to 'exist' the user must either be able to
  // create new blog entries, or it must have existing posts.
  return $account->uid && 
         user_access('access content') && 
         (user_access('create blog content', $account) ||      
          _blog_post_exists($account));

The $account->uid part of the conditional check will preclude access to blog/0 even if anonymous users can create blogs posts.

A module evoking hook_menu_alter() you can override this route behavior by providing your own access callback that excludes the $account->uid check:

/**
 * Implements hook_menu_alter().
 */
function MYMODULE_menu_alter(&$items) {
  if (isset($items['blog/%user_uid_optional'])) {
    $items['blog/%user_uid_optional']['access callback'] = 'MYMODULE_blog_page_user_access';
  }
}

function MYMODULE_blog_page_user_access($account) {
  return user_access('access content') && (user_access('create blog content', $account) || _blog_post_exists($account));
}
1
  • +1 upvote and accepted answer: 1) it explains why the URL is not possible out-of-the-boc 2) provides a solution. Thank you! Commented Oct 18, 2016 at 7:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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