You are here

class ReferenceFormBuilder in Bibliography & Citation 8

Same name and namespace in other branches
  1. 2.0.x modules/bibcite_entity/src/ReferenceFormBuilder.php \Drupal\bibcite_entity\ReferenceFormBuilder

Provides reference form building and processing hotfix.

Hierarchy

Expanded class hierarchy of ReferenceFormBuilder

See also

https://www.drupal.org/project/bibcite/issues/2930990

1 string reference to 'ReferenceFormBuilder'
bibcite_entity.services.yml in modules/bibcite_entity/bibcite_entity.services.yml
modules/bibcite_entity/bibcite_entity.services.yml
1 service uses ReferenceFormBuilder
reference.form_builder in modules/bibcite_entity/bibcite_entity.services.yml
Drupal\bibcite_entity\ReferenceFormBuilder

File

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

Namespace

Drupal\bibcite_entity
View source
class ReferenceFormBuilder extends FormBuilder {

  /**
   * Get entity if stores in cache.
   *
   * If we have in cache our entity,
   * we need to apply it to ReferenceEntityFormBuilder.
   *
   * @return bool
   *   Return entity from cache. FALSE if not in cache.
   */
  public function restoreFromCache() {
    $request = $this->requestStack
      ->getCurrentRequest();
    $form_state = (new FormState())
      ->setFormState([]);
    $form_state
      ->setRequestMethod($request
      ->getMethod());
    $input = $form_state
      ->getUserInput();
    if (!isset($input)) {
      $input = $form_state
        ->isMethodType('get') ? $request->query
        ->all() : $request->request
        ->all();
    }
    if (isset($input['form_build_id'])) {
      $current_user_id = \Drupal::currentUser()
        ->id();
      $cid = 'bibcite_entity_populate:' . $current_user_id . ':' . $input['form_build_id'];
      if ($cache = \Drupal::cache()
        ->get($cid)) {
        \Drupal::cache()
          ->delete($cid);
        return $cache->data;
      }
    }
    return FALSE;
  }

  /**
   * {@inheritdoc}
   */
  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;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
FormBuilder::$classResolver protected property The class resolver.
FormBuilder::$csrfToken protected property The CSRF token generator to validate the form token.
FormBuilder::$currentUser protected property The current user.
FormBuilder::$elementInfo protected property The element info manager.
FormBuilder::$eventDispatcher protected property The event dispatcher.
FormBuilder::$formCache protected property The form cache.
FormBuilder::$formSubmitter protected property The form submitter.
FormBuilder::$formValidator protected property The form validator.
FormBuilder::$moduleHandler protected property The module handler.
FormBuilder::$requestStack protected property The request stack.
FormBuilder::$safeCoreValueCallables protected property Defines element value callables which are safe to run even when the form state has an invalid CSRF token.
FormBuilder::$themeManager protected property The theme manager.
FormBuilder::buildFormAction protected function Builds the $form['#action'].
FormBuilder::buttonWasClicked protected function Determines if a given button triggered the form submission.
FormBuilder::currentUser protected function Gets the current active user.
FormBuilder::deleteCache public function Deletes a form in the cache. Overrides FormCacheInterface::deleteCache
FormBuilder::doBuildForm public function Builds and processes all elements in the structured form array. Overrides FormBuilderInterface::doBuildForm
FormBuilder::doSubmitForm public function Handles the submitted form, executing callbacks and processing responses. Overrides FormSubmitterInterface::doSubmitForm
FormBuilder::elementTriggeredScriptedSubmission protected function Detects if an element triggered the form submission via Ajax.
FormBuilder::executeSubmitHandlers public function Executes custom submission handlers for a given form. Overrides FormSubmitterInterface::executeSubmitHandlers
FormBuilder::executeValidateHandlers public function Executes custom validation handlers for a given form. Overrides FormValidatorInterface::executeValidateHandlers
FormBuilder::getCache public function Fetches a form from the cache. Overrides FormCacheInterface::getCache
FormBuilder::getFileUploadMaxSize protected function Wraps file_upload_max_size().
FormBuilder::getForm public function Gets a renderable form array. Overrides FormBuilderInterface::getForm
FormBuilder::getFormId public function Determines the ID of a form. Overrides FormBuilderInterface::getFormId
FormBuilder::handleInputElement protected function Adds the #name and #value properties of an input element before rendering.
FormBuilder::prepareForm public function Prepares a structured form array. Overrides FormBuilderInterface::prepareForm
FormBuilder::processForm public function Processes a form submission. Overrides FormBuilderInterface::processForm
FormBuilder::rebuildForm public function Constructs a new $form from the information in $form_state. Overrides FormBuilderInterface::rebuildForm
FormBuilder::redirectForm public function Redirects the user to a URL after a form has been processed. Overrides FormSubmitterInterface::redirectForm
FormBuilder::renderFormTokenPlaceholder public function Renders the form CSRF token. It's a #lazy_builder callback.
FormBuilder::renderPlaceholderFormAction public function Renders a form action URL. It's a #lazy_builder callback.
FormBuilder::retrieveForm public function Retrieves the structured array that defines a given form. Overrides FormBuilderInterface::retrieveForm
FormBuilder::setCache public function Stores a form in the cache. Overrides FormCacheInterface::setCache
FormBuilder::setInvalidTokenError public function Sets a form_token error on the given form state. Overrides FormValidatorInterface::setInvalidTokenError
FormBuilder::submitForm public function Retrieves, populates, and processes a form. Overrides FormBuilderInterface::submitForm
FormBuilder::trustedCallbacks public static function Lists the trusted callbacks provided by the implementing class. Overrides TrustedCallbackInterface::trustedCallbacks
FormBuilder::validateForm public function Validates user-submitted form data in the $form_state. Overrides FormValidatorInterface::validateForm
FormBuilder::valueCallableIsSafe protected function Helper function to normalize the different callable formats.
FormBuilder::__construct public function Constructs a new FormBuilder.
FormBuilderInterface::AJAX_FORM_REQUEST constant Request key for AJAX forms that submit to the form's original route.
ReferenceFormBuilder::buildForm public function Builds and processes a form for a given form ID. Overrides FormBuilder::buildForm
ReferenceFormBuilder::restoreFromCache public function Get entity if stores in cache.
TrustedCallbackInterface::THROW_EXCEPTION constant Untrusted callbacks throw exceptions.
TrustedCallbackInterface::TRIGGER_SILENCED_DEPRECATION constant Untrusted callbacks trigger silenced E_USER_DEPRECATION errors.
TrustedCallbackInterface::TRIGGER_WARNING constant Untrusted callbacks trigger E_USER_WARNING errors.