Drupal9 JSON:API如何按Paragraphs字段过滤文章?
解决方案
问题根源
Paragraphs字段本质是实体引用字段,直接对该字段使用CONTAINS过滤时,匹配的是Paragraph实体的ID而非其内部文本内容,所以无法返回预期结果。
正确过滤方式
需要通过JSON:API的跨实体关系过滤,关联到Paragraph实体内部的具体文本字段进行匹配。以下是实现"标题或Paragraph文本包含关键词"的两种有效请求格式:
格式1:使用关系字段路径
/jsonapi/node/article?filter[group][conjunction]=OR&filter[title][operator]=CONTAINS&filter[title][value]=search term&filter[field_paragraphs.field_text][operator]=CONTAINS&filter[field_paragraphs.field_text][value]=search term
格式2:使用分隔符关联字段
/jsonapi/node/article?filter[group][conjunction]=OR&filter[title][operator]=CONTAINS&filter[title][value]=search term&filter[field_paragraphs--field_text][operator]=CONTAINS&filter[field_paragraphs--field_text][value]=search term
关键注意事项
- 替换占位符:将
field_paragraphs改为你文章类型中实际的Paragraph字段名,field_text改为Paragraph实体里存储文本的目标字段名(比如field_body、field_content等)。 - 权限验证:确保当前请求用户拥有访问Paragraph实体及其目标文本字段的权限,否则过滤会失效。
- 多Paragraph类型适配:如果使用了多种Paragraph类型,可额外添加
filter[field_paragraphs.type][value]=your_paragraph_type参数,限定仅匹配指定类型的Paragraph内容。
内容的提问来源于stack exchange,提问作者wot
相关产品推荐
相关产品推荐

