5

I have read Hide an entity with hook_entity_view_alter(), and by all rights this code should hide every paragraph on a page:

/**
 * Implements hook_ENTITY_TYPE_view_alter().
 */
function mymodule_paragraph_view_alter(array &$build, EntityInterface $entity, EntityViewDisplayInterface $display) {
  $build['#access'] = false;
}

There are no errors, I know the function is being called because the debugger stops there on a breakpoint, I've cleared cache with every test.

I've tried hook_entity_view, hook_entity_view_alter, hook_ENTITY_TYPE_view and hook_ENTITY_TYPE_view_alter.

None of them have any effect whether I'm logged in or not.

['#access'] is not showing up in the build array in any of these hooks which makes me think the advice might be out of date since 8.4 dropped? If it does work - why should it work?

Are there any other ways, better ways of hiding paragraph entities (or any entity really)?

Obviously there will be some logic involved later which is why I want to do it in one of these hooks (or similar).


Edit 1: It has been suggested that there should be a $build['content'] array or similar. There is only the entity object in whichever hook I try of the four I have listed:

inspect build variable

3
  • 1
    Inspect the $build variable. Are you sure it shouldn't be something like $build['content']['#access'] = false?
    – Felix Eve
    Commented Oct 19, 2017 at 23:06
  • I'd try try adding that #access property everywhere until something sticks. Maybe to #paragraph? Maybe the each field separately? If all else fails you could always try unsetting stuff?
    – Felix Eve
    Commented Oct 20, 2017 at 0:27
  • Hehehe, yeah... that's why I'm here, I tried that already =)
    – Darvanen
    Commented Oct 20, 2017 at 0:41

3 Answers 3

1

Try this:

$build['#printed'] = TRUE
1

$build['#printed'] = TRUE works but is heavily cached..you can set your cache though or still try $build['body']['0']['#access'] = FALSE; OR

$build['filter_this_view']['actions']['submit']['#access'] = FALSE;

depending on your $build array structure. The fields will not be cached

1

I know it didn't work for you, but I am using mymodule_comment_view_alter() (i.e. hooking into hook_ENTITY_TYPE_view_alter) to selectively hide individual comments.

Although $build didn't have an '#access' key, eventually I tried

$build['#access'] = false;

and that does work, for comments anyway.

Also I don't need to clear the cache at all whilst editing the hook (I do have Twig debugging on it local.services.yml, but with twig.config.cache still enabled.)

(Drupal 8.5.5)

1
  • Unfortunately this question is specifically about paragraph entities, not comments. The bit about 'any other entity really' is in reference to other ways of achieving this :)
    – Darvanen
    Commented Jul 12, 2018 at 0:19

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.