View source
<?php
namespace Drupal\markdown\Form;
use Drupal\Core\Config\Config;
use Drupal\Core\Form\ConfirmFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
use Drupal\markdown\Plugin\Markdown\ParserInterface;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\Serializer\NameConverter\CamelCaseToSnakeCaseNameConverter;
class ParserOperationForm extends ConfirmFormBase {
protected $operationConfigNames = [
'default' => 'markdown.settings',
'disable' => 'markdown.parser.%s',
'enable' => 'markdown.parser.%s',
];
protected $operation;
protected $operationMethod;
protected $parser;
protected $variables;
public static function createOperationUrl(ParserInterface $parser, $operation) {
$url = Url::fromRoute('markdown.parser.operation', [
'parser' => $parser,
'operation' => $operation,
]);
$token = \Drupal::csrfToken()
->get($url
->getInternalPath());
$options = $url
->getOptions();
$options['query']['token'] = $token;
$url
->setOptions($options);
return $url;
}
public function getFormId() {
return 'markdown_parser_operation';
}
public function getDescription() {
switch ($this->operation) {
case 'default':
return $this
->t('Are you sure you want to set %parser as the default markdown parser?', [
'@operation' => $this->operation,
'%parser' => $this->parser
->getLabel(FALSE),
]);
}
return $this
->t('Are you sure you want to @operation the %parser markdown parser?', [
'@operation' => $this->operation,
'%parser' => $this->parser
->getLabel(FALSE),
]);
}
public function getQuestion() {
return $this
->t('Confirm Operation');
}
public function getConfirmText() {
switch ($this->operation) {
case 'default':
return $this
->t('Set as default');
}
return $this
->t(ucfirst($this->operation));
}
public function getSuccessMessage() {
$variables = [
'@action' => substr($this->operation, -1, 1) === 'e' ? $this
->t($this->operation . 'd') : $this->operation . 'ed',
'@operation' => $this->operation,
'%parser' => $this->parser
->getLabel(FALSE),
'@parser_id' => $this->parser
->getPluginId(),
];
switch ($this->operation) {
case 'default':
return $this
->t('%parser was set as the default markdown parser.', $variables);
}
return $this
->t('The markdown parser %parser was @action.', $variables);
}
public function getCancelUrl() {
return new Url('markdown.overview');
}
public function buildForm(array $form, FormStateInterface $form_state, ParserInterface $parser = NULL, $operation = NULL) {
$this
->initializeOperation($parser, $operation);
$form = parent::buildForm($form, $form_state);
if (!empty(\Drupal::request()
->get('_drupal_ajax'))) {
$form['actions']['cancel']['#attributes']['class'][] = 'dialog-cancel';
}
return $form;
}
protected function initializeOperation(ParserInterface $parser, $operation) {
$this->operation = $operation;
$this->parser = $parser;
$this->variables = [
'@action' => substr($this->operation, -1, 1) === 'e' ? $this
->t($this->operation . 'd') : $this->operation . 'ed',
'@operation' => $this->operation,
'%parser' => $this->parser
->getLabel(FALSE),
'@parser_id' => $this->parser
->getPluginId(),
];
$converter = new CamelCaseToSnakeCaseNameConverter();
$method = $converter
->denormalize("operation_{$operation}");
if (!method_exists($this, $method)) {
throw new NotFoundHttpException();
}
$this->operationMethod = [
$this,
$method,
];
}
public function executeOperation(ParserInterface $parser, $operation) {
$this
->initializeOperation($parser, $operation);
$configName = sprintf(isset($this->operationConfigNames[$this->operation]) ? $this->operationConfigNames[$this->operation] : 'markdown.parser.%s', $this->parser
->getPluginId());
$config = $this
->configFactory()
->getEditable($configName);
$callable = $this->operationMethod;
$response = $callable($config);
$this
->logger('markdown')
->notice('Performed operation (@operation) on parser %parser (@parser_id).', [
'@operation' => $this->operation,
'%parser' => $this->parser
->getLabel(FALSE),
'@parser_id' => $this->parser
->getPluginId(),
]);
if ($message = $this
->getSuccessMessage()) {
drupal_set_message($message);
}
if ($response) {
return $response;
}
return $this
->redirect('markdown.overview');
}
protected function getParserConfig() {
return $this
->configFactory()
->getEditable('markdown.parser.' . $this->parser
->getPluginId());
}
protected function operationDefault(Config $config) {
$config
->set('default_parser', $this->parser
->getPluginId())
->save();
}
protected function operationDisable(Config $config) {
$config
->set('enabled', FALSE)
->save();
}
public function operationEnable(Config $config) {
$config
->set('enabled', TRUE)
->save();
}
public function submitForm(array &$form, FormStateInterface $form_state) {
if ($this->operation) {
$url = static::createOperationUrl($this->parser, $this->operation);
}
else {
$url = $this
->getCancelUrl();
}
$form_state
->setRedirectUrl($url);
}
}