1

I need a page to be half content, half node edit form.

How can I do this? I thought I'd put the content type's form display into a block. Sounds easy enough.

I've been trying contrib and custom modules, but I can't seem to grab the form.

Thought I could get away with something like this.

class EvaluationBlock extends BlockBase {

  /**
   * {@inheritdoc}
   */
  public function build() {
    $node = \Drupal::routeMatch()->getParameter('node');
    if ($node instanceof \Drupal\node\NodeInterface) {
      $render_array = EntityFormDisplay::collectRenderDisplay($node, 'project_evaluation_form');
      // error_log($render_array);
      return $render_array;
    }
  }
}
2
  • 1
    This is about rendering a custom form mode. The key to this is to add the form mode to the entity type, then you can build a form for the form display. See drupal.stackexchange.com/questions/255419/…
    – 4uk4
    Commented Jul 17, 2020 at 6:40
  • @4k4 Yes, thank you both. This was the missing piece. I thought I had tried this. I'm going to post the full code.
    – dmgig
    Commented Jul 17, 2020 at 14:54

1 Answer 1

1

Thanks to the comments above, the working module code is here:

project_evaluation_form_block.module

<?php
/**
 * @file
 */
 function project_evaluation_form_block_entity_type_build(array &$entity_types) {
   $entity_types['node']->setFormClass('project_evaluation_form', 'Drupal\node\NodeForm');
 }

src/Plugin/Block/EvaluationBlock.php

<?php
/**
 * @file
 * Contains \Drupal\article\Plugin\Block\EvaluationBlock.
 */

namespace Drupal\project_evaluation_form_block\Plugin\Block;

use Drupal\Core\Block\BlockBase;
use Drupal\Core\Form\FormInterface;
use Drupal\Core\Entity\Entity\EntityFormDisplay;

/**
 * Provides an 'evaluation' block.
 *
 * @Block(
 *   id = "evaluation_form_block",
 *   admin_label = @Translation("Evaluation form block"),
 *   category = @Translation("Evaluation form block test")
 * )
 */
class EvaluationBlock extends BlockBase {

  /**
   * {@inheritdoc}
   */
  public function build() {
    $node = \Drupal::routeMatch()->getParameter('node');
    $edit_form = \Drupal::entityTypeManager()->getFormObject('node', 'project_evaluation_form')->setEntity($node);
    return \Drupal::formBuilder()->getForm($edit_form);
  }
}

Had to adjust the submission settings for the content type to have the save button appear.

And with this, I am able to add this form as a block to a page.

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