View source
<?php
namespace Drupal\diff;
use Drupal\Component\Plugin\PluginBase;
use Drupal\Component\Utility\Xss;
use Drupal\Core\Datetime\DateFormatter;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Entity\RevisionLogInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Mail\MailFormatHelper;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Link;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Core\Url;
use Drupal\diff\Controller\PluginRevisionController;
use Drupal\node\NodeInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
abstract class DiffLayoutBase extends PluginBase implements DiffLayoutInterface, ContainerFactoryPluginInterface {
use StringTranslationTrait;
protected $configFactory;
protected $entityTypeManager;
protected $entityParser;
protected $date;
public function __construct(array $configuration, $plugin_id, $plugin_definition, ConfigFactoryInterface $config, EntityTypeManagerInterface $entity_type_manager, DiffEntityParser $entity_parser, DateFormatter $date) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->configFactory = $config;
$this->entityTypeManager = $entity_type_manager;
$this->entityParser = $entity_parser;
$this->date = $date;
$this->configuration += $this
->defaultConfiguration();
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('config.factory'), $container
->get('entity_type.manager'), $container
->get('diff.entity_parser'), $container
->get('date.formatter'));
}
protected function buildRevisionLink(ContentEntityInterface $revision) {
if ($revision instanceof RevisionLogInterface) {
$revision_date = $this->date
->format($revision
->getRevisionCreationTime(), 'short');
$revision_link = Link::fromTextAndUrl($revision_date, $revision
->toUrl('revision'))
->toString();
}
else {
$revision_link = Link::fromTextAndUrl($revision
->label(), $revision
->toUrl('revision'))
->toString();
}
return $revision_link;
}
public function buildRevisionsData(ContentEntityInterface $left_revision, ContentEntityInterface $right_revision) {
$right_revision = $this
->buildRevisionData($right_revision);
$right_revision['#prefix'] = '<div class="diff-revision__items-group">';
$right_revision['#suffix'] = '</div>';
$left_revision = $this
->buildRevisionData($left_revision);
$left_revision['#prefix'] = '<div class="diff-revision__items-group">';
$left_revision['#suffix'] = '</div>';
return [
'header' => [
'diff_revisions' => [
'#type' => 'item',
'#title' => $this
->t('Comparing'),
'#wrapper_attributes' => [
'class' => 'diff-revision',
],
'items' => [
'#prefix' => '<div class="diff-revision__items">',
'#suffix' => '</div>',
'right_revision' => $right_revision,
'left_revision' => $left_revision,
],
],
],
];
}
protected function buildRevisionData(ContentEntityInterface $revision) {
if ($revision instanceof RevisionLogInterface) {
$revision_log = Xss::filter($revision
->getRevisionLogMessage());
$user_id = $revision
->getRevisionUserId();
$revision_link['date'] = [
'#type' => 'link',
'#title' => $this->date
->format($revision
->getRevisionCreationTime(), 'short'),
'#url' => $revision
->toUrl('revision'),
'#prefix' => '<div class="diff-revision__item diff-revision__item-date">',
'#suffix' => '</div>',
];
$revision_link['author'] = [
'#type' => 'link',
'#title' => $revision
->getRevisionUser()
->getDisplayName(),
'#url' => Url::fromUri(\Drupal::request()
->getUriForPath('/user/' . $user_id)),
'#theme' => 'username',
'#account' => $revision
->getRevisionUser(),
'#prefix' => '<div class="diff-revision__item diff-revision__item-author">',
'#suffix' => '</div>',
];
if ($revision_log) {
$revision_link['message'] = [
'#type' => 'markup',
'#prefix' => '<div class="diff-revision__item diff-revision__item-message">',
'#suffix' => '</div>',
'#markup' => $revision_log,
];
}
}
else {
$revision_link['label'] = [
'#type' => 'link',
'#title' => $revision
->label(),
'#url' => $revision
->toUrl('revision'),
'#prefix' => '<div class="diff-revision__item diff-revision__item-date">',
'#suffix' => '</div>',
];
}
return $revision_link;
}
protected function buildFilterNavigation(ContentEntityInterface $entity, ContentEntityInterface $left_revision, ContentEntityInterface $right_revision, $layout, $active_filter) {
$options['raw'] = [
'title' => $this
->t('Raw'),
'url' => PluginRevisionController::diffRoute($entity, $left_revision
->getRevisionId(), $right_revision
->getRevisionId(), $layout, [
'filter' => 'raw',
]),
];
$options['strip_tags'] = [
'title' => $this
->t('Strip tags'),
'url' => PluginRevisionController::diffRoute($entity, $left_revision
->getRevisionId(), $right_revision
->getRevisionId(), $layout, [
'filter' => 'strip_tags',
]),
];
$filter = $options[$active_filter];
unset($options[$active_filter]);
array_unshift($options, $filter);
$build['options'] = [
'#type' => 'operations',
'#links' => $options,
];
return $build;
}
protected function applyMarkdown($markdown, $items) {
if (!$markdown) {
return $items;
}
if ($markdown == 'drupal_html_to_text') {
return trim(MailFormatHelper::htmlToText($items), "\n");
}
elseif ($markdown == 'filter_xss') {
return trim(Xss::filter($items), "\n");
}
elseif ($markdown == 'filter_xss_all') {
return trim(Xss::filter($items, []), "\n");
}
else {
return $items;
}
}
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
}
public function validateConfigurationForm(array &$form, FormStateInterface $form_state) {
}
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
}
public function defaultConfiguration() {
return [];
}
public function getConfiguration() {
return $this->configFactory
->getEditable('diff.layout_plugins');
}
public function setConfiguration(array $configuration) {
$config = $this->configFactory
->getEditable('diff.layout_plugins');
$config
->set($this->pluginId, $configuration);
$config
->save();
}
}