My website has many products posted by different people. In each node.html.twig, How can i show the personal contact form for each owner?
I use the Twig Tweak module to render form.
Suppose I have "author" variable (Drupal\user\Entity
) for use.
I try in 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);
}
You should use
Renderer
service instead ofwhich is deprecated, e.g.drupal_render()