Why Gemfury? Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Debian packages RPM packages NuGet packages

Repository URL to install this package:

Details    
novicell/custom_forms / src / Plugin / Block / CustomFormReceiptBlock.php
Size: Mime:
<?php


namespace Drupal\custom_forms\Plugin\Block;


use Drupal;
use Drupal\Core\Annotation\Translation;
use Drupal\Core\Block\Annotation\Block;
use Drupal\Core\Block\BlockBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\custom_forms\Entity\CustomFormSubmission;
use Drupal\dds\Service\SessionService;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Throwable;

/**
 * Class CustomFormBlock
 *
 * @package Drupal\custom_forms\Plugin\Block
 *
 * @Block(
 *   id = "custom_form_receipt_block",
 *   admin_label = @Translation("Receipt"),
 *   category = @Translation("Custom Forms"),
 * )
 */
class CustomFormReceiptBlock extends BlockBase implements ContainerFactoryPluginInterface {

  /**
   * @var \Drupal\dds\Service\SessionService
   *   The session service to ensure we have access to a proper session.
   */
  protected $sessionService;

  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
    return new static(
      $configuration,
      $plugin_id,
      $plugin_definition,
      $container->get('dds.session')
    );
  }

  public function __construct(
    array $configuration,
    $plugin_id,
    $plugin_definition,
    SessionService $session_service
  ) {
    parent::__construct($configuration, $plugin_id, $plugin_definition);
    $this->sessionService = $session_service;
  }

  /**
   * {@inheritdoc}
   */
  public function build() {
    $build = [];
    $query = Drupal::request()->query;

    $cache_contexts   = $this->getCacheContexts();
    $cache_tags       = $this->getCacheTags();
    // Disable caching of receipt data.
    $cache_max_age    = 0;
    // Just in case we also add the cache context to the query parameter we use.
    $cache_contexts[] = 'url.query_args:S';

    if (
      $query->has('S') &&
      !empty($query->get('S'))
    ) {
      /** @var \Drupal\custom_forms\Entity\CustomFormSubmissionInterface $submission */
      $submission = CustomFormSubmission::loadByUniqueId($query->get('S'));

      // Only continue if submission isn't null.
      if ($submission === NULL) {
        return $build;
      }

      try {
        $data = $submission->getData();

        // Make sure we have a submission token.
        if (empty($data['_submission_token'])) {
          return $build;
        }

        $tempstore  = $this->sessionService->getTempStore('private', 'custom_form_submission_'.$submission->id());
        $token      = $tempstore->get('submission_token');

        // If the token that was stored in the user's session matches that of
        // the submission, we can render the receipt.
        if ($token === $data['_submission_token']) {

          // Add the template and data to populate it.
          $build['#theme']          = 'submission_receipt';
          $build['#submission']     = $submission;
        }

        unset($data);
      } catch (Throwable $e) {
        $error_message = t('Failed preparing submission data for receipt template. Message = %message', [
          '%message' => $e->getMessage()
        ]);
        \Drupal::logger('Custom Forms')->error($error_message);
      }
    }

    $build['#cache']['contexts'] = $cache_contexts;
    $build['#cache']['tags'] = $cache_tags;
    $build['#cache']['max-age'] = $cache_max_age;

    return $build;
  }

  /**
   * {@inheritdoc}
   */
  public function blockForm($form, FormStateInterface $form_state) {
    $form = parent::blockForm($form, $form_state);

    $form['description'] = [
      '#type' => 'container',
      'message' => ['#markup' => $this->t('This block does not have any configuration.')]
    ];

    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function defaultConfiguration() {
    $configuration = parent::defaultConfiguration();

    $configuration['label_display'] = 'hidden';

    return $configuration;
  }

  /**
   * {@inheritdoc}
   */
  public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
    $form = parent::buildConfigurationForm($form, $form_state);

    // Hide the description
    $form['admin_label']['#type'] = 'hidden';

    // Hide the label field.
    $form['label']['#type'] = 'value';
    $form['label_display']['#type'] = 'value';

    return $form;
  }

}