This project is not covered by Drupal’s security advisory policy.

Utility module for use when developing custom or contributed modules with in-code saving of entities where you want to create a new revision.

Use it like this:

\Drupal::service('revision_helper.entity')->setRevision($node, TRUE, "Sync from data API.");

The motivation for this module was being frustrated that revisions were not automatic when a content type was set to make revisions by default, and then much further frustration that translateable entities needed a different set of steps. Ironically we are not yet accounting for that, this is what our custom code needed.

The line above is basically simply doing this:

      // Make this change a new revision
      $node->setNewRevision(TRUE);
      $node->revision_log = 'Sync from data API';
      $node->setRevisionCreationTime(REQUEST_TIME);
      $node->setRevisionUserId(11142);

with extra checks that revisions are available etc.

What we expect to have to add, when the use case comes up again and after a lot of testing, is a fallback/addition for translation-having content lke this:

      // This node type has translations, although it should not,
      // so for now we need this silliness to save a revision.
      $storage =\Drupal::entityTypeManager()->getStorage('node');
      $revision = $node->getTranslation(LanguageInterface::LANGCODE_DEFAULT);
      $revision->setRevisionCreationTime(\Drupal::time()->getCurrentTime());
      $revision = $storage->createRevision($revision);
      $revision->save();

Again, that is not implemented yet, and not 100% sure it will be needed.

You can support Agaric's overall contributions to Drupal and a bit beyond by supporting Drutopia at opencollective.com/drutopia. Thanks!

Supporting organizations: 
Created the module.

Project information

  • Project categories: Developer tools
  • Created by mlncn on , updated
  • shield alertThis project is not covered by the security advisory policy.
    Use at your own risk! It may have publicly disclosed vulnerabilities.

Releases