You are here

public function ReferenceForm::form in Bibliography & Citation 8

Same name and namespace in other branches
  1. 2.0.x modules/bibcite_entity/src/Form/ReferenceForm.php \Drupal\bibcite_entity\Form\ReferenceForm::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

modules/bibcite_entity/src/Form/ReferenceForm.php, line 18

Class

ReferenceForm
Form controller for Reference edit forms.

Namespace

Drupal\bibcite_entity\Form

Code

public function form(array $form, FormStateInterface $form_state) {

  // Try to restore from temp store, this must be done before calling.

  /** @var \Drupal\Core\TempStore\PrivateTempStoreFactory $tempStoreFactory */
  $tempStoreFactory = \Drupal::getContainer()
    ->get('tempstore.private');
  $store = $tempStoreFactory
    ->get('bibcite_reference_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');
    $entity = $preview
      ->getFormObject()
      ->getEntity();
    $entity->inPreview = NULL;
    $form_state
      ->set('has_been_previewed', TRUE);
  }
  $form = parent::form($form, $form_state);
  $operation = $this
    ->getOperation();
  $form['#title'] = $this
    ->t('<em>@operation @type</em> @title', [
    '@operation' => $operation !== 'default' ? ucfirst($operation) : $this
      ->t('Create'),
    '@type' => $this
      ->getBundleEntity()
      ->label(),
    '@title' => $this
      ->getEntity()
      ->label(),
  ]);
  return $form;
}