View source
<?php
namespace Drupal\webform_templates\Controller;
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Form\FormBuilderInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Url;
use Drupal\webform\Utility\WebformDialogHelper;
use Drupal\webform\WebformInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
class WebformTemplatesController extends ControllerBase implements ContainerInjectionInterface {
protected $currentUser;
protected $formBuilder;
protected $webformStorage;
public function __construct(AccountInterface $current_user, FormBuilderInterface $form_builder, EntityTypeManagerInterface $entity_type_manager) {
$this->currentUser = $current_user;
$this->formBuilder = $form_builder;
$this->webformStorage = $entity_type_manager
->getStorage('webform');
}
public static function create(ContainerInterface $container) {
return new static($container
->get('current_user'), $container
->get('form_builder'), $container
->get('entity_type.manager'));
}
public function index(Request $request, $manage = FALSE) {
$keys = $request
->get('search');
$category = $request
->get('category');
if ($keys && preg_match('#\\(([^)]+)\\)$#', $keys, $match)) {
if ($webform = $this->webformStorage
->load($match[1])) {
return new RedirectResponse($webform
->toUrl()
->setAbsolute(TRUE)
->toString());
}
}
$header = [];
$header['title'] = $this
->t('Title');
$header['description'] = [
'data' => $this
->t('Description'),
'class' => [
RESPONSIVE_PRIORITY_LOW,
],
];
$header['category'] = [
'data' => $this
->t('Category'),
'class' => [
RESPONSIVE_PRIORITY_LOW,
],
];
if ($manage) {
$header['owner'] = [
'data' => $this
->t('Author'),
'class' => [
RESPONSIVE_PRIORITY_LOW,
],
];
}
$header['operations'] = [
'data' => $this
->t('Operations'),
];
$webforms = $this
->getTemplates($keys, $category);
$rows = [];
foreach ($webforms as $webform) {
$route_parameters = [
'webform' => $webform
->id(),
];
$row['title'] = $webform
->toLink();
$row['description']['data']['#markup'] = $webform
->get('description');
$row['category']['data']['#markup'] = $webform
->get('category');
if ($manage) {
$row['owner'] = ($owner = $webform
->getOwner()) ? $owner
->toLink() : '';
$operations = [];
if ($webform
->access('update')) {
$operations['edit'] = [
'title' => $this
->t('Build'),
'url' => $this
->ensureDestination($webform
->toUrl('edit-form')),
];
}
if ($webform
->access('submission_page')) {
$operations['view'] = [
'title' => $this
->t('View'),
'url' => $webform
->toUrl('canonical'),
];
}
if ($webform
->access('update')) {
$operations['settings'] = [
'title' => $this
->t('Settings'),
'url' => $webform
->toUrl('settings'),
];
}
if ($webform
->access('duplicate')) {
$operations['duplicate'] = [
'title' => $this
->t('Duplicate'),
'url' => $webform
->toUrl('duplicate-form', [
'query' => [
'template' => 1,
],
]),
'attributes' => WebformDialogHelper::getModalDialogAttributes(WebformDialogHelper::DIALOG_NARROW),
];
}
if ($webform
->access('delete') && $webform
->hasLinkTemplate('delete-form')) {
$operations['delete'] = [
'title' => $this
->t('Delete'),
'url' => $this
->ensureDestination($webform
->toUrl('delete-form')),
'attributes' => WebformDialogHelper::getModalDialogAttributes(WebformDialogHelper::DIALOG_NARROW),
];
}
$row['operations']['data'] = [
'#type' => 'operations',
'#links' => $operations,
'#prefix' => '<div class="webform-dropbutton">',
'#suffix' => '</div>',
];
}
else {
$row['operations']['data']['select'] = [
'#type' => 'link',
'#title' => $this
->t('Select'),
'#url' => Url::fromRoute('entity.webform.duplicate_form', $route_parameters),
'#attributes' => WebformDialogHelper::getModalDialogAttributes(WebformDialogHelper::DIALOG_NARROW, [
'button',
'button--primary',
]),
];
$row['operations']['data']['preview'] = [
'#type' => 'link',
'#title' => $this
->t('Preview'),
'#url' => Url::fromRoute('entity.webform.preview', $route_parameters),
'#attributes' => WebformDialogHelper::getModalDialogAttributes(WebformDialogHelper::DIALOG_NORMAL, [
'button',
]),
];
}
$rows[] = $row;
}
$build = [];
$build['filter_form'] = $this->formBuilder
->getForm('\\Drupal\\webform_templates\\Form\\WebformTemplatesFilterForm', $keys);
if ($total = count($rows)) {
$build['info'] = [
'#markup' => $this
->formatPlural($total, '@total template', '@total templates', [
'@total' => $total,
]),
'#prefix' => '<div>',
'#suffix' => '</div>',
];
}
$build['table'] = [
'#type' => 'table',
'#header' => $header,
'#rows' => $rows,
'#sticky' => TRUE,
'#empty' => $this
->t('There are no templates available.'),
'#cache' => [
'contexts' => $this->webformStorage
->getEntityType()
->getListCacheContexts(),
'tags' => $this->webformStorage
->getEntityType()
->getListCacheTags(),
],
];
WebformDialogHelper::attachLibraries($build);
return $build;
}
public function previewForm(Request $request, WebformInterface $webform) {
if (!$webform
->isTemplate()) {
return new NotFoundHttpException();
}
return $webform
->getSubmissionForm([], 'preview');
}
protected function getTemplates($keys = '', $category = '') {
$query = $this->webformStorage
->getQuery();
$query
->condition('template', TRUE);
$query
->condition('archive', FALSE);
if ($keys) {
$or = $query
->orConditionGroup()
->condition('title', $keys, 'CONTAINS')
->condition('description', $keys, 'CONTAINS')
->condition('category', $keys, 'CONTAINS')
->condition('elements', $keys, 'CONTAINS');
$query
->condition($or);
}
if ($category) {
$query
->condition('category', $category);
}
$query
->sort('title');
$entity_ids = $query
->execute();
if (empty($entity_ids)) {
return [];
}
$entities = $this->webformStorage
->loadMultiple($entity_ids);
if (!$this
->isAdmin()) {
foreach ($entities as $entity_id => $entity) {
if (!$entity
->access('view')) {
unset($entities[$entity_id]);
}
}
}
return $entities;
}
public function previewTitle(WebformInterface $webform = NULL) {
return $this
->t('Previewing @title template', [
'@title' => $webform
->label(),
]);
}
protected function isAdmin() {
return $this->currentUser
->hasPermission('administer webform') || $this->currentUser
->hasPermission('edit any webform');
}
protected function ensureDestination(Url $url) {
return $url
->mergeOptions([
'query' => $this
->getRedirectDestination()
->getAsArray(),
]);
}
}