0

我的网站有许多不同人发布的产品。在每个 node.html.twig 中,我如何显示每个所有者的个人联系表?我使用Twig Tweak 模块来呈现表单。假设我有“作者”变量 ( Drupal\user\Entity) 可供使用。

我在 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);
}
4

1 回答 1

0

您应该使用Renderer服务而不是drupal_render()已弃用,例如

/**
 * 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);
}
于 2017-08-05T10:52:25.240 回答