2017-05-02 4 views
0

私のウェブサイトには、さまざまな人が投稿した多くの製品があります。各node.html.twigでは、オーナーごとに個人の連絡先フォームを表示するにはどうすればよいですか? Twig Tweak moduleを使用してフォームをレンダリングします。 "author"変数(Drupal\user\Entity)が使用されているとします。ノードのtwigテンプレートに個人用連絡先フォームを表示するにはどうすればよいですか?

私はhook_preprocess_nodeで試してみてください。

function MY_thEME_preprocess_node(&$variables) { 
$message = Drupal::entityTypeManager()->getStorage('contact_message')->create([ 
    'contact_form' => 'personal', 
    'recipient' => $user->id(), 
]); 

$form = \Drupal\Core\Entity\EntityFormBuilder::getForm($message); 
$form['#title'] = $this->t('Contact @username', ['@username' => $user->getDisplayName()]); 
$form['#cache']['contexts'][] = 'user.permissions'; 
$variables['personal_form'] = drupal_render($form); 
} 

答えて

0

あなたは、例えば、代わりに推奨されていませんdrupal_render()Renderer serviceを使用する必要があります

/** 
* Prepares variables for node templates. 
* 
* @see template_preprocess_node() 
*/ 
function mymodule_preprocess_node(&$variables) { 
    $message = Drupal::entityTypeManager()->getStorage('contact_message')->create([ 
    'contact_form' => 'personal', 
    'recipient' => $user->id(), 
    ]); 

    $form = \Drupal\Core\Entity\EntityFormBuilder::getForm($message); 
    $form['#title'] = $this->t('Contact @username', ['@username' => $user->getDisplayName()]); 
    $form['#cache']['contexts'][] = 'user.permissions'; 
    $variables['personal_form'] = \Drupal::service('renderer')->renderRoot($form); 
} 
関連する問題