page_tester-1.0.x-dev/src/Controller/CommentsController.php
src/Controller/CommentsController.php
<?php
namespace Drupal\page_tester\Controller;
use Symfony\Component\HttpFoundation\Request;
use Drupal\Core\Ajax\OpenDialogCommand;
use Drupal\Core\Ajax\OpenOffCanvasDialogCommand;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Ajax\AjaxResponse;
use Drupal\Core\Ajax\OpenModalDialogCommand;
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Form\FormBuilder;
use Drupal\Core\Link;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Drupal\Core\Url;
use Drupal\page_tester\PageTesterService;
/**
* Returns responses for Comments routes.
*/
class CommentsController extends ControllerBase {
/**
* The form builder.
*
* @var \Drupal\Core\Form\FormBuilder
*/
protected $formBuilder;
/**
* The entity type manager.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;
/**
* The page tester service.
*
* @var \Drupal\page_tester\PageTesterService
*/
protected $pageTesterService;
/**
* The ModalFormExampleController constructor.
*
* @param \Drupal\Core\Form\FormBuilder $formBuilder
* The form builder.
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* The entity type manager.
* @param \Drupal\page_tester\PageTesterService $pageTesterService
* The page tester service.
*/
public function __construct(
FormBuilder $formBuilder,
EntityTypeManagerInterface $entity_type_manager,
PageTesterService $pageTesterService
) {
$this->formBuilder = $formBuilder;
$this->entityTypeManager = $entity_type_manager;
$this->pageTesterService = $pageTesterService;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('form_builder'),
$container->get('entity_type.manager'),
$container->get('page_tester.pagetester_service'),
);
}
/**
* Opens modal with comments listed in a table.
*
* @param string $test_id
* The test id.
*
* @return AjaxResponse
*/
public function viewComments($test_id) {
// Get the title of the node.
$title = $this->t('View Comments');
// Create the AjaxResponse object.
$response = new AjaxResponse();
// Attach the library needed to use the OpenDialogCommand response.
$attachments['library'][] = 'core/drupal.dialog.ajax';
$attachments['library'][] = 'core/drupal.dialog.off_canvas';
$response->setAttachments($attachments);
$header = [
[
'data' => $this->t('Test Number'),
'class' => [RESPONSIVE_PRIORITY_LOW],
],
$this->t('Comment'),
[
'data' => $this->t('Operations'),
'class' => [RESPONSIVE_PRIORITY_LOW],
],
];
$results = $this->pageTesterService->getComments($test_id);
$rows = [];
$referer_url = \Drupal::request()->headers->get('referer');
foreach ($results as $comment) {
$testLink = Link::fromTextAndUrl($comment->test_number, new Url(
'page_tester.view_comment',
[
'test_id' => $test_id,
'test_number' => $comment->test_number,
],
[
'query' => ['destination' => $referer_url]
]
))->toString();
$deleteLink = Link::fromTextAndUrl('Delete', new Url('page_tester.comment_delete', [
'id' => $comment->ptid
]))->toString();
$rows[] = [
'data' => [
['data' => ['#markup' => $testLink]],
$this->t($comment->message),
['data' => ['#markup' => $deleteLink]],
],
];
}
$build['comment_table'] = [
'#type' => 'table',
'#header' => $header,
'#rows' => $rows,
'#weight' => 100,
'#attributes' => [],
'#empty' => $this->t('No log messages available.'),
'#attached' => [
'library' => [],
],
];
// Add the open dialog command to the ajax response.
$response->addCommand(new OpenModalDialogCommand($title, $build, ['width' => '70%']));
return $response;
}
/**
* Sets the test when going to a comment.
*
* @param Symfony\Component\HttpFoundation\Request $request
* The request object.
* @param string $test_id
* The test id.
* @param int $test_number
* The test number.
*
* @return Symfony\Component\HttpFoundation\RedirectResponse
*
*/
public function viewComment(Request $request, $test_id, $test_number) {
$sessionName = 'page_tests_array_' . $test_id;
$request = \Drupal::request();
$page_tests_array = $request->getSession()->get($sessionName, []);
$page_tests_array['test'] = $test_number;
$request->getSession()->set($sessionName, $page_tests_array);
$testPlugin = $this->pageTesterService->getTestPlugin($test_id);
$testPlugin->setTest($test_id, $page_tests_array['test']);
return new RedirectResponse(\Drupal::request()->headers->get('referer'));
}
/**
* Opens modal with comment form.
*
* @param string $test_id
* The test id.
* @param string $test_number
* The test number.
*
* @return AjaxResponse
*/
public function createComment(int $test_id, int $test_number) {
// Create the AjaxResponse object.
$response = new AjaxResponse();
// Attach the library needed to use the OpenDialogCommand response.
$attachments['library'][] = 'core/drupal.dialog.ajax';
$response->setAttachments($attachments);
// Add the open dialog command to the ajax response.
$title = $this->t('Create Comment');
$form = $this->formBuilder->getForm('Drupal\page_tester\Form\CreateCommentForm', $test_id, $test_number);
$response->addCommand(new OpenModalDialogCommand($title, $form, ['width' => '800']));
return $response;
}
/**
* Deletes comment.
*
* @param string $id
* The id.
*/
public function deleteComment($id) {
$this->pageTesterService->deleteComment($id);
return new RedirectResponse(\Drupal::request()->headers->get('referer'));
}
}
