View source
<?php
namespace Drupal\node_revision_delete\Form;
use Drupal\Component\Utility\Xss;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
use Drupal\node_revision_delete\Utility\Donation;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\node_revision_delete\NodeRevisionDeleteInterface;
use Drupal\Core\Link;
use Drupal\Core\Datetime\DateFormatterInterface;
use Drupal\Core\Render\RendererInterface;
use Drupal\node\NodeTypeInterface;
class CandidateRevisionsContentTypeForm extends FormBase {
protected $entityTypeManager;
protected $nodeRevisionDelete;
protected $dateFormatter;
protected $renderer;
public function __construct(EntityTypeManagerInterface $entity_type_manager, NodeRevisionDeleteInterface $node_revision_delete, DateFormatterInterface $date_formatter, RendererInterface $renderer) {
$this->entityTypeManager = $entity_type_manager;
$this->nodeRevisionDelete = $node_revision_delete;
$this->dateFormatter = $date_formatter;
$this->renderer = $renderer;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('entity_type.manager'), $container
->get('node_revision_delete'), $container
->get('date.formatter'), $container
->get('renderer'));
}
public function getFormId() {
return 'node_revision_delete_candidates_revisions_content_type';
}
public function buildForm(array $form, FormStateInterface $form_state, NodeTypeInterface $node_type = NULL) {
$header = [
$this
->t('Revision ID'),
[
'data' => $this
->t('Revision'),
'class' => [
RESPONSIVE_PRIORITY_MEDIUM,
],
],
$this
->t('nid'),
[
'data' => $this
->t('Title'),
'class' => [
RESPONSIVE_PRIORITY_MEDIUM,
],
],
[
'data' => $this
->t('Operations'),
'class' => [
RESPONSIVE_PRIORITY_MEDIUM,
],
],
];
$node_type_machine_name = $node_type
->id();
$revisions = $this->nodeRevisionDelete
->getCandidatesRevisions($node_type_machine_name);
$rows = [];
foreach ($revisions as $revision) {
$revision = $this->entityTypeManager
->getStorage('node')
->loadRevision($revision);
$username = [
'#theme' => 'username',
'#account' => $revision
->getRevisionUser(),
];
$date = $this->dateFormatter
->format($revision->revision_timestamp->value, 'short');
$revision_url = new Url('entity.node.revision', [
'node' => $revision
->id(),
'node_revision' => $revision
->getRevisionId(),
]);
$revision_link = Link::fromTextAndUrl($date, $revision_url)
->toRenderable();
$revision_info = [
'#type' => 'inline_template',
'#template' => '{% trans %}{{ date }} by {{ username }}{% endtrans %}{% if message %}<p class="revision-log">{{ message }}</p>{% endif %}',
'#context' => [
'date' => $this->renderer
->renderPlain($revision_link),
'username' => $this->renderer
->renderPlain($username),
'message' => [
'#markup' => $revision->revision_log->value,
'#allowed_tags' => Xss::getHtmlTagList(),
],
],
];
$nid = $revision
->id();
$vid = $revision
->getRevisionId();
$route_parameters_destination = [
'node_type' => $node_type_machine_name,
];
$destination = Url::fromRoute('node_revision_delete.candidate_revisions_content_type', $route_parameters_destination)
->toString();
$destination_options = [
'query' => [
'destination' => $destination,
],
];
$route_parameters_dropbutton = [
'node' => $nid,
'node_revision' => $vid,
];
$dropbutton = [
'#type' => 'dropbutton',
'#links' => [
'delete' => [
'title' => $this
->t('Delete'),
'url' => Url::fromRoute('node.revision_delete_confirm', $route_parameters_dropbutton, $destination_options),
],
],
];
$rows[$vid] = [
$vid,
[
'data' => $revision_info,
],
$nid,
Link::fromTextAndUrl($revision
->label(), $revision
->toUrl('canonical')),
[
'data' => $dropbutton,
],
];
}
$node_type_url = $node_type
->toUrl()
->toString();
$caption = $this
->t('Candidates revisions for content type <a href=":url">%title</a>', [
':url' => $node_type_url,
'%title' => $node_type
->label(),
]);
$form['candidate_revisions'] = [
'#type' => 'tableselect',
'#caption' => $caption,
'#header' => $header,
'#options' => $rows,
'#empty' => $this
->t('There are not candidates revisions to be deleted.'),
'#sticky' => TRUE,
];
$form['actions']['#type'] = 'actions';
$form['actions']['submit'] = [
'#type' => 'submit',
'#value' => $this
->t('Delete revisions'),
'#button_type' => 'primary',
];
$form['#prefix'] = Donation::getDonationText();
return $form;
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$candidate_revisions = array_filter($form_state
->getValue('candidate_revisions'));
if (count($candidate_revisions)) {
batch_set($this->nodeRevisionDelete
->getRevisionDeletionBatch($candidate_revisions, FALSE));
}
}
}