View source
<?php
namespace Drupal\webform;
use Drupal\Core\Ajax\AjaxResponse;
use Drupal\Core\Entity\EntityForm;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
use Drupal\webform\Ajax\WebformRefreshCommand;
use Drupal\webform\Form\WebformEntityAjaxFormTrait;
use Drupal\webform\Plugin\WebformHandlerInterface;
use Drupal\webform\Plugin\WebformHandlerManagerInterface;
use Drupal\webform\Utility\WebformDialogHelper;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
class WebformEntityHandlersForm extends EntityForm {
use WebformEntityAjaxFormTrait;
protected $entity;
protected $handlerManager;
public function __construct(WebformHandlerManagerInterface $handler_manager) {
$this->handlerManager = $handler_manager;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('plugin.manager.webform.handler'));
}
public function form(array $form, FormStateInterface $form_state) {
$user_input = $form_state
->getUserInput();
$form['#id'] = 'webform-handlers-form';
$header = [
[
'data' => $this
->t('Title / Description'),
],
[
'data' => $this
->t('ID'),
'class' => [
RESPONSIVE_PRIORITY_LOW,
],
],
[
'data' => $this
->t('Summary'),
'class' => [
RESPONSIVE_PRIORITY_LOW,
],
],
[
'data' => $this
->t('Status'),
'class' => [
RESPONSIVE_PRIORITY_LOW,
],
],
[
'data' => $this
->t('Weight'),
'class' => [
'webform-tabledrag-hide',
],
],
[
'data' => $this
->t('Operations'),
],
];
$handlers = $this->entity
->getHandlers();
$rows = [];
foreach ($handlers as $handler_id => $handler) {
$offcanvas_dialog_attributes = WebformDialogHelper::getOffCanvasDialogAttributes($handler
->getOffCanvasWidth());
$row['#attributes']['class'][] = 'draggable';
$row['#attributes']['data-webform-key'] = $handler_id;
$row['#weight'] = isset($user_input['handlers']) && isset($user_input['handlers'][$handler_id]) ? $user_input['handlers'][$handler_id]['weight'] : NULL;
$row['handler'] = [
'#tree' => FALSE,
'data' => [
'label' => [
'#type' => 'link',
'#title' => $handler
->label(),
'#url' => Url::fromRoute('entity.webform.handler.edit_form', [
'webform' => $this->entity
->id(),
'webform_handler' => $handler_id,
]),
'#attributes' => $offcanvas_dialog_attributes,
],
'description' => [
'#prefix' => '<br/>',
'#markup' => $handler
->getNotes() ?: $handler
->description(),
],
],
];
$row['id'] = [
'data' => [
'#markup' => $handler
->getHandlerId(),
],
];
$row['summary'] = $handler
->getSummary();
if ($handler
->isDisabled()) {
$status = $this
->t('Disabled');
}
else {
$status = $handler
->supportsConditions() && $handler
->getConditions() ? $this
->t('Conditional') : $this
->t('Enabled');
}
$row['status'] = [
'data' => [
'#markup' => $status,
],
];
$row['weight'] = [
'#type' => 'weight',
'#title' => $this
->t('Weight for @title', [
'@title' => $handler
->label(),
]),
'#title_display' => 'invisible',
'#delta' => 50,
'#default_value' => $handler
->getWeight(),
'#attributes' => [
'class' => [
'webform-handler-order-weight',
],
],
'#wrapper_attributes' => [
'class' => [
'webform-tabledrag-hide',
],
],
];
$operations = [];
$operations['edit'] = [
'title' => $this
->t('Edit'),
'url' => Url::fromRoute('entity.webform.handler.edit_form', [
'webform' => $this->entity
->id(),
'webform_handler' => $handler_id,
]),
'attributes' => $offcanvas_dialog_attributes,
];
if ($handler
->cardinality() === WebformHandlerInterface::CARDINALITY_UNLIMITED) {
$operations['duplicate'] = [
'title' => $this
->t('Duplicate'),
'url' => Url::fromRoute('entity.webform.handler.duplicate_form', [
'webform' => $this->entity
->id(),
'webform_handler' => $handler_id,
]),
'attributes' => $offcanvas_dialog_attributes,
];
}
if ($this->entity
->access('test')) {
$operations['test'] = [
'title' => $this
->t('Test'),
'url' => Url::fromRoute('entity.webform.test_form', [
'webform' => $this->entity
->id(),
], [
'query' => [
'_webform_handler' => $handler_id,
],
]),
];
}
$operations['status'] = [
'title' => $handler
->isEnabled() ? $this
->t('Disable') : $this
->t('Enable'),
'url' => Url::fromRoute('entity.webform.handler.' . ($handler
->isEnabled() ? 'disable' : 'enable'), [
'webform' => $this->entity
->id(),
'webform_handler' => $handler_id,
]),
'attributes' => WebformDialogHelper::getModalDialogAttributes(WebformDialogHelper::DIALOG_NARROW, [
'use-ajax',
]),
];
$operations['delete'] = [
'title' => $this
->t('Delete'),
'url' => Url::fromRoute('entity.webform.handler.delete_form', [
'webform' => $this->entity
->id(),
'webform_handler' => $handler_id,
]),
'attributes' => WebformDialogHelper::getModalDialogAttributes(WebformDialogHelper::DIALOG_NARROW),
];
$row['operations'] = [
'#type' => 'operations',
'#links' => $operations,
'#prefix' => '<div class="webform-dropbutton">',
'#suffix' => '</div>',
];
$rows[$handler_id] = $row;
}
$form['handlers'] = [
'#type' => 'table',
'#header' => $header,
'#tabledrag' => [
[
'action' => 'order',
'relationship' => 'sibling',
'group' => 'webform-handler-order-weight',
],
],
'#attributes' => [
'id' => 'webform-handlers',
'class' => [
'webform-handlers-table',
],
],
'#empty' => $this
->t('There are currently no handlers setup for this webform.'),
] + $rows;
WebformDialogHelper::attachLibraries($form);
$form['#attached']['library'][] = 'webform/webform.admin.tabledrag';
return parent::form($form, $form_state);
}
protected function actionsElement(array $form, FormStateInterface $form_state) {
$form = parent::actionsElement($form, $form_state);
$form['submit']['#value'] = $this
->t('Save handlers');
unset($form['delete']);
return $form;
}
public function submitForm(array &$form, FormStateInterface $form_state) {
if (!$form_state
->isValueEmpty('handlers')) {
$this
->updateHandlerWeights($form_state
->getValue('handlers'));
}
parent::submitForm($form, $form_state);
}
public function save(array $form, FormStateInterface $form_state) {
$webform = $this
->getEntity();
$webform
->save();
$context = [
'@label' => $webform
->label(),
'link' => $webform
->toLink($this
->t('Edit'), 'handlers')
->toString(),
];
$this
->logger('webform')
->notice('Webform @label handler saved.', $context);
$this
->messenger()
->addStatus($this
->t('Webform %label handler saved.', [
'%label' => $webform
->label(),
]));
}
protected function updateHandlerWeights(array $handlers) {
foreach ($handlers as $handler_id => $handler_data) {
if ($this->entity
->getHandlers()
->has($handler_id)) {
$this->entity
->getHandler($handler_id)
->setWeight($handler_data['weight']);
}
}
}
public static function ajaxOperation(WebformInterface $webform, $webform_handler, $operation, Request $request) {
$handler = $webform
->getHandler($webform_handler);
$handler
->{$operation}();
$webform
->save();
$t_args = [
'@label' => $handler
->label(),
'@op' => $operation === 'enable' ? t('enabled') : t('disabled'),
];
\Drupal::messenger()
->addStatus(t('This @label handler was @op.', $t_args));
$url = $webform
->toUrl('handlers', [
'query' => [
'update' => $webform_handler,
],
])
->toString();
if ($request->request
->get('js')) {
$response = new AjaxResponse();
$response
->addCommand(new WebformRefreshCommand($url));
return $response;
}
return new RedirectResponse($url);
}
}