You are here

public function NodeForm::form in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/node/src/NodeForm.php \Drupal\node\NodeForm::form()

Gets the actual form array to be built.

Overrides ContentEntityForm::form

See also

\Drupal\Core\Entity\EntityForm::processForm()

\Drupal\Core\Entity\EntityForm::afterBuild()

File

core/modules/node/src/NodeForm.php, line 72
Contains \Drupal\node\NodeForm.

Class

NodeForm
Form controller 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().
  $uuid = $this->entity
    ->uuid();
  $store = $this->tempStoreFactory
    ->get('node_preview');

  // If the user is creating a new node, the UUID is passed in the request.
  if ($request_uuid = \Drupal::request()->query
    ->get('uuid')) {
    $uuid = $request_uuid;
  }
  if ($preview = $store
    ->get($uuid)) {

    /** @var $preview \Drupal\Core\Form\FormStateInterface */
    foreach ($preview
      ->getValues() as $name => $value) {
      $form_state
        ->setValue($name, $value);
    }

    // Rebuild the form.
    $form_state
      ->setRebuild();
    $this->entity = $preview
      ->getFormObject()
      ->getEntity();
    $this->entity->in_preview = NULL;

    // Remove the stale temp store entry for existing nodes.
    if (!$this->entity
      ->isNew()) {
      $store
        ->delete($uuid);
    }
    $this->hasBeenPreviewed = TRUE;
  }

  /** @var \Drupal\node\NodeInterface $node */
  $node = $this->entity;
  if ($this->operation == 'edit') {
    $form['#title'] = $this
      ->t('<em>Edit @type</em> @title', array(
      '@type' => node_get_type_label($node),
      '@title' => $node
        ->label(),
    ));
  }
  $current_user = $this
    ->currentUser();

  // Changed must be sent to the client, for later overwrite error checking.
  $form['changed'] = array(
    '#type' => 'hidden',
    '#default_value' => $node
      ->getChangedTime(),
  );
  $form['advanced'] = array(
    '#type' => 'vertical_tabs',
    '#attributes' => array(
      'class' => array(
        'entity-meta',
      ),
    ),
    '#weight' => 99,
  );
  $form = parent::form($form, $form_state);

  // Add a revision_log field if the "Create new revision" option is checked,
  // or if the current user has the ability to check that option.
  $form['revision_information'] = array(
    '#type' => 'details',
    '#group' => 'advanced',
    '#title' => t('Revision information'),
    // Open by default when "Create new revision" is checked.
    '#open' => $node
      ->isNewRevision(),
    '#attributes' => array(
      'class' => array(
        'node-form-revision-information',
      ),
    ),
    '#attached' => array(
      'library' => array(
        'node/drupal.node',
      ),
    ),
    '#weight' => 20,
    '#optional' => TRUE,
  );
  $form['revision'] = array(
    '#type' => 'checkbox',
    '#title' => t('Create new revision'),
    '#default_value' => $node->type->entity
      ->isNewRevision(),
    '#access' => $current_user
      ->hasPermission('administer nodes'),
    '#group' => 'revision_information',
  );
  $form['revision_log'] += array(
    '#states' => array(
      'visible' => array(
        ':input[name="revision"]' => array(
          'checked' => TRUE,
        ),
      ),
    ),
    '#group' => 'revision_information',
  );

  // Node author information for administrators.
  $form['author'] = array(
    '#type' => 'details',
    '#title' => t('Authoring information'),
    '#group' => 'advanced',
    '#attributes' => array(
      'class' => array(
        'node-form-author',
      ),
    ),
    '#attached' => array(
      'library' => array(
        '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'] = array(
    '#type' => 'details',
    '#title' => t('Promotion options'),
    '#group' => 'advanced',
    '#attributes' => array(
      'class' => array(
        'node-form-options',
      ),
    ),
    '#attached' => array(
      'library' => array(
        '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';
  $form['#entity_builders']['update_status'] = [
    $this,
    'updateStatus',
  ];
  return $form;
}