Option to remove all unpublished entities from the results.

Created on 14 September 2023, over 1 year ago
Updated 6 November 2023, about 1 year ago

Problem/Motivation

I would like the option to enable or disable unpublished entities from the results of my queries. I don't want to deal with the unpublished entities in my front end application and therefor would like to have filtered them out from the beginning.

Proposed resolution

As you can see in the screenshot I have a reference field where one of the referenced items is an unpublished item (status = false). I don't want to get this result and would like to only get the once that have status true.

āœØ Feature request
Status

Fixed

Version

2.0

Component

Code

Created by

šŸ‡§šŸ‡ŖBelgium cedricl

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

  • Issue created by @cedricl
  • šŸ‡¦šŸ‡ŗAustralia almunnings Melbourne, šŸ‡¦šŸ‡ŗ

    šŸ‘

    Could be tricky. Could be easy. Iā€™m keen. Will have a look this weekend.

  • šŸ‡¦šŸ‡ŗAustralia almunnings Melbourne, šŸ‡¦šŸ‡ŗ

    Users without access to view unpublished data wouldn't see the unpublished items, but your request is specifically asking for unpublished items to always be hidden from entity reference fields.

    My gut feel is this should stay as permissions, and Drupal already has a way for you to achieve this.

    But perhaps a hook could do what you need?

    use Drupal\Core\Cache\RefinableCacheableDependencyInterface;
    use Drupal\Core\Entity\EntityPublishedInterface;
    use Drupal\Core\Field\FieldItemListInterface;
    
    /**
     * Implements hook_graphql_compose_field_results_alter().
     */
    function MYMODULE_graphql_compose_field_results_alter(array &$results, array $context, RefinableCacheableDependencyInterface $metadata) {
      $field_list = $context['value'] ?? NULL;
      if (!$field_list instanceof FieldItemListInterface) {
        return;
      }
    
      if ($field_list->getFieldDefinition()->getType() === 'entity_reference') {
        $results = array_filter(
          $results,
          fn ($entity) => ($entity instanceof EntityPublishedInterface) ? $entity->isPublished() : TRUE
        );
      }
    }
    
  • šŸ‡¦šŸ‡ŗAustralia darvanen Sydney, Australia

    Could this perhaps be repurposed as a feature request for filters on edge queries?

  • šŸ‡¦šŸ‡ŗAustralia almunnings Melbourne, šŸ‡¦šŸ‡ŗ

    Demand for this feature has recently skyrocketed 200%

    I'll look into it more.

  • Status changed to Needs review over 1 year ago
  • šŸ‡¦šŸ‡ŗAustralia almunnings Melbourne, šŸ‡¦šŸ‡ŗ

    Ok, deployed to develop

    https://git.drupalcode.org/project/graphql_compose/-/commit/bfec317fefee...

    Its in there somewhere. Lots to unpack.
    I've applied it to entity references and edges.

    Let me know how you go with your testing!

  • šŸ‡¦šŸ‡ŗAustralia darvanen Sydney, Australia

    Tried to test, I:

    • Pulled latest 2.0.x
    • Cleared cache (on my contrib rig, no other modules installed except dependencies)
    • Searched for new options in /admin/config/graphql_compose
    • Checked the explorer

    I can't see where/how to set a filter on the query, what am I missing?

  • šŸ‡¦šŸ‡ŗAustralia almunnings Melbourne, šŸ‡¦šŸ‡ŗ

    It should be under Advanced settings at admin/config/graphql_compose/settings

    Exclude unpublished entities which will be on by default for new installs

  • Status changed to RTBC over 1 year ago
  • šŸ‡¦šŸ‡ŗAustralia darvanen Sydney, Australia

    Ahah! The one place I didn't look.

    Yes, this passes manual testing of a simple unpublished article. Very nice. I believe this meets the criteria of the IS as it stands.

  • šŸ‡§šŸ‡ŖBelgium cedricl

    Does exactly what I wanted. Thanks for the help @almunnings

  • šŸ‡¦šŸ‡ŗAustralia almunnings Melbourne, šŸ‡¦šŸ‡ŗ

    In 2.0.0 cheers

  • Status changed to Fixed about 1 year ago
  • šŸ‡¦šŸ‡ŗAustralia almunnings Melbourne, šŸ‡¦šŸ‡ŗ
Production build 0.71.5 2024