@jsondrop / drupal
Drupal 11的核心实体类型和帮助器
@jsondrop/core.
npm install @jsondrop/core @jsondrop/drupal
类型
导入包将Drupal核心的资源类型注册为 客户:
- 固定的类型:
user--user,file--file,menu_link_content--menu_link_content,path_alias--path_alias,以及配置实体(node_type,taxonomy_vocabulary,media_type,user_role,block_content_type); - 按实体类型回退:any
node--*是一个Node,taxonomy_term--*一个Term,media--*一个Media,block_content--*一个BlockContent,paragraph--*一个Paragraph,用基本字段键入。
通过扩展来添加你的bundle的字段:
import type { ToMany, ToOne } from '@jsondrop/core';
import type { File, ImageMeta, MediaImage, Node, Term, TextField } from '@jsondrop/drupal';
export interface Article extends Node<'article'> {
body: TextField | null;
field_image: ToOne<File<ImageMeta>>;
field_tags: ToMany<Term<'tags'>>;
field_hero: ToOne<MediaImage>;
}
declare module '@jsondrop/core' {
interface ResourceTypes {
'node--article': Article;
}
}
字段值类型: TextField, PathField, LinkField, FileUri,
ImageMeta, FileMeta, DateRangeField, AddressField.
标准配置文件媒体类型: MediaImage, MediaDocument, MediaAudio,
MediaVideo, MediaRemoteVideo.
助手
| 助手 | 需要 | |
|---|---|---|
translatePath(client, path) | decoupled_router | 解析路径;未找到时为NULL |
getResourceByPath(client, path, options) | decoupled_router | 通过重定向解析路径并获取其实体 |
getMenu(client, menu) / getMenuItems | jsonapi_menu_items | 作为排序树/平面列表的菜单 |
buildMenuTree(items) | 巢的链接 parent;也需要核心 menu_link_content | |
getView(client, view, display, { filters, arguments, sort, page }) | jsonapi_views | 视图显示结果和计数 |
image(client, field) | 图像字段为 { url, alt, title, width, height } | |
fileUrl(client, file) | 绝对文件URL | |
imageStyleUrl(file, style) | consumer_image_styles | 图像样式派生的URL |
用户
| 助手 | 需要 | |
|---|---|---|
getCurrentUser(client) / getCurrentUserId | 从入口点登录的用户(任何身份验证) me 联系;匿名时为NULL | |
login(client, { name, pass }) | Drupal会话登录(/user/login)、服务器端:返回会话cookie、CSRF和注销令牌 | |
logout(client, session) | 使用注销令牌结束会话 | |
isLoggedIn(client, cookie) | 会话是否仍然有效 | |
requestPasswordReset(client, { mail }) | 发送一个重置链接;对于未知账户的答案是一样的 |
有关OAuth登录(simple_oauth),请参见 OAuthClient 在
@jsondrop/core. 两者都显示在
例7.
const page = await getResourceByPath<Article>(drupal, '/blog/hello', {
query: { include: ['field_image', 'field_tags'] },
});
if (!page?.resource) notFound();