I have a content type "Slider" which uses an Entity reference revisions field named "Slides" to save reference to various paragraphs that are then displayed on the frontend as a hero slider.

Everything works fine but now we have received a request of filtering out the slides by a show/hide boolean flag in the slide paragraph. Adding a boolean field to each such paragraph is straightforward but how do I filter those paragraphs based on it? I'm assuming a Drupal hook that allows a function to filter out the referenced entities by some arbitrary criterion but I've been unable to find it.

I understand that this question relates to a 3rd party module but it is quite possible that a core Drupal hook could be used in this context.

Any help will be greatly appreciated.


  • I'm possible to answer this question without knowing how the slides are loaded. Are you loading them via Views? or custom DB call code? What 3rd party module are you using?
    – No Sssweat
    Jun 22, 2020 at 9:42
  • The 3rd party module I'm using is Entity reference revisions (drupal.org/project/entity_reference_revisions). The paragraphs are associated with the "Slider" content type using this field and I only use "manage display" for the slider to display these slides so I'm guessing these paragraphs are being loaded the same way as any other field value is loaded? I'm not sure about it.
    – Ejaz
    Jun 22, 2020 at 10:17
  • The third party module is extending the core entity reference field, so I don't think the answer is specific to this module. The most generic solution is probably by access checking, for example in a hook_paragraph_access().
    – 4uk4
    Jun 22, 2020 at 11:37
  • @4k4 thanks for the suggestion. hook_paragraph_access() looks like the hook I have been search for :D I will try it out and post the results.
    – Ejaz
    Jun 22, 2020 at 14:07
  • @4k4 the hook_paragraph_access() which implements hook_ENTITY_TYPE_access() (api.drupal.org/api/drupal/…) worked out nicely for limiting access to paragraph entities based on their custom field but my field renderer still kept rendering empty containers for inaccessible paragraphs. The field formatter is part of yet another 3rd party module so I'm thinking it has to with that module's code. A perfect solution otherwise.
    – Ejaz
    Jun 24, 2020 at 10:26

1 Answer 1


I'd recommend doing this by creating a new Field Formatter by extending the default EntityReferenceRevisionsEntityFormatter (or extending whichever formatter you're using to display the paragraphs). If you're not sure how to create a field formatter, review the Create a custom field formatter documentation on d.o.

Specifically, you'll need to override the getEntitiesToView() method, something like this:

protected function getEntitiesToView(EntityReferenceFieldItemListInterface $items, $langcode) {
  $entities = parent::getEntitiesToView($items, $langcode);
  $allowed_entities = [];

  foreach($entities as $entity) {
    // Check if your show/hide checkbox is checked.
    // Note: I can't remember if this check is sufficient by itself.
    if (!$entity->my_show_hide_field->isEmpty()) {
      $allowed_entities[] = $entity;

  return $allowed_entities;

Lastly, you'll need to configure your entity reference revisions field to use this new formatter.

  • This looks great. I will try it out and post the results.
    – Ejaz
    Jun 22, 2020 at 14:06
  • I modified the 3rd party slider module's formatter to restrict access to paragraphs. Worked out nicely.
    – Ejaz
    Jun 24, 2020 at 10:27
  • Hi, just make sure you created a new formatter in your own custom module vs modifying the code in another module, otherwise your changes will get erased if you ever update that module!
    – sonfd
    Jun 24, 2020 at 10:32
  • Next step is to extend the formatter in question instead of modifying it.
    – Ejaz
    Jun 24, 2020 at 10:35

Your Answer

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

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