function NodeForm::form

Same name in other branches
  1. 9 core/modules/node/src/NodeForm.php \Drupal\node\NodeForm::form()
  2. 8.9.x core/modules/node/src/NodeForm.php \Drupal\node\NodeForm::form()
  3. 11.x core/modules/node/src/NodeForm.php \Drupal\node\NodeForm::form()

Overrides ContentEntityForm::form

File

core/modules/node/src/NodeForm.php, line 90

Class

NodeForm
Form handler for the node edit forms.

Namespace

Drupal\node

Code

public function form(array $form, FormStateInterface $form_state) {
    // Try to restore from temp store, this must be done before calling
    // parent::form().
    $store = $this->tempStoreFactory
        ->get('node_preview');
    // Attempt to load from preview when the uuid is present unless we are
    // rebuilding the form.
    $request_uuid = \Drupal::request()->query
        ->get('uuid');
    if (!$form_state->isRebuilding() && $request_uuid && ($preview = $store->get($request_uuid))) {
        
        /** @var \Drupal\Core\Form\FormStateInterface $preview */
        $form_state->setStorage($preview->getStorage());
        $form_state->setUserInput($preview->getUserInput());
        // Rebuild the form.
        $form_state->setRebuild();
        // The combination of having user input and rebuilding the form means
        // that it will attempt to cache the form state which will fail if it is
        // a GET request.
        $form_state->setRequestMethod('POST');
        $this->entity = $preview->getFormObject()
            ->getEntity();
        $this->entity->in_preview = NULL;
        $form_state->set('has_been_previewed', TRUE);
    }
    
    /** @var \Drupal\node\NodeInterface $node */
    $node = $this->entity;
    if ($this->operation == 'edit') {
        $form['#title'] = $this->t('<em>Edit @type</em> @title', [
            '@type' => node_get_type_label($node),
            '@title' => $node->label(),
        ]);
    }
    // Changed must be sent to the client, for later overwrite error checking.
    $form['changed'] = [
        '#type' => 'hidden',
        '#default_value' => $node->getChangedTime(),
    ];
    $form = parent::form($form, $form_state);
    $form['advanced']['#attributes']['class'][] = 'entity-meta';
    $form['meta'] = [
        '#type' => 'details',
        '#group' => 'advanced',
        '#weight' => -10,
        '#title' => $this->t('Status'),
        '#attributes' => [
            'class' => [
                'entity-meta__header',
            ],
        ],
        '#tree' => TRUE,
        '#access' => $this->currentUser
            ->hasPermission('administer nodes'),
    ];
    $form['meta']['published'] = [
        '#type' => 'item',
        '#markup' => $node->isPublished() ? $this->t('Published') : $this->t('Not published'),
        '#access' => !$node->isNew(),
        '#wrapper_attributes' => [
            'class' => [
                'entity-meta__title',
            ],
        ],
    ];
    $form['meta']['changed'] = [
        '#type' => 'item',
        '#title' => $this->t('Last saved'),
        '#markup' => !$node->isNew() ? $this->dateFormatter
            ->format($node->getChangedTime(), 'short') : $this->t('Not saved yet'),
        '#wrapper_attributes' => [
            'class' => [
                'entity-meta__last-saved',
            ],
        ],
    ];
    $form['meta']['author'] = [
        '#type' => 'item',
        '#title' => $this->t('Author'),
        '#markup' => $node->getOwner()
            ->getAccountName(),
        '#wrapper_attributes' => [
            'class' => [
                'entity-meta__author',
            ],
        ],
    ];
    $form['status']['#group'] = 'footer';
    // Node author information for administrators.
    $form['author'] = [
        '#type' => 'details',
        '#title' => $this->t('Authoring information'),
        '#group' => 'advanced',
        '#attributes' => [
            'class' => [
                'node-form-author',
            ],
        ],
        '#attached' => [
            'library' => [
                'node/drupal.node',
            ],
        ],
        '#weight' => 90,
        '#optional' => TRUE,
    ];
    if (isset($form['uid'])) {
        $form['uid']['#group'] = 'author';
    }
    if (isset($form['created'])) {
        $form['created']['#group'] = 'author';
    }
    // Node options for administrators.
    $form['options'] = [
        '#type' => 'details',
        '#title' => $this->t('Promotion options'),
        '#group' => 'advanced',
        '#attributes' => [
            'class' => [
                'node-form-options',
            ],
        ],
        '#attached' => [
            'library' => [
                'node/drupal.node',
            ],
        ],
        '#weight' => 95,
        '#optional' => TRUE,
    ];
    if (isset($form['promote'])) {
        $form['promote']['#group'] = 'options';
    }
    if (isset($form['sticky'])) {
        $form['sticky']['#group'] = 'options';
    }
    $form['#attached']['library'][] = 'node/form';
    return $form;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.