Commit 25029299 authored by David Beasley's avatar David Beasley Committed by Benjamin Melançon
Browse files

Issue #3139411 by dabley, ashutosh.mishra, JasonLuttrell, Ibn al-Hazardous,...

Issue #3139411 by dabley, ashutosh.mishra, JasonLuttrell, Ibn al-Hazardous, mlncn: Drupal 9 Compatibility
parent 4e3a6767
Loading
Loading
Loading
Loading

composer.json

0 → 100644
+16 −0
Original line number Diff line number Diff line
{
  "name": "drupal/user_revision",
  "description": "Provides revision support for users.",
  "type": "drupal-module",
  "homepage": "https://www.drupal.org/project/user_revision",
  "authors": [
    {
      "name": "See contributors",
      "homepage": "https://www.drupal.org/node/1051280/committers"
    }
  ],
  "support": {
    "issues": "https://www.drupal.org/project/issues/user_revision"
  },
  "license": "GPL-2.0+"
}
+9 −16
Original line number Diff line number Diff line
@@ -26,14 +26,7 @@ class UserRevisionAccessCheck implements AccessInterface {
   *
   * @var array
   */
  protected $access = array();

  /**
   * Constructs a new UserRevisionAccessCheck.
   *
   * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
   *   The entity manager.
   */
  protected $access = [];

  /**
   * UserRevisionAccessCheck constructor.
@@ -77,28 +70,28 @@ class UserRevisionAccessCheck implements AccessInterface {
  /**
   * Checks user revision access.
   *
   * @param \Drupal\node\NodeInterface $node
   *   The node to check.
   * @param \Drupal\user\UserInterface $user
   *   The user to check.
   * @param \Drupal\Core\Session\AccountInterface $account
   *   A user object representing the user for whom the operation is to be
   *   performed.
   * @param string $op
   *   (optional) The specific operation being checked. Defaults to 'view.'
   *   (optional) The specific operation being checked. Defaults to 'view'.
   *
   * @return bool
   *   TRUE if the operation may be performed, FALSE otherwise.
   */
  public function checkAccess(UserInterface $user, AccountInterface $account, $op = 'view') {
    $map = array(
    $map = [
      'view' => 'view all user revisions',
      'update' => 'revert all user revisions',
      'delete' => 'delete all user revisions',
    );
    $own_map = array(
    ];
    $own_map = [
      'view' => 'view own user revisions',
      'update' => 'revert own user revisions',
      'delete' => 'delete own user revisions',
    );
    ];

    if (!$user || !isset($map[$op]) || !isset($own_map[$op])) {
      // If there was no user to check against, or the $op was not one of the
@@ -111,7 +104,7 @@ class UserRevisionAccessCheck implements AccessInterface {
      return FALSE;
    }

    // Check minimal revisions count
    // Check minimal revisions count.
    if (user_revision_count($user) < 2) {
      return FALSE;
    }
+17 −16
Original line number Diff line number Diff line
@@ -70,13 +70,14 @@ class UserController extends ControllerBase implements ContainerInjectionInterfa
   */
  public function revisionOverview(UserInterface $user) {
    $account = $this->currentUser();
    $user_storage = $this->entityTypeManager->getStorage('user');
    $user_storage = $this->entityTypeManager()->getStorage('user');
    $access_check = new UserRevisionAccessCheck($this->entityTypeManager());

    $build = array();
    $build['#title'] = $this->t('Revisions for %title', array('%title' => $user->label()));
    $header = array($this->t('Revision'), $this->t('Operations'));
    $build = [];
    $build['#title'] = $this->t('Revisions for %title', ['%title' => $user->label()]);
    $header = [$this->t('Revision'), $this->t('Operations')];

    $rows = array();
    $rows = [];

    $vids = user_revision_ids($user);

@@ -95,7 +96,7 @@ class UserController extends ControllerBase implements ContainerInjectionInterfa
          $link = $user->toLink($date)->toString();
        }
        else {
          $link = Link::fromTextAndUrl($date, new Url('entity.user.revision', array('user' => $user->id(), 'user_revision' => $vid)))->toString();
          $link = Link::fromTextAndUrl($date, new Url('entity.user.revision', ['user' => $user->id(), 'user_revision' => $vid]))->toString();
        }


@@ -158,14 +159,14 @@ class UserController extends ControllerBase implements ContainerInjectionInterfa
      }
    }

    $build['user_revisions_table'] = array(
    $build['user_revisions_table'] = [
      '#theme' => 'table',
      '#rows' => $rows,
      '#header' => $header,
      '#attached' => array(
        'library' => array('user_revision/user.admin')
      )
    );
      '#attached' => [
        'library' => ['user_revision/user.admin'],
      ],
    ];

    return $build;
  }
@@ -185,12 +186,12 @@ class UserController extends ControllerBase implements ContainerInjectionInterfa
   * @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
   */
  public function revisionShow($user, $user_revision) {
    $user_history = $this->entityTypeManager->getStorage('user')->loadRevision($user_revision);
    $user_history = $this->entityTypeManager()->getStorage('user')->loadRevision($user_revision);
    if ($user_history->id() != $user) {
      throw new NotFoundHttpException;
      throw new NotFoundHttpException();
    }
    /* @var $view_builder \Drupal\Core\Entity\EntityViewBuilder */
    $view_builder = $this->entityTypeManager->getViewBuilder($user_history->getEntityTypeId());
    $view_builder = $this->entityTypeManager()->getViewBuilder($user_history->getEntityTypeId());
    return $view_builder->view($user_history);
  }

@@ -208,8 +209,8 @@ class UserController extends ControllerBase implements ContainerInjectionInterfa
   * @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
   */
  public function revisionPageTitle($user_revision) {
    $user = $this->entityTypeManager->getStorage('user')->loadRevision($user_revision);
    return $this->t('Revision of %title from %date', array('%title' => $user->label(), '%date' => $this->dateFormatter->format($user->get('revision_timestamp')->value)));
    $user = $this->entityTypeManager()->getStorage('user')->loadRevision($user_revision);
    return $this->t('Revision of %title from %date', ['%title' => $user->label(), '%date' => $this->dateFormatter->format($user->get('revision_timestamp')->value)]);
  }

}
+21 −10
Original line number Diff line number Diff line
@@ -8,6 +8,7 @@ use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Drupal\Core\Datetime\DateFormatter;

/**
 * Provides a form for delete a user revision.
@@ -27,24 +28,34 @@ class UserRevisionDeleteForm extends ConfirmFormBase {
   * @var \Drupal\Core\Entity\EntityStorageInterface
   */
  protected $userStorage;
  /**
   * The date formatter service.
   *
   * @var \Drupal\Core\Datetime\DateFormatter
   */
  protected $dateFormatter;

  /**
   * Constructs a new UserRevisionDeleteForm.
   *
   * @param \Drupal\Core\Entity\EntityStorageInterface $user_storage
   *   The user storage.
   * @param \Drupal\Core\Datetime\DateFormatter $date_formatter
   *   The date formatter service.
   */
  public function __construct(EntityStorageInterface $user_storage) {
  public function __construct(EntityStorageInterface $user_storage, DateFormatter $date_formatter) {
    $this->userStorage = $user_storage;
    $this->dateFormatter = $date_formatter;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    $entity_manager = $container->get('entity.manager');
    $entity_manager = $container->get('entity_type.manager');
    return new static(
      $entity_manager->getStorage('user')
      $entity_manager->getStorage('user'),
      $container->get('date.formatter')
    );
  }

@@ -59,14 +70,14 @@ class UserRevisionDeleteForm extends ConfirmFormBase {
   * {@inheritdoc}
   */
  public function getQuestion() {
    return t('Are you sure you want to delete the revision from %revision-date?', array('%revision-date' => \Drupal::service('date.formatter')->format($this->revision->revision_timestamp->value)));
    return t('Are you sure you want to delete the revision from %revision-date?', ['%revision-date' => $this->dateFormatter->format($this->revision->revision_timestamp->value)]);
  }

  /**
   * {@inheritdoc}
   */
  public function getCancelUrl() {
    return new Url('entity.user.version_history', array('user' => $this->revision->id()));
    return new Url('entity.user.version_history', ['user' => $this->revision->id()]);
  }

  /**
@@ -82,7 +93,7 @@ class UserRevisionDeleteForm extends ConfirmFormBase {
  public function buildForm(array $form, FormStateInterface $form_state, $user = NULL, $user_revision = NULL) {
    $this->revision = $this->userStorage->loadRevision($user_revision);
    if ($this->revision->id() != $user) {
      throw new NotFoundHttpException;
      throw new NotFoundHttpException();
    }
    return parent::buildForm($form, $form_state);
  }
@@ -93,15 +104,15 @@ class UserRevisionDeleteForm extends ConfirmFormBase {
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $this->userStorage->deleteRevision($this->revision->getRevisionId());

    $this->logger('user_revision')->notice('user: deleted %name revision %revision.', array('%name' => $this->revision->label(), '%revision' => $this->revision->getRevisionId()));
    $this->messenger()->addStatus(t('Revision from %revision-date of user %name has been deleted.', array('%revision-date' => \Drupal::service('date.formatter')->format($this->revision->revision_timestamp->value), '%name' => $this->revision->label())));
    $this->logger('user_revision')->notice('user: deleted %name revision %revision.', ['%name' => $this->revision->label(), '%revision' => $this->revision->getRevisionId()]);
    $this->messenger()->addMessage($this->t('Revision from %revision-date of user %name has been deleted.', ['%revision-date' => $this->dateFormatter->format($this->revision->revision_timestamp->value), '%name' => $this->revision->label()]));
    $form_state->setRedirect(
      'entity.user.canonical', array('user' => $this->revision->id())
      'entity.user.canonical', ['user' => $this->revision->id()]
    );

    if (user_revision_count($this->revision) > 1) {
      $form_state->setRedirect(
        'entity.user.version_history', array('user' => $this->revision->id())
        'entity.user.version_history', ['user' => $this->revision->id()]
      );
    }
  }
+22 −10
Original line number Diff line number Diff line
@@ -8,6 +8,7 @@ use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Drupal\Core\Datetime\DateFormatter;

/**
 * Provides a form for reverting a user revision.
@@ -27,15 +28,24 @@ class UserRevisionRevertForm extends ConfirmFormBase {
   * @var \Drupal\Core\Entity\EntityStorageInterface
   */
  protected $userStorage;
  /**
   * The date formatter service.
   *
   * @var \Drupal\Core\Datetime\DateFormatter
   */
  protected $dateFormatter;

  /**
   * Constructs a new UserRevisionRevertForm.
   *
   * @param \Drupal\Core\Entity\EntityStorageInterface $user_storage
   *   The user storage.
   * @param \Drupal\Core\Datetime\DateFormatter $date_formatter
   *   The date formatter service.
   */
  public function __construct(EntityStorageInterface $user_storage) {
  public function __construct(EntityStorageInterface $user_storage, DateFormatter $date_formatter) {
    $this->userStorage = $user_storage;
    $this->dateFormatter = $date_formatter;
  }

  /**
@@ -43,7 +53,8 @@ class UserRevisionRevertForm extends ConfirmFormBase {
   */
  public static function create(ContainerInterface $container) {
    return new static(
      $container->get('entity.manager')->getStorage('user')
      $container->get('entity_type.manager')->getStorage('user'),
      $container->get('date.formatter')
    );
  }

@@ -58,14 +69,14 @@ class UserRevisionRevertForm extends ConfirmFormBase {
   * {@inheritdoc}
   */
  public function getQuestion() {
    return t('Are you sure you want to revert to the revision from %revision-date?', array('%revision-date' => \Drupal::service('date.formatter')->format($this->revision->revision_timestamp->value)));
    return t('Are you sure you want to revert to the revision from %revision-date?', ['%revision-date' => $this->dateFormatter->format($this->revision->revision_timestamp->value)]);
  }

  /**
   * {@inheritdoc}
   */
  public function getCancelUrl() {
    return new Url('entity.user.version_history', array('user' => $this->revision->id()));
    return new Url('entity.user.version_history', ['user' => $this->revision->id()]);
  }

  /**
@@ -88,7 +99,7 @@ class UserRevisionRevertForm extends ConfirmFormBase {
  public function buildForm(array $form, FormStateInterface $form_state, $user = NULL, $user_revision = NULL) {
    $this->revision = $this->userStorage->loadRevision($user_revision);
    if ($this->revision->id() != $user) {
      throw new NotFoundHttpException;
      throw new NotFoundHttpException();
    }
    return parent::buildForm($form, $form_state);
  }
@@ -101,17 +112,18 @@ class UserRevisionRevertForm extends ConfirmFormBase {
    // Make this the new default revision for the user.
    $this->revision->isDefaultRevision(TRUE);

    // The revision timestamp will be updated when the revision is saved. Keep the
    // The revision timestamp will be updated when the revision is saved.
    // Keep the
    // original one for the confirmation message.
    $original_revision_timestamp = $this->revision->revision_timestamp->value;

    $this->revision->revision_log = t('Copy of the revision from %date.', array('%date' => \Drupal::service('date.formatter')->format($original_revision_timestamp)));
    $this->revision->revision_log = $this->t('Copy of the revision from %date.', ['%date' => $this->dateFormatter->format($original_revision_timestamp)]);
    $this->revision->save();

    $this->logger('user_revision')->notice('user: reverted %name revision %revision.', array('%name' => $this->revision->label(), '%revision' => $this->revision->getRevisionId()));
    $this->messenger()->addStatus(t('User %name has been reverted back to the revision from %revision-date.', array('%name' => $this->revision->label(), '%revision-date' => \Drupal::service('date.formatter')->format($original_revision_timestamp))));
    $this->logger('user_revision')->notice('user: reverted %name revision %revision.', ['%name' => $this->revision->label(), '%revision' => $this->revision->getRevisionId()]);
    $this->messenger()->addMessage($this->t('User %name has been reverted back to the revision from %revision-date.', ['%name' => $this->revision->label(), '%revision-date' => $this->dateFormatter->format($original_revision_timestamp)]));
    $form_state->setRedirect(
      'entity.user.version_history', array('user' => $this->revision->id())
      'entity.user.version_history', ['user' => $this->revision->id()]
    );
  }

Loading