CaptchaPointForm.php 3.22 KB
Newer Older
1 2 3 4 5 6
<?php

namespace Drupal\captcha\Form;

use Drupal\Core\Entity\EntityForm;
use Drupal\Core\Form\FormStateInterface;
7 8
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\RequestStack;
9

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

15
  /**
16 17
   * The request stack.
   *
18 19 20 21 22 23 24 25
   * @var \Symfony\Component\HttpFoundation\RequestStack
   */
  protected $requestStack;

  /**
   * CaptchaPointForm constructor.
   *
   * @param \Symfony\Component\HttpFoundation\RequestStack $request_stack
26
   *   Constructor.
27 28 29 30 31 32
   */
  public function __construct(RequestStack $request_stack) {
    $this->requestStack = $request_stack;
  }

  /**
33 34
   * Create Captcha Points.
   *
35
   * @param \Symfony\Component\DependencyInjection\ContainerInterface $container
36
   *   Event to create Captcha points.
37 38 39 40 41 42 43 44 45
   *
   * @return static
   */
  public static function create(ContainerInterface $container) {
    return new static(
      $container->get('request_stack')
    );
  }

46 47 48 49 50 51 52 53 54 55 56
  /**
   * {@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
57
    // Support to set a default form_id through a query argument.
58
    $request = $this->requestStack->getCurrentRequest();
Berdir's avatar
Berdir committed
59 60 61 62 63
    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'));
    }

64
    $form['label'] = [
65 66
      '#type' => 'textfield',
      '#title' => $this->t('Form ID'),
67
      '#description' => $this->t('Also works with the base form ID.'),
Berdir's avatar
Berdir committed
68
      '#default_value' => $captcha_point->label(),
69
      '#required' => TRUE,
70
    ];
71

72
    $form['formId'] = [
73 74
      '#type' => 'machine_name',
      '#default_value' => $captcha_point->id(),
75
      '#machine_name' => [
76
        'exists' => 'captcha_point_load',
77
      ],
78
      '#disable' => !$captcha_point->isNew(),
79
      '#required' => TRUE,
80
    ];
81 82

    // Select widget for CAPTCHA type.
83
    $form['captchaType'] = [
84
      '#type' => 'select',
85 86
      '#title' => $this->t('Challenge type'),
      '#description' => $this->t('The CAPTCHA type to use for this form.'),
87 88
      '#default_value' => ($captcha_point->getCaptchaType() ?: $this->config('captcha.settings')
        ->get('default_challenge')),
89
      '#options' => _captcha_available_challenge_types(),
90
    ];
91 92 93 94 95 96 97 98

    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function save(array $form, FormStateInterface $form_state) {
99
    /* @var CaptchaPoint $captcha_point */
100 101 102
    $captcha_point = $this->entity;
    $status = $captcha_point->save();

Berdir's avatar
Berdir committed
103
    if ($status == SAVED_NEW) {
104
      $this->messenger()->addMessage($this->t('Captcha Point for %form_id form was created.', [
105
        '%form_id' => $captcha_point->getFormId(),
106
      ]));
107 108
    }
    else {
109
      $this->messenger()->addMessage($this->t('Captcha Point for %form_id form was updated.', [
110
        '%form_id' => $captcha_point->getFormId(),
111
      ]));
112 113 114
    }
    $form_state->setRedirect('captcha_point.list');
  }
115

116
}