Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Create an entry approval workflow with Statamic Revisions

Posted on Sep 14 When managing entries in your Statamic application that gather external data, such as from APIs, you may find it necessary to implement a data control step before publication. Statamic offers a useful tool for this purpose known as the revisions feature, specifically focusing on working copies.In the following sections, I'll guide you through the process of creating an Approval Mechanism for Statamic.This article is structured as follows:This article is primarily aimed at Statamic developers who are already acquainted with Statamic's core concepts and have a basic understanding of PHP and Laravel. To fully comprehend and implement the Approval Mechanism discussed here, readers should be familiar with key Statamic elements like entries and blueprints.Additionally, a basic grasp of Vue.js for handling user interface components and API integration knowledge will prove beneficial in following concepts presented in this article.I would recommended that you also have a basic understanding of extending Statamic, but if you're new to this, it's a good idea to explore those concepts beforehand for a smoother learning experience.You need Statamic Pro to enable the revisions feature.In this article, our primary objective is to create an Approval Mechanism within Statamic, emphasizing the process of managing entries and enabling a structured review and Feedback workflow. While we briefly touch on data import as context, our main focus lies in developing this mechanism. We'll explore how to efficiently flag entries as 'Needs Review,' facilitate feedback from CMS authors through a built-in feedback form, and dynamically update entry statuses for better organization.Additionally, we'll demonstrate how to log feedback and maintain feedback history as part of this comprehensive solution. For illustration, we'll use a 'persons' collection where each person is associated with a unique 'external_service_id,' linking them to external data sources.Working copies serve as a temporary version of an entry, generated when you edit and save an existing entry. You then have the option to create a revision or proceed with publication. Should you opt for a revision or publication, the working copy will be discarded. Conversely, choosing to save your changes ensures that the working copy is promptly updated.By default working copies are saved inside the revisions folder. The path can be set in the config/statamic/revisions.php file. By default it's set to storage_path('statamic/revisions').We assume we already have some existing persons in our collection. Inside our import method we want to update or create a working copy of each entry.Now let's implement these methods. We are assuming that the new data comes in an array that represents the data structure of our person blueprint. For example:Creating the working copyUpdating the working copy is a little more complicated since the data can't be set directly on the working copy. We have to get the data from the working copy, update it and then save it again.So, at the moment, we always create or update a working copy and overwrite a possible existing one. In this case, that's okay, but you may want to check if there is already a working copy, and if so, create a new revision instead of overwriting the working copy. But that results in potentially a lot of revisions. So you have to decide what's best for you. I will only cover the working copy case here.Additionally, you should have a field in your entries blueprint where you save the last imported date to check if the incoming data is newer than the last import. If not, you can skip the entry. That, of course, presupposes that your external data also provides some kind of 'updated_at' field.What we did before is basically everything you need to have a simple approval mechanism where new data gets saved inside a working copy to be reviewed. But we want to give the CMS authors a hint that there is something to review. We will do this by adding a status field.What we need is a custom field type that we can use in our blueprint. I won't go into detail here about how to create a custom field type. You can read about it in the Statamic documentation.We will create a custom field type called 'approval_status' that will have three possible values: 'needs_review,' 'approved,' and 'feedback_sent.'We will add some methods to our ApprovalStatus.php fieldtype class to check the status.As you can see, it can be better to outsource the logic to a separate class. We will create an ApprovalStatus class that will have a static method to get the status. This method will use a variable $feedbackDate. This variable will be set when the CMS author gives feedback and deleted when the working copy is published. We will cover this later.Here's a little diagram that visually explains the logic behind the status check:… and the corresponding code:The Vue components to our ApprovalStatus field type class are pretty simple. They get the status with the help of the preload() and the preProcessIndex() methods.You can also add your classes and pass a color to your status array to style the status in the control panel. You can find more information about index field types here: Statamic Index FieldtypesThe form is also a custom fieldtype. It will have a textarea and a button to submit the feedback. We assume that the person entry has an email stored. We can write our feedback into the text field and with the click on the button, we will make a post request to a custom route that takes care of error handling and sending the mail. After sending the mail, we want to disable the form and the button to prevent multiple feedbacks.This is how the Vue component can look like:To allow a better overview of previous sent feedback, you can log your feedback in a replicator field below the feedback form. Simply add some code to the method that handles your post request from the form component above. Let's assume this method is inside the same class as the create and update working copy methods.To finish up the approval mechanism, we just have to delete the feedback date automatically when the entry gets published. We can achieve this by listening to the EntrySaved vent. We will create a listener class that will check if the entry is from the persons collection and has set a feedback date and if so, delete the feedback date.This is how the Feedback UI could look like:Great! 🎉 We now have a simple approval mechanism for our Statamic application. Of course, you can extend this mechanism to your needs. For example, you can add a field to your entries blueprint where you can set the email address of the person that should review the entry or for the person that should review it. It's also very handy to add a filter to your index view to filter for entries that need a review.When handling multisites, you should think about if you want to have a separate approval mechanism for each site or if you want to have a global one. In my case, I disabled the review form for any other language than the default one, and the authors are always sending reviews for all localizations in one mail. When creating and updating the working copy, you have to update each localization separately. Take a look at the $entry->descendants() method to get all localizations of an entry.I hope this article was helpful for you. If you have any questions or suggestions, please let me know in the comments. 👋Templates let you quickly answer FAQs or store snippets for re-use. Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink. Hide child comments as well Confirm For further actions, you may consider blocking this person and/or reporting abuse Piyali Debroy - Aug 17 Sebastian Wessel - Sep 8 Sebastian Wessel - Sep 8 Mike Vardy - Aug 30 We are hiring. Once suspended, visuellverstehen will not be able to comment or publish posts until their suspension is removed. Once unsuspended, visuellverstehen will be able to comment and publish posts again. Once unpublished, all posts by visuellverstehen will become hidden and only accessible to themselves. If visuellverstehen is not suspended, they can still re-publish their posts from their dashboard. Note: Once unpublished, this post will become invisible to the public and only accessible to Lakkes. They can still re-publish the post if they are not suspended. Thanks for keeping DEV Community safe. Here is what you can do to flag visuellverstehen: visuellverstehen consistently posts content that violates DEV Community's code of conduct because it is harassing, offensive or spammy. Unflagging visuellverstehen will restore default visibility to their posts. DEV Community — A constructive and inclusive social network for software developers. With you every step of your journey. Built on Forem — the open source software that powers DEV and other inclusive communities.Made with love and Ruby on Rails. DEV Community © 2016 - 2023. We're a place where coders share, stay up-to-date and grow their careers.



This post first appeared on VedVyas Articles, please read the originial post: here

Share the post

Create an entry approval workflow with Statamic Revisions

×

Subscribe to Vedvyas Articles

Get updates delivered right to your inbox!

Thank you for your subscription

×