基本的に、TV(別名テンプレート変数)の値をデフォルトでユーザーの拡張フィールドの値に設定します。次の内容で
get_author
という名前のスニペットを作ってみました。
$user = $modx->getUser();
if (!$user) return '';
$profile = $user->getOne('Profile');
if (!$profile) return '';
$extended = $profile->get('extended');
return(isset($extended['author_page'])) ? $extended['author_page'] : '';
次のコードをTVのデフォルト値プロパティに追加します。
@EVAL return $modx->runSnippet('get_author');
コード自体は機能しているように見えますが、ユーザーがマネージャーで「デフォルトに設定」ボタンをクリックするまで、値はデフォルトで目的の値になりません。しかし、
8
(
@eval
なしで)のような静的な値を入力すると、フォームの読み込み時に値がすぐにデフォルトになります。これをどのように行う必要があるか誰にも教えてもらえますか?
前もって感謝します!
あなたがこれを試す方法は不可能です。デフォルト値は、リソースの保存時ではなく、テンプレート変数のレンダリング中に計算されます。デフォルトに設定をクリックするだけで、上記のように役立ちます。
次の2つの解決策があります。テンプレート変数の値を
OnDocFormSaveで実行されているプラグインの値に設定します。
$user = $modx->getUser());
($user) {
$profile = $user->getOne('Profile');
if ($profile) {
$extended = $profile->get('extended');
if (isset($extended['author_page'])) {
$resource->setTVValue('my_tv', $extended['author_page']);
}
}
}
または、createdbyまたはeditedbyの値をuserIdパラメータとして使用するスニペット「AuthorPage」でコードを実行します。
$output = '';
$userId = $modx->getOption('userId', $scriptProperties, 0);
$user = $modx->getUser($userId);
($user) {
$profile = $user->getOne('Profile');
if ($profile) {
$extended = $profile->get('extended');
if (isset($extended['author_page'])) {
$output = $extended['author_page'];
}
}
}
return $output;
次のようにそのスニペットを呼び出します。
[[AuthorPage? &userId=`[[*createdby]]`]]