Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
534 views
in Technique[技术] by (71.8m points)

drupal 8 - How can I filter a json:api request by drupal_internal__id?

I have a Drupal 8.9.10 site and I have installed the Events Logging module (https://www.drupal.org/project/events_logging), which logs entity operations into a custom entity called events_logging.

I can perform a generic GET request to http://mydomain/jsonapi/events_logging/events_logging and I get a result like this one:

{
  "jsonapi": {
    "version": "1.0",
    "meta": {
      "links": {
        "self": {
          "href": "http://jsonapi.org/format/1.0/"

        }
      }
    }
  },
  "data": [
    {
      "type": "events_logging--events_logging",
      "id": "da05a174-4ee3-4299-bd8c-fb612515df6b",
      "links": {
        "self": {
          "href": "http://mydomain/jsonapi/events_logging/events_logging/da05a174-4ee3-4299-bd8c-fb612515df6b"
        }
      },
      "attributes": {
        "drupal_internal__id": 1,
        "langcode": "en",
        "name": "Test1",
        "status": true,
        "created": "2020-12-27T15:24:14+00:00",
        "changed": "2020-12-27T15:24:14+00:00",
        "events_logging_type": "taxonomy_term_update",
        "operation": "update",
        "logpath": "/taxonomy/term/147/edit?destination=/admin/structure/taxonomy/manage/titolo_anagrafica/overview",
        "ref_numeric": 147,
        "description": "user admin (uid 1) performed update operation on entity Taxonomy term (id 147)",
        "info": null,
        "ip": "172.24.0.5",
        "ref_title": "Test1"
      },
      "relationships": {
        "user_id": {
          "data": {
            "type": "user--user",
            "id": "5c2be933-69bb-4a24-84bc-05845753b076"
          },
          "links": {
            "related": {
              "href": "http://mydomain/jsonapi/events_logging/events_logging/da05a174-4ee3-4299-bd8c-fb612515df6b/user_id"
            },
            "self": {
              "href": "http://mydomain/jsonapi/events_logging/events_logging/da05a174-4ee3-4299-bd8c-fb612515df6b/relationships/user_id"
            }
          }
        }
      }
    },
    {
      "type": "events_logging--events_logging",
      "id": "c083cb4d-58bf-4718-9d16-a3a6b700cda2",
      "links": {
        "self": {
          "href": "http://mydomain/jsonapi/events_logging/events_logging/c083cb4d-58bf-4718-9d16-a3a6b700cda2"
        }
      },
      "attributes": {
        "drupal_internal__id": 2,
        "langcode": "en",
        "name": "Test2",
        "status": true,
        "created": "2020-12-27T15:25:53+00:00",
        "changed": "2020-12-27T15:25:53+00:00",
        "events_logging_type": "taxonomy_term_insert",
        "operation": "insert",
        "logpath": "/admin/structure/taxonomy/manage/titolo_anagrafica/add",
        "ref_numeric": 149,
        "description": "user admin (uid 1) performed insert operation on entity Taxonomy term (id 149)",
        "info": null,
        "ip": "172.24.0.5",
        "ref_title": "Test2"
      },
      "relationships": {
        "user_id": {
          "data": {
            "type": "user--user",
            "id": "5c2be933-69bb-4a24-84bc-05845753b076"
          },
          "links": {
            "related": {
              "href": "http://mydomain/jsonapi/events_logging/events_logging/c083cb4d-58bf-4718-9d16-a3a6b700cda2/user_id"
            },
            "self": {
              "href": "http://mydomain/jsonapi/events_logging/events_logging/c083cb4d-58bf-4718-9d16-a3a6b700cda2/relationships/user_id"
            }
          }
        }
      }
    },
    {
      "type": "events_logging--events_logging",
      "id": "c2aaa557-6b3b-48f6-b332-cfb64b5685d6",
      "links": {
        "self": {
          "href": "http://mydomain/jsonapi/events_logging/events_logging/c2aaa557-6b3b-48f6-b332-cfb64b5685d6"
        }
      },
      "attributes": {
        "drupal_internal__id": 3,
        "langcode": "en",
        "name": "Test3",
        "status": true,
        "created": "2020-12-27T15:26:19+00:00",
        "changed": "2020-12-27T15:26:19+00:00",
        "events_logging_type": "taxonomy_term_update",
        "operation": "update",
        "logpath": "/taxonomy/term/149/edit",
        "ref_numeric": 149,
        "description": "user admin (uid 1) performed update operation on entity Taxonomy term (id 149)",
        "info": null,
        "ip": "172.24.0.5",
        "ref_title": "Test3"
      },
      "relationships": {
        "user_id": {
          "data": {
            "type": "user--user",
            "id": "5c2be933-69bb-4a24-84bc-05845753b076"
          },
          "links": {
            "related": {
              "href": "http://mydomain/jsonapi/events_logging/events_logging/c2aaa557-6b3b-48f6-b332-cfb64b5685d6/user_id"
            },
            "self": {
              "href": "http://mydomain/jsonapi/events_logging/events_logging/c2aaa557-6b3b-48f6-b332-cfb64b5685d6/relationships/user_id"
            }
          }
        }
      }
    }
  ],
  "links": {
    "self": {
      "href": "http://mydomain/jsonapi/events_logging/events_logging"
    }
  }
}

I would like to filter the resource by that "drupal_internal__id" so what I tried is: http://mydomain/jsonapi/events_logging/events_logging?filter[f][condition][path]=drupal_internal__id&filter[f][condition][operator]=%3D&filter[f][condition][value]=2

but what I get is an empty result:

{
  "jsonapi": {
    "version": "1.0",
    "meta": {
      "links": {
        "self": {
          "href": "http://jsonapi.org/format/1.0/"
        }
      }
    }
  },
  "data": [],
  "links": {
    "self": {
      "href": "http://mydomain/jsonapi/events_logging/events_logging?filter%5Bf%5D%5Bcondition%5D%5Boperator%5D=%3D&filter%5Bf%5D%5Bcondition%5D%5Bpath%5D=drupal_internal__id&filter%5Bf%5D%5Bcondition%5D%5Bvalue%5D=2"
    }
  }
}

I also tried intentionally misspelling the field name, just to make sure I got a 400 Bad Request.

Am I missing something?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Ok, turns out it was a permission issue.
For the JSON:API access I use a specific user with its role and permissions.
I tried with admin and I could filter the results.
Apparently the "View Events Logging entities" allows access to all the events, but if you want to filter them you need also the "Administer Event log entities" permission.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...