You are here

MultiStepFormBase.php in Activity 8

File

src/Form/MultiStepFormBase.php
View source
<?php

namespace Drupal\activity\Form;

use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Session\SessionManagerInterface;
use Drupal\user\PrivateTempStoreFactory;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Multi step form.
 */
abstract class MultiStepFormBase extends FormBase {

  /**
   * The tempstore factory.
   *
   * @var \Drupal\user\PrivateTempStoreFactory
   */
  protected $tempStoreFactory;

  /**
   * The session manager.
   *
   * @var \Drupal\Core\Session\SessionManagerInterface
   */
  private $sessionManager;

  /**
   * The current user.
   *
   * @var \Drupal\Core\Session\AccountInterface
   */
  private $currentUser;

  /**
   * The store.
   *
   * @var \Drupal\user\PrivateTempStore
   */
  protected $store;

  /**
   * Constructs a \Drupal\activity\Form\MultiStepFormBase.
   *
   * @param \Drupal\Core\Session\AccountInterface $current_user
   *   The current user.
   * @param \Drupal\Core\Session\SessionManagerInterface $session_manager
   *   The session manager.
   * @param \Drupal\user\PrivateTempStoreFactory $temp_store_factory
   *   The temp store factory.
   */
  public function __construct(AccountInterface $current_user, SessionManagerInterface $session_manager, PrivateTempStoreFactory $temp_store_factory) {
    $this->currentUser = $current_user;
    $this->sessionManager = $session_manager;
    $this->tempStoreFactory = $temp_store_factory;
    $this->store = $this->tempStoreFactory
      ->get('multistep_data');
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static($container
      ->get('current_user'), $container
      ->get('session_manager'), $container
      ->get('user.private_tempstore'));
  }

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {

    // Start a manual session for anonymous users.
    if ($this->currentUser
      ->isAnonymous() && !isset($_SESSION['multistep_form_holds_session'])) {
      $_SESSION['multistep_form_holds_session'] = TRUE;
      $this->sessionManager
        ->start();
    }
    return $form;
  }

  /**
   * Saves the data from the multistep form.
   */
  protected function saveData() {
  }

  /**
   * Set to null label and hook used for the multistep form.
   */
  protected function deleteStore() {
    $this->store
      ->delete('hook');
    $this->store
      ->delete('label');
  }

}

Classes

Namesort descending Description
MultiStepFormBase Multi step form.