View source
<?php
namespace Drupal\fontyourface\Controller;
use Drupal\Core\Url;
use Drupal\Core\Ajax\AjaxResponse;
use Drupal\Core\Ajax\ReplaceCommand;
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Link;
use Drupal\fontyourface\Entity\Font;
class FontYourFaceController extends ControllerBase {
public function activateFont(Font $font, $js) {
try {
$font
->activate();
if ($js == 'ajax') {
$url = Url::fromRoute('entity.font.deactivate', [
'js' => 'nojs',
'font' => $font
->id(),
], [
'query' => \Drupal::destination()
->getAsArray(),
]);
$url
->setOptions([
'attributes' => [
'id' => 'font-status-' . $font
->id(),
'class' => [
'font-status',
'enabled',
'use-ajax',
],
],
]);
$text = $this
->t('Enable');
$link = Link::fromTextAndUrl($text, $url)
->toString();
$response = new AjaxResponse();
return $response
->addCommand(new ReplaceCommand('#font-status-' . $font
->id(), $link));
}
else {
\Drupal::messenger()
->addMessage($this
->t('Font @font successfully enabled', [
'@font' => $font->name->value,
]));
return $this
->redirect('entity.font.collection');
}
} catch (Exception $e) {
$error = $e
->getMessage();
if ($js == 'ajax') {
return new AjaxResponse([
'response' => TRUE,
'message' => $error,
], 503);
}
else {
\Drupal::messenger()
->addMessage($error, 'error');
return $this
->redirect('entity.font.collection');
}
}
}
public function deactivateFont(Font $font, $js) {
try {
$font
->deactivate();
if ($js == 'ajax') {
$url = Url::fromRoute('entity.font.activate', [
'js' => 'nojs',
'font' => $font
->id(),
], [
'query' => \Drupal::destination()
->getAsArray(),
]);
$url
->setOptions([
'attributes' => [
'id' => 'font-status-' . $font
->id(),
'class' => [
'font-status',
'disabled',
'use-ajax',
],
],
]);
$text = $this
->t('Enable');
$link = Link::fromTextAndUrl($text, $url)
->toString();
$response = new AjaxResponse();
return $response
->addCommand(new ReplaceCommand('#font-status-' . $font
->id(), $link));
}
else {
\Drupal::messenger()
->addMessage($this
->t('Font @font successfully disabled', [
'@font' => $font->name->value,
]));
return $this
->redirect('entity.font.collection');
}
} catch (Exception $e) {
$error = $e
->getMessage();
if ($js == 'ajax') {
return new AjaxResponse([
'response' => TRUE,
'message' => $error,
], 503);
}
else {
\Drupal::messenger()
->addMessage($error, 'error');
return $this
->redirect('entity.font.collection');
}
}
}
}