View source
<?php
namespace Drupal\node_authlink\Form;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\node\Entity\Node;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Config\ConfigFactoryInterface;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Drupal\Core\Entity\EntityTypeManagerInterface;
class NodeAuthlinkNodeForm extends FormBase {
protected $configFactory;
protected $entityTypeManager;
public function __construct(ConfigFactoryInterface $config_factory, EntityTypeManagerInterface $entity_type_manager) {
$this->configFactory = $config_factory;
$this->entityTypeManager = $entity_type_manager;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('config.factory'), $container
->get('entity_type.manager'));
}
public function getFormId() {
return 'node_authlink_node_form';
}
public function buildForm(array $form, FormStateInterface $form_state, $node = NULL) {
if (!is_numeric($node)) {
throw new NotFoundHttpException();
}
$config = $this->configFactory
->get('node_authlink.settings');
$config_grants = $config
->get('grants');
$node = Node::load($node);
$form['disclaimer'] = [
'#type' => 'markup',
'#markup' => '<p>' . $this
->t('Use the following form to manage anonymous authlinks for performing View, Update or Delete tasks without any further authentication. The links available will depends on the configuration of this content type.') . '</p>',
];
if (isset($config_grants[$node
->bundle()])) {
foreach ($config_grants[$node
->bundle()] as $op) {
if (!$op) {
continue;
}
$has_revisions = FALSE;
if ($op == 'view') {
$has_revisions = TRUE;
$node_storage = $this->entityTypeManager
->getStorage('node');
$result = $node_storage
->getQuery()
->allRevisions()
->condition($node
->getEntityType()
->getKey('id'), $node
->id())
->sort($node
->getEntityType()
->getKey('revision'), 'DESC')
->range(0, 50)
->execute();
if (!empty($result)) {
$revision_options = [];
foreach ($result as $vid => $nid) {
$revision = $node_storage
->loadRevision($vid);
$langcode = $node
->language()
->getId();
if ($revision
->hasTranslation($langcode) && $revision
->getTranslation($langcode)
->isRevisionTranslationAffected()) {
$dateFormatter = \Drupal::service('date.formatter');
$date = $dateFormatter
->format($revision->revision_timestamp->value, 'short');
if ($revision
->isDefaultRevision()) {
$revision_options[$vid] = [
'text' => $this
->t('Current revision'),
'url' => node_authlink_get_url($node, $op),
];
}
else {
$revision_options[$vid] = [
'text' => $date,
'url' => node_authlink_get_url($node, $op, $vid),
];
}
}
}
}
}
if ($has_revisions) {
$form['revisions'] = [
'#type' => 'select',
'#title' => $this
->t('Revisions'),
'#options' => [],
];
foreach ($revision_options as $vid => $revision_option) {
$form['revisions']['#options'][$vid] = $revision_option['text'];
$form['link_' . $op . '_' . $vid] = [
'#type' => 'item',
'#markup' => "<p><strong>" . $op . "</strong>: " . $revision_option['url'] . "</p>",
'#states' => [
'visible' => [
'[name="revisions"]' => [
'value' => $vid,
],
],
],
];
}
}
else {
$url = node_authlink_get_url($node, $op);
if ($url) {
$form['link_' . $op] = [
'#type' => 'item',
'#markup' => "<p><strong>{$op}</strong>: {$url}</p>",
];
}
}
}
if (node_authlink_load_authkey($node
->id())) {
$form['delete'] = [
'#type' => 'submit',
'#value' => $this
->t('Delete authlink'),
'#weight' => 10,
'#submit' => [
'::deleteAuthlink',
],
];
}
else {
$form['create'] = [
'#type' => 'submit',
'#value' => $this
->t('Create authlink'),
'#weight' => 10,
'#submit' => [
'::createAuthlink',
],
];
}
}
return $form;
}
public function createAuthlink(array &$form, FormStateInterface $form_state) {
node_authlink_create($form_state
->getBuildInfo()['args'][0]);
}
public function deleteAuthlink(array &$form, FormStateInterface $form_state) {
node_authlink_delete($form_state
->getBuildInfo()['args'][0]);
}
public function validateForm(array &$form, FormStateInterface $form_state) {
parent::validateForm($form, $form_state);
}
public function submitForm(array &$form, FormStateInterface $form_state) {
foreach ($form_state
->getValues() as $key => $value) {
$this
->messenger()
->addMessage($key . ': ' . $value);
}
}
public function access(AccountInterface $account, $node) {
if (is_numeric($node)) {
$node = Node::load($node);
$enable = $this
->config('node_authlink.settings')
->get('enable');
if (isset($enable[$node
->bundle()]) && $enable[$node
->bundle()] && ($account
->hasPermission('create and delete node authlinks') || $account
->hasPermission(sprintf('create and delete node %s authlinks', $node
->bundle())))) {
return AccessResult::allowed();
}
}
return AccessResult::forbidden();
}
}