8

How to get the side-wide contact form (feedback) individually displayed in a custom block or other page then /contact (e.g. frontpage)?

I've tried this code, but it is not working (are contact forms entities?):

// get default form
// get the entity object
$default_form = \Drupal::config('contact.settings')->get('default_form');
$entity = \Drupal::entityManager()->getStorage('contact_form')->load($default_form);

// get view builder
// render view
$view_builder = \Drupal::entityManager()->getViewBuilder('contact_form');
$full_output = $view_builder->view($entity);
  • INFO: 1) the default side-wide contact form (feedback) is an entity (not so the login form). 2) entityManager is deprecated in favour of entityTypeManager (API docs) – nilsun Apr 6 '16 at 2:51
  • I would like to refer to a new main D8 question to help with all sub-questions like this here, caused by the open basic question, well documented here in details: drupal.stackexchange.com/questions/197149/… – nilsun Apr 6 '16 at 18:48
6

Contact form is an entity, but you can't view it.

What you want instead is an add form of a contact_message with contact_form as bundle.

See ContactController::contactSitePage() (EDIT: API docs) as example:

$message = \Drupal::entityTypeManager()
  ->getStorage('contact_message')
  ->create(array(
    'contact_form' => $contact_form->id(),
  ));

// This works in a controller, use \Drupal::service('entity.form_builder') elsewhere.
$form = $this->entityFormBuilder()->getForm($message);
$form['#title'] = SafeMarkup::checkPlain($contact_form->label());

EDIT (thanks @nilsun): Note that entityManager is deprecated in favour of entityTypeManager.

  • INFO: entityManager is deprecated in favour of entityTypeManager (API docs) and some links are missing here in the answer. In the answer it states: See ContactController::contactSitePage() as example. Where is it? Is it a doc page on Drupal.org? Is it a function in core? How to implement this in a preprocess function since the question was about .theme? Things have changed end of last year and there are missing informations about the final D8 core regarding such questions in most answers. – nilsun Apr 6 '16 at 2:41
  • I would like to refer to a new main D8 question to help with all sub-questions like this here, caused by the open basic question, well documented here in details: drupal.stackexchange.com/questions/197149/… – nilsun Apr 6 '16 at 18:49
  • String::checkPlain is now SafeMarkup::checkPlain – pcambra Jun 15 '16 at 10:56
  • 1
    @pcambra Thanks, note that anyone can update answers, you can just fix something like that yourself. – Berdir Jun 18 '16 at 18:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.