You are here

public function ReferenceFormBuilder::buildForm in Bibliography & Citation 2.0.x

Same name and namespace in other branches
  1. 8 modules/bibcite_entity/src/ReferenceFormBuilder.php \Drupal\bibcite_entity\ReferenceFormBuilder::buildForm()

Builds and processes a form for a given form ID.

The form may also be retrieved from the cache if the form was built in a previous page load. The form is then passed on for processing, validation, and submission if there is proper input.

Parameters

\Drupal\Core\Form\FormInterface|string $form_arg: The value must be one of the following:

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Return value

array The rendered form. This function may also perform a redirect and hence may not return at all depending upon the $form_state flags that were set.

Throws

\Drupal\Core\Form\FormAjaxException Thrown when a form is triggered via an AJAX submission. It will be handled by \Drupal\Core\Form\EventSubscriber\FormAjaxSubscriber.

\Drupal\Core\Form\EnforcedResponseException Thrown when a form builder returns a response directly, usually a \Symfony\Component\HttpFoundation\RedirectResponse. It will be handled by \Drupal\Core\EventSubscriber\EnforcedFormResponseSubscriber.

Overrides FormBuilder::buildForm

See also

self::redirectForm()

File

modules/bibcite_entity/src/ReferenceFormBuilder.php, line 51

Class

ReferenceFormBuilder
Provides reference form building and processing hotfix.

Namespace

Drupal\bibcite_entity

Code

public function buildForm($form_id, FormStateInterface &$form_state) {
  $form = parent::buildForm($form_id, $form_state);
  $form_id = $this
    ->getFormId($form_id, $form_state);
  $input = $form_state
    ->getUserInput();
  $check_cache = isset($input['form_id']) && $input['form_id'] == $form_id && !empty($input['form_build_id']);
  if (!$check_cache) {

    // If it is first form build (form not cached),
    // we need to save entity for second form build.
    $info = $form_state
      ->getBuildInfo();
    $callback_object = $info['callback_object'];
    $entity = $callback_object
      ->getEntity();
    $current_user_id = \Drupal::currentUser()
      ->id();
    \Drupal::cache()
      ->set('bibcite_entity_populate:' . $current_user_id . ':' . $form['#build_id'], $entity, \Drupal::time()
      ->getRequestTime() + 3600);
  }
  return $form;
}