/**
  * {@inheritdoc}
  */
 public function submitForm(array &$form, FormStateInterface $form_state)
 {
     $this->revision->setNewRevision();
     // Make this the new default revision for the node.
     $this->revision->isDefaultRevision(TRUE);
     // The revision timestamp will be updated when the revision is saved. Keep the
     // original one for the confirmation message.
     $original_revision_timestamp = $this->revision->getRevisionCreationTime();
     $this->revision->revision_log = t('Copy of the revision from %date.', array('%date' => format_date($original_revision_timestamp)));
     $this->revision->save();
     $this->logger('content')->notice('@type: reverted %title revision %revision.', array('@type' => $this->revision->bundle(), '%title' => $this->revision->label(), '%revision' => $this->revision->getRevisionId()));
     drupal_set_message(t('@type %title has been reverted to the revision from %revision-date.', array('@type' => node_get_type_label($this->revision), '%title' => $this->revision->label(), '%revision-date' => format_date($original_revision_timestamp))));
     $form_state->setRedirect('entity.node.version_history', array('node' => $this->revision->id()));
 }
 /**
  * {@inheritdoc}
  */
 public function submitForm(array &$form, array &$form_state)
 {
     $this->revision->setNewRevision();
     // Make this the new default revision for the node.
     $this->revision->isDefaultRevision(TRUE);
     // The revision timestamp will be updated when the revision is saved. Keep the
     // original one for the confirmation message.
     $original_revision_timestamp = $this->revision->getRevisionCreationTime();
     $this->revision->revision_log = t('Copy of the revision from %date.', array('%date' => format_date($original_revision_timestamp)));
     $this->revision->save();
     watchdog('content', '@type: reverted %title revision %revision.', array('@type' => $this->revision->bundle(), '%title' => $this->revision->label(), '%revision' => $this->revision->getRevisionId()));
     drupal_set_message(t('@type %title has been reverted back to the revision from %revision-date.', array('@type' => node_get_type_label($this->revision), '%title' => $this->revision->label(), '%revision-date' => format_date($original_revision_timestamp))));
     $form_state['redirect_route'] = array('route_name' => 'node.revision_overview', 'route_parameters' => array('node' => $this->revision->id()));
 }
 /**
  * Checks node revision access.
  *
  * @param \Drupal\node\NodeInterface $node
  *   The node 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.'
  *
  * @return bool
  *   TRUE if the operation may be performed, FALSE otherwise.
  */
 public function checkAccess(NodeInterface $node, AccountInterface $account, $op = 'view')
 {
     $map = array('view' => 'view all revisions', 'update' => 'revert all revisions', 'delete' => 'delete all revisions');
     $bundle = $node->bundle();
     $type_map = array('view' => "view {$bundle} revisions", 'update' => "revert {$bundle} revisions", 'delete' => "delete {$bundle} revisions");
     if (!$node || !isset($map[$op]) || !isset($type_map[$op])) {
         // If there was no node to check against, or the $op was not one of the
         // supported ones, we return access denied.
         return FALSE;
     }
     // Statically cache access by revision ID, language code, user account ID,
     // and operation.
     $langcode = $node->language()->getId();
     $cid = $node->getRevisionId() . ':' . $langcode . ':' . $account->id() . ':' . $op;
     if (!isset($this->access[$cid])) {
         // Perform basic permission checks first.
         if (!$account->hasPermission($map[$op]) && !$account->hasPermission($type_map[$op]) && !$account->hasPermission('administer nodes')) {
             $this->access[$cid] = FALSE;
             return FALSE;
         }
         // There should be at least two revisions. If the vid of the given node
         // and the vid of the default revision differ, then we already have two
         // different revisions so there is no need for a separate database check.
         // Also, if you try to revert to or delete the default revision, that's
         // not good.
         if ($node->isDefaultRevision() && ($this->nodeStorage->countDefaultLanguageRevisions($node) == 1 || $op == 'update' || $op == 'delete')) {
             $this->access[$cid] = FALSE;
         } elseif ($account->hasPermission('administer nodes')) {
             $this->access[$cid] = TRUE;
         } else {
             // First check the access to the default revision and finally, if the
             // node passed in is not the default revision then access to that, too.
             $this->access[$cid] = $this->nodeAccess->access($this->nodeStorage->load($node->id()), $op, $account) && ($node->isDefaultRevision() || $this->nodeAccess->access($node, $op, $account));
         }
     }
     return $this->access[$cid];
 }
Esempio n. 4
0
 /**
  * Prepares a revision to be reverted.
  *
  * @param \Drupal\node\NodeInterface $revision
  *   The revision to be reverted.
  * @param \Drupal\Core\Form\FormStateInterface $form_state
  *   The current state of the form.
  *
  * @return \Drupal\node\NodeInterface
  *   The prepared revision ready to be stored.
  */
 protected function prepareRevertedRevision(NodeInterface $revision, FormStateInterface $form_state)
 {
     $revision->setNewRevision();
     $revision->isDefaultRevision(TRUE);
     return $revision;
 }
Esempio n. 5
0
 /**
  * Prepares a revision to be reverted.
  *
  * @param \Drupal\node\NodeInterface $revision
  *   The revision to be reverted.
  *
  * @return \Drupal\node\NodeInterface
  *   The prepared revision ready to be stored.
  */
 protected function prepareRevertedRevision(NodeInterface $revision)
 {
     /** @var \Drupal\node\NodeInterface $default_revision */
     $default_revision = $this->nodeStorage->load($revision->id());
     // If the entity is translated, make sure only translations affected by the
     // specified revision are reverted.
     $languages = $default_revision->getTranslationLanguages();
     if (count($languages) > 1) {
         // @todo Instead of processing all the available translations, we should
         //   let the user decide which translations should be reverted. See
         //   https://www.drupal.org/node/2465907.
         foreach ($languages as $langcode => $language) {
             if ($revision->hasTranslation($langcode) && !$revision->getTranslation($langcode)->isRevisionTranslationAffected()) {
                 $revision_translation = $revision->getTranslation($langcode);
                 $default_translation = $default_revision->getTranslation($langcode);
                 foreach ($default_revision->getFieldDefinitions() as $field_name => $definition) {
                     if ($definition->isTranslatable()) {
                         $revision_translation->set($field_name, $default_translation->get($field_name)->getValue());
                     }
                 }
             }
         }
     }
     $revision->setNewRevision();
     $revision->isDefaultRevision(TRUE);
     return $revision;
 }