[Drupal] How to expand NodeForm so new argument can passed to it

After you have integrated the form class in the entity type as form mode

function mymodule_entity_type_build(array &$entity_types) {
  $entity_types['node']->setFormClass('service', 'Drupal\mymodule\Form\ServiceBuilderForm');
}

and invoked the node form in a controller

$form = $this->entityFormBuilder()->getForm($node, 'service');

or without controller directly in a route:

_entity_form: 'node.service'

the entity including the field data is passed to your form

class ServiceBuilderForm extends NodeForm {
  /**
   * {@inheritdoc}
   */
  public function form(array $form, FormStateInterface $form_state) {
    $node = $this->entity;
    if ($node->getType() == 'service_request') {
      $field_value = $node->field_service_type->value;
  

and you avoid the error from your question that the entity is null.

Tags:

Forms

Nodes

9