저희 포럼은 사용자 상태 사용자 정의 기능을 많이 활용하고 있습니다. 최근, 해당 정보를 더 눈에 띄고 두드러지게 표시할 수 있는 방법에 대한 논의가 있었는데요. 마우스를 관련 아이콘에 올려야만 표시되던 사용자 상태를, 게시물에서 사용자 이름 아래에 항상 표시되도록 하는 것입니다.
이것이 가능할지 쉽게 구현할 수 있는 방법이 있는지 열심히 찾아봤지만, 아직까지 답을 찾지 못했습니다.
이것의 변형 버전을 만들어서, 게시자 이름 아래에 사용자 정의 필드를 표시하도록 했습니다. 테마 컴포넌트를 생성하고, JS에는 다음을 사용했습니다:
import Component from "@glimmer/component";
import { withPluginApi } from "discourse/lib/plugin-api";
import { get } from "@ember/object"; // We still need this
// here, add the id of the custom field, prefixed with "user_field_"
// to get the user field id:
// 1. go to admin/config/user-fields
// 2. click on the user field's name
// 3. the user field id is shown in the url after admin/config/user-fields/
const FIELDS_TO_SHOW = [
{ fieldKey: "user_field_2", display: "inline" },
{ fieldKey: "user_field_1", display: "block" }
];
// ----------------------------------------
function addCustomFields(api) {
FIELDS_TO_SHOW.forEach(config => {
api.renderAfterWrapperOutlet(
"post-meta-data-poster-name",
class extends Component {
// getter to pass the key to the template
get currentFieldKey() {
return config.fieldKey;
}
// getter to pass the display type to the template
get currentDisplayType() {
return config.display;
}
static shouldRender(args) {
// use config.fieldKey to find the data
const value = get(args.post.user.custom_fields, config.fieldKey);
return !!value;
}
<template>
<div class="
poster-custom-field
poster-custom-field-{{this.currentDisplayType}}
field-{{this.currentFieldKey}}
">
{{get @post.user.custom_fields this.currentFieldKey}}
</div>
</template>
}
);
});
}
export default {
name: "show-poster-custom-fields",
initialize() {
withPluginApi("1.0.0", (api) => {
addCustomFields(api);
});
}
};
그리고 CSS에는 다음을 사용했습니다:
.topic-meta-data .names {
flex-wrap: wrap;
align-items: baseline;
}
// base styles for all custom field display
.poster-custom-field {
font-size: 0.9em;
color: var(--primary-high-or-secondary-low);
flex-shrink: 0;
}
// style for inline display
.poster-custom-field-inline {
display: inline-block;
margin-left: 8px;
order: 5;
}
.topic-meta-data .names .user-title {
order: 10;
flex-basis: 100%;
width: 100%;
display: block;
margin-left: 0;
margin-top: 2px;
font-size: 0.9em;
color: var(--primary-high-or-secondary-low);
}
// style for below the name display
.poster-custom-field-block {
flex-basis: 100%;
margin-top: 4px;
order: 20;
}
실제로 적용하면 아래 이미지와 같습니다. "more stuff"와 "some information"은 두 사용자 정의 필드에 있는 내용입니다.