2

If you enable the contributed Blog module, a link with the anchor text "View most recent blog entries" shows up on every post. I want to hide this link using PHP (i.e. not resorting to css or javascript).

So far I've tried this in hook_node_links_alter():

unset($links['usernames_blog']['#links']['blog_usernames_blog']);

It doesn't work. I don't think the offending link is in the links array, but somewhere else (I've yet to figure out where).

  • I've never attempted to enable the Blog module so I'm just taking a guess here but does the view your want to disable show up in the View menu (structure->views)? If so you should easily be able to disable it with the buttons on the far right. – mrlexington Jul 31 at 15:00
  • Is there any reason to use the Blog module at all? Seems to me its sole purpose is to make migration from earlier versions easier. – Alfred Armstrong Jul 31 at 15:50
  • @AlfredArmstrong It may be that I would be better off without it. I am migrating a Drupal 7 multi-user blog to Drupal 8, and it seemed a good idea at the time to continue using the Blog module. I'll explore alternative approaches if I am unable to get rid of this misfeature. – Free Radical Jul 31 at 16:14
  • 1
    @mrlexington I do not want to disable a view. I want to hide a link that shows up on every node on the site (whether the user who authored the node actually has a blog or not) – Free Radical Jul 31 at 16:27
  • Enable Devel's submodule Kint and then ksm($variables); inside the hook in question, flush cache and then check what's in that array. Maybe you need to ensure your implementation runs last via hook_implements_alter, but that's just a wild guess. Same as you I would need to look into the Blog module's code to find out how the links got added in the first place. – leymannx Jul 31 at 18:15
3

unset($links['usernames_blog']['#links']['blog_usernames_blog']);

Might be correct, but make sure your module has higher weight, so your hook alter fires after the blog's module.

I suggest using the weight module to give your custom module higher weight. Else, you could run on Devel's Execute PHP module_set_weight('mymodule', 11); to give it higher weight.


If that doesn't work, If you look at the module's code, there is another link that gets added via hook_ENTITY_TYPE_view

Thus, all you gotta do is use hook_ENTITY_TYPE_view_alter

mymodule.module

/**
 * Implements hook_ENTITY_TYPE_view_alter().
 */
function mymodule_user_view_alter(array &$build, Drupal\Core\Entity\EntityInterface $entity, \Drupal\Core\Entity\Display\EntityViewDisplayInterface $display) {
  if (!empty($build['summary']['blog'])) {
    unset($build['summary']['blog']);
  }
}
  • Thanks! Using hook_ENTITY_TYPE_view_alter() to unset($build['summary']['blog']);is the correct solution. – Free Radical Aug 1 at 15:35

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.