How to attach programmatically a view to the edit node form in Drupal 8?

In D7 I could achieve this by using this code

 * Implements of hook_form_alter().
function MYMODULE_form_alter(&$form, &$form_state, $form_id){

  // target a single form only
  if ($form_id == "form_id") {

    $form['html_markup'] = [
      '#markup' => views_embed_view('name_of_view','default'),
      '#weight' => 1

I'm looking for a way how can it implemented in D8.

Any tips will be appreciated!

  • What doesn't work about this? hook_form_alter was virtually unchanged for D8.
    – Kevin
    Sep 21, 2018 at 13:16

2 Answers 2


I researched this for a while, then I found a sample implementation in modules/views/tests/modules/views_test_data/src/Form/ViewsTestDataElementEmbedForm.php:

$form['view'] = [
  '#type' => 'view',
  '#name' => 'test_view_embed',
  '#display_id' => 'embed_1',
  '#arguments' => [25],
  '#embed' => TRUE,

In that file it is used in FormBase::buildForm, but it works nicely also in hook_form_FORM_ID_alter(); I reported another example in this comment to a similar post.


In drupal 8 you can embed a view in two simple steps ,first get your desired view using $myView = Views::getView('VIEW_NAME'); and then make a rendered output using $rendered_view = $myView->buildRenderable('block_1',array($your_arguments));, then you can attach it to your node edit form.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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