Commit b5becc1a authored by LKS90's avatar LKS90

Implemented the disable/enable captcha point in a little different way

parent c475bd48
......@@ -311,18 +311,20 @@ function captcha_form_alter(array &$form, FormStateInterface $form_state, $form_
if ($config->get('add_captcha_description')) {
$captcha_element['#description'] = _captcha_get_description();
}
if ($captcha_point->status()) {
// Get placement in form and insert in form.
$captcha_placement = _captcha_get_captcha_placement($form_id, $form);
_captcha_insert_captcha_element($form, $captcha_placement, $captcha_element);
}
// Get placement in form and insert in form.
$captcha_placement = _captcha_get_captcha_placement($form_id, $form);
_captcha_insert_captcha_element($form, $captcha_placement, $captcha_element);
}
}
elseif ($config->get('administration_mode') && $account->hasPermission('administer CAPTCHA settings')
&& (!\Drupal::service('router.admin_context')->isAdminRoute() || $config->get('allow_on_admin_pages'))) {
// Add CAPTCHA administration tools.
/* @var CaptchaPoint $captcha_point */
$captcha_point = \Drupal::entityManager()->getStorage('captcha_point')->load($form_id);
/* var $captcha_point CaptchaPoint */
$captcha_point = captcha_get_form_id_setting($form_id);
// For administrators: show CAPTCHA info and offer link to configure it.
$captcha_element = [
'#type' => 'details',
......@@ -334,17 +336,19 @@ function captcha_form_alter(array &$form, FormStateInterface $form_state, $form_
];
if ($captcha_point !== NULL && $captcha_point->getCaptchaType()) {
$captcha_element['#title'] = t('CAPTCHA: challenge "@type" enabled', ['@type' => $captcha_point->getCaptchaType()]);
$markup = $captcha_point->status() ? '%type (<a href="@change">change</a>, <a href="@disable">disable</a>)' : '%type (<a href="@change">change</a>, <a href="@enable">enable</a>)';
$captcha_element['#title'] = $captcha_point->status() ? t('CAPTCHA: challenge "@type" enabled', ['@type' => $captcha_point->getCaptchaType()]) : t('CAPTCHA: challenge "@type" disabled', ['@type' => $captcha_point->getCaptchaType()]);
$captcha_element['#description'] = t('Untrusted users will see a CAPTCHA here (<a href="@settings">general CAPTCHA settings</a>).',
['@settings' => Url::fromRoute('captcha_settings')->toString()]
);
$captcha_element['challenge'] = [
'#type' => 'item',
'#title' => t('Enabled challenge'),
'#markup' => t('%type (<a href="@change">change</a>, <a href="@disable">disable</a>)', [
'#markup' => t($markup, [
'%type' => $captcha_point->getCaptchaType(),
'@change' => $captcha_point->url('edit-form', ['query' => drupal_get_destination()]),
//'@disable' => url("admin/config/people/captcha/captcha-points/$form_id/disable", array('query' => drupal_get_destination())),
'@change' => $captcha_point->url('edit-form', ['query' => Drupal::destination()->getAsArray()]),
'@disable' => $captcha_point->url('disable-form', ['query' => Drupal::destination()->getAsArray()]),
'@enable' => $captcha_point->url('enable-form', ['query' => Drupal::destination()->getAsArray()]),
]),
];
// // Add an example challenge with solution.
......@@ -373,7 +377,7 @@ function captcha_form_alter(array &$form, FormStateInterface $form_state, $form_
'#markup' => \Drupal::l(
t('Place a CAPTCHA here for untrusted users.'),
Url::fromRoute('captcha_point.add', [], [
'query' => drupal_get_destination() + ['form_id' => $form_id],
'query' => Drupal::destination()->getAsArray() + ['form_id' => $form_id],
])
),
];
......
......@@ -41,6 +41,26 @@ entity.captcha_point.edit_form:
requirements:
_permission: 'administer CAPTCHA settings'
entity.captcha_point.disable_form:
path: '/admin/config/people/captcha/captcha-points/{captcha_point}/disable'
defaults:
_entity_form: 'captcha_point.disable'
_title: 'Disable CAPTCHA point'
options:
_admin_route: TRUE
requirements:
_permission: 'administer CAPTCHA settings'
entity.captcha_point.enable_form:
path: '/admin/config/people/captcha/captcha-points/{captcha_point}/enable'
defaults:
_entity_form: 'captcha_point.enable'
_title: 'Enable CAPTCHA point'
options:
_admin_route: TRUE
requirements:
_permission: 'administer CAPTCHA settings'
entity.captcha_point.delete_form:
path: '/admin/config/people/captcha/captcha-points/{captcha_point}/delete'
defaults:
......
......@@ -59,4 +59,9 @@ interface CaptchaPointInterface extends ConfigEntityInterface {
* Captcha type.
*/
public function setCaptchaType($captcha_type);
/**
* {@inheritdoc}
*/
public function disable();
}
......@@ -8,6 +8,7 @@
namespace Drupal\captcha\Entity;
use Drupal\Core\Config\Entity\ConfigEntityBase;
use Drupal\Core\Cache\Cache;
use Drupal\captcha\CaptchaPointInterface;
/**
......@@ -21,6 +22,8 @@ use Drupal\captcha\CaptchaPointInterface;
* "form" = {
* "add" = "Drupal\captcha\Form\CaptchaPointForm",
* "edit" = "Drupal\captcha\Form\CaptchaPointForm",
* "disable" = "Drupal\captcha\Form\CaptchaPointDisableForm",
* "enable" = "Drupal\captcha\Form\CaptchaPointEnableForm",
* "delete" = "Drupal\captcha\Form\CaptchaPointDeleteForm"
* }
* },
......@@ -32,6 +35,8 @@ use Drupal\captcha\CaptchaPointInterface;
* },
* links = {
* "edit-form" = "/admin/config/people/captcha/captcha-points/{captcha_point}",
* "disable-form" = "/admin/config/people/captcha/captcha-points/{captcha_point}/disable",
* "enable-form" = "/admin/config/people/captcha/captcha-points/{captcha_point}/enable",
* "delete-form" = "/admin/config/people/captcha/captcha-points/{captcha_point}/delete",
* }
* )
......@@ -91,6 +96,14 @@ class CaptchaPoint extends ConfigEntityBase implements CaptchaPointInterface {
}
}
/**
* {@inheritdoc}
*/
public function disable() {
Cache::invalidateTags($this->getCacheTags());
return $this->setStatus(FALSE);
}
/**
* {@inheritdoc}
*/
......
<?php
/**
* @file
* Contains \Drupal\captcha\Form\CaptchaPointDisableForm.
*/
namespace Drupal\captcha\Form;
use Drupal\Core\Entity\EntityConfirmFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
/**
* Builds the form to delete a Captcha Point.
*/
class CaptchaPointDisableForm extends EntityConfirmFormBase {
/**
* {@inheritdoc}
*/
public function getQuestion() {
return $this->t('Are you sure you want to disable the Captcha?');
}
/**
* {@inheritdoc}
*/
public function getDescription() {
return $this->t('This will disable the captcha.');
}
/**
* {@inheritdoc}
*/
public function getCancelUrl() {
return new Url('captcha_point.list');
}
/**
* {@inheritdoc}
*/
public function getConfirmText() {
return $this->t('Disable');
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
$this->entity->disable();
$this->entity->save();
drupal_set_message($this->t('Captcha point %label has been disabled.', array('%label' => $this->entity->label())));
}
}
<?php
/**
* @file
* Contains \Drupal\captcha\Form\CaptchaPointEnableForm.
*/
namespace Drupal\captcha\Form;
use Drupal\Core\Entity\EntityConfirmFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
/**
* Builds the form to delete a Captcha Point.
*/
class CaptchaPointEnableForm extends EntityConfirmFormBase {
/**
* {@inheritdoc}
*/
public function getQuestion() {
return $this->t('Are you sure you want to enable the Captcha?');
}
/**
* {@inheritdoc}
*/
public function getDescription() {
return $this->t('This will enable the captcha.');
}
/**
* {@inheritdoc}
*/
public function getCancelUrl() {
return new Url('captcha_point.list');
}
/**
* {@inheritdoc}
*/
public function getConfirmText() {
return $this->t('Enable');
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
$this->entity->enable();
$this->entity->save();
drupal_set_message($this->t('Captcha point %label has been enabled.', array('%label' => $this->entity->label())));
}
}
......@@ -7,6 +7,7 @@
namespace Drupal\captcha\Form;
use Drupal\captcha\Entity\CaptchaPoint;
use Drupal\Core\Entity\EntityForm;
use Drupal\captcha\CaptchaPointInterface;
use Drupal\Core\Form\FormStateInterface;
......@@ -62,17 +63,18 @@ class CaptchaPointForm extends EntityForm {
* {@inheritdoc}
*/
public function save(array $form, FormStateInterface $form_state) {
/* @var CaptchaPoint $captcha_point */
$captcha_point = $this->entity;
$status = $captcha_point->save();
if ($status == SAVED_NEW) {
drupal_set_message($this->t('Captcha Point for %label form was created.', array(
'%label' => $captcha_point->label(),
'%label' => $captcha_point->getFormId(),
)));
}
else {
drupal_set_message($this->t('Captcha Point for %label form was updated.', array(
'%label' => $captcha_point->label(),
'%label' => $captcha_point->getFormId(),
)));
}
$form_state->setRedirect('captcha_point.list');
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment