You are here

class EntityLegalDocumentAcceptanceForm in Entity Legal 4.0.x

Same name and namespace in other branches
  1. 8.2 src/Form/EntityLegalDocumentAcceptanceForm.php \Drupal\entity_legal\Form\EntityLegalDocumentAcceptanceForm
  2. 3.0.x src/Form/EntityLegalDocumentAcceptanceForm.php \Drupal\entity_legal\Form\EntityLegalDocumentAcceptanceForm

Provides a confirmation form for deleting a custom block entity.

Hierarchy

Expanded class hierarchy of EntityLegalDocumentAcceptanceForm

1 file declares its use of EntityLegalDocumentAcceptanceForm
EntityLegalDocument.php in src/Entity/EntityLegalDocument.php

File

src/Form/EntityLegalDocumentAcceptanceForm.php, line 14

Namespace

Drupal\entity_legal\Form
View source
class EntityLegalDocumentAcceptanceForm extends FormBase {

  /**
   * The Entity Legal Document used by this form.
   *
   * @var \Drupal\entity_legal\EntityLegalDocumentInterface
   */
  protected $document;

  /**
   * The private temp store.
   *
   * @var \Drupal\Core\TempStore\PrivateTempStore
   */
  protected $tempStore;

  /**
   * Builds a new form instance.
   *
   * @param \Drupal\Core\TempStore\PrivateTempStoreFactory $private_temp_store_factory
   *   The private temp store factory service.
   */
  public function __construct(PrivateTempStoreFactory $private_temp_store_factory) {
    $this->tempStore = $private_temp_store_factory
      ->get('entity_legal');
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static($container
      ->get('tempstore.private'));
  }

  /**
   * Sets the legal document.
   *
   * @param \Drupal\entity_legal\EntityLegalDocumentInterface $document
   *   The legal document.
   */
  public function setDocument(EntityLegalDocumentInterface $document) {
    $this->document = $document;
  }

  /**
   * {@inheritdoc}
   */
  public function getFormId() {
    return 'entity_legal_document_acceptance_form';
  }

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
    $has_agreed = $this->document
      ->userHasAgreed();
    $form['agree'] = [
      '#title' => $this->document
        ->getAcceptanceLabel(),
      '#type' => 'checkbox',
      '#required' => TRUE,
      '#default_value' => $has_agreed,
      '#disabled' => $has_agreed,
    ];
    $form['submit'] = [
      '#value' => t('Submit'),
      '#type' => 'submit',
      '#access' => !$has_agreed,
    ];
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $published_version = $this->document
      ->getPublishedVersion();
    \Drupal::entityTypeManager()
      ->getStorage(ENTITY_LEGAL_DOCUMENT_ACCEPTANCE_ENTITY_NAME)
      ->create([
      'document_version_name' => $published_version
        ->id(),
    ])
      ->save();

    // Restore potential postponed messages and show them on the correct page.
    // @see \Drupal\entity_legal\Plugin\EntityLegal\Redirect::execute()
    if ($grouped_messages = $this->tempStore
      ->get('postponed_messages')) {
      foreach ($grouped_messages as $type => $messages) {
        foreach ($messages as $message) {
          $this
            ->messenger()
            ->addMessage($message, $type);
        }
      }
      $this->tempStore
        ->delete('postponed_messages');
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DependencySerializationTrait::$_entityStorages protected property
DependencySerializationTrait::$_serviceIds protected property
DependencySerializationTrait::__sleep public function 2
DependencySerializationTrait::__wakeup public function 2
EntityLegalDocumentAcceptanceForm::$document protected property The Entity Legal Document used by this form.
EntityLegalDocumentAcceptanceForm::$tempStore protected property The private temp store.
EntityLegalDocumentAcceptanceForm::buildForm public function Form constructor. Overrides FormInterface::buildForm
EntityLegalDocumentAcceptanceForm::create public static function Instantiates a new instance of this class. Overrides FormBase::create
EntityLegalDocumentAcceptanceForm::getFormId public function Returns a unique string identifying the form. Overrides FormInterface::getFormId
EntityLegalDocumentAcceptanceForm::setDocument public function Sets the legal document.
EntityLegalDocumentAcceptanceForm::submitForm public function Form submission handler. Overrides FormInterface::submitForm
EntityLegalDocumentAcceptanceForm::__construct public function Builds a new form instance.
FormBase::$configFactory protected property The config factory. 3
FormBase::$requestStack protected property The request stack. 1
FormBase::$routeMatch protected property The route match.
FormBase::config protected function Retrieves a configuration object.
FormBase::configFactory protected function Gets the config factory for this form. 3
FormBase::container private function Returns the service container.
FormBase::currentUser protected function Gets the current user.
FormBase::getRequest protected function Gets the request object.
FormBase::getRouteMatch protected function Gets the route match.
FormBase::logger protected function Gets the logger for a specific channel.
FormBase::redirect protected function Returns a redirect response object for the specified route.
FormBase::resetConfigFactory public function Resets the configuration factory.
FormBase::setConfigFactory public function Sets the config factory for this form.
FormBase::setRequestStack public function Sets the request stack object to use.
FormBase::validateForm public function Form validation handler. Overrides FormInterface::validateForm 72
LoggerChannelTrait::$loggerFactory protected property The logger channel factory service.
LoggerChannelTrait::getLogger protected function Gets the logger for a specific channel.
LoggerChannelTrait::setLoggerFactory public function Injects the logger channel factory.
MessengerTrait::$messenger protected property The messenger. 27
MessengerTrait::messenger public function Gets the messenger. 27
MessengerTrait::setMessenger public function Sets the messenger.
RedirectDestinationTrait::$redirectDestination protected property The redirect destination service. 1
RedirectDestinationTrait::getDestinationArray protected function Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url.
RedirectDestinationTrait::getRedirectDestination protected function Returns the redirect destination service.
RedirectDestinationTrait::setRedirectDestination public function Sets the redirect destination service.
StringTranslationTrait::$stringTranslation protected property The string translation service. 4
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.