CaptchaPointForm.php 3.5 KB
Newer Older
1 2 3 4
<?php

namespace Drupal\captcha\Form;

5
use Drupal\captcha\Service\CaptchaService;
6 7
use Drupal\Core\Entity\EntityForm;
use Drupal\Core\Form\FormStateInterface;
8 9
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\RequestStack;
10

11
/**
12
 * Entity Form to edit CAPTCHA points.
13
 */
14 15
class CaptchaPointForm extends EntityForm {

16 17 18 19 20 21 22
  /**
   * The CAPTCHA helper service.
   *
   * @var \Drupal\captcha\Service\CaptchaService
   */
  protected $captchaService;

23
  /**
24 25
   * The request stack.
   *
26 27 28 29 30 31 32 33
   * @var \Symfony\Component\HttpFoundation\RequestStack
   */
  protected $requestStack;

  /**
   * CaptchaPointForm constructor.
   *
   * @param \Symfony\Component\HttpFoundation\RequestStack $request_stack
34
   *   Constructor.
35
   */
36
  public function __construct(RequestStack $request_stack, CaptchaService $captcha_service) {
37
    $this->requestStack = $request_stack;
38
    $this->captchaService = $captcha_service;
39 40 41
  }

  /**
42 43
   * Create Captcha Points.
   *
44
   * @param \Symfony\Component\DependencyInjection\ContainerInterface $container
45
   *   Event to create Captcha points.
46 47 48 49 50
   *
   * @return static
   */
  public static function create(ContainerInterface $container) {
    return new static(
51 52
      $container->get('request_stack'),
      $container->get('captcha.helper')
53 54 55
    );
  }

56 57 58 59 60 61 62 63 64 65 66
  /**
   * {@inheritdoc}
   */
  public function form(array $form, FormStateInterface $form_state) {
    $form = parent::form($form, $form_state);

    module_load_include('inc', 'captcha', 'captcha.admin');

    /* @var CaptchaPointInterface $captchaPoint */
    $captcha_point = $this->entity;

Berdir's avatar
Berdir committed
67
    // Support to set a default form_id through a query argument.
68
    $request = $this->requestStack->getCurrentRequest();
Berdir's avatar
Berdir committed
69 70 71 72 73
    if ($captcha_point->isNew() && !$captcha_point->id() && $request->query->has('form_id')) {
      $captcha_point->set('formId', $request->query->get('form_id'));
      $captcha_point->set('label', $request->query->get('form_id'));
    }

74
    $form['label'] = [
75 76
      '#type' => 'textfield',
      '#title' => $this->t('Form ID'),
77
      '#description' => $this->t('Also works with the base form ID.'),
Berdir's avatar
Berdir committed
78
      '#default_value' => $captcha_point->label(),
79
      '#required' => TRUE,
80
    ];
81

82
    $form['formId'] = [
83 84
      '#type' => 'machine_name',
      '#default_value' => $captcha_point->id(),
85
      '#machine_name' => [
86
        'exists' => 'captcha_point_load',
87
      ],
88
      '#disable' => !$captcha_point->isNew(),
89
      '#required' => TRUE,
90
    ];
91 92

    // Select widget for CAPTCHA type.
93
    $form['captchaType'] = [
94
      '#type' => 'select',
95 96
      '#title' => $this->t('Challenge type'),
      '#description' => $this->t('The CAPTCHA type to use for this form.'),
97 98
      '#default_value' => $captcha_point->getCaptchaType() ?: $this->config('captcha.settings')->get('default_challenge'),
      '#options' => $this->captchaService->getAvailableChallengeTypes(),
99
    ];
100 101 102 103 104 105 106 107

    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function save(array $form, FormStateInterface $form_state) {
108
    /* @var CaptchaPoint $captcha_point */
109 110 111
    $captcha_point = $this->entity;
    $status = $captcha_point->save();

Berdir's avatar
Berdir committed
112
    if ($status == SAVED_NEW) {
113
      $this->messenger()->addMessage($this->t('Captcha Point for %form_id form was created.', [
114
        '%form_id' => $captcha_point->getFormId(),
115
      ]));
116 117
    }
    else {
118
      $this->messenger()->addMessage($this->t('Captcha Point for %form_id form was updated.', [
119
        '%form_id' => $captcha_point->getFormId(),
120
      ]));
121 122 123
    }
    $form_state->setRedirect('captcha_point.list');
  }
124

125
}