3

We are trying to figure out a way to use the Flag module to prevent concurrent editing. In our research, we found the Content locking (anti-concurrent editing) module, but we thought since we are already using the flag module, that we could also use it for content locking.

The scenario would be if a user (person a) is already editing content A, and then another user (person b) tries to edit content A, then person b should not be allowed to edit content A until person a is done editing the content.

What is the best way to do this because we are having trouble trying to find the right combination to get this done? We were thinking once a content is being edited, a flag is set to lock the content and whenever another user tries to edit the content, they are prompted with a message "person a is already editing the content. please wait until they are done".

Is this possible with the Flag module? Would I need to use the Rules modules as well?

2

Below are 2 possible solutions (options) to address this question (which I assume is about D7) ...

Option 1: Use the Rules and Flag modules

Yes you can implement such edit-in-progress (EIP) feature using the Flag module, but you'll also need the Rules module to get it to work. Read on for more details ...

Step 1: Create an edit-in-progress flag

Create a global (not user specific) flag, and say you name (label) it edit-in-progress flag (='EIP' for short). The purpose of this flag is like so:

  • If this flag is turned on for a node, it means "somebody is editing this node, so do not allow any additional edit sessions for this node to be started".
  • If this flag is not turned on for a node, it means "the node is available for authorized users to start editing it".

Step 2: Create a rule to disallow edit if the EIP flag is set

Create a rule to disallow edit of a node if the EIP flag for a node is already set. To recognize a request to start an edit session for a node, use an URL like node/xyz/edit (with xyz = nid of the node).

Refer to my answer to the question about "How to restrict access to a node via node/12 and allow access via a path like content/sometitle?", which contains a rule that is pretty close to what's needed for this rule here:

  • Use the same Rules Event, i.e. "Drupal is initializing" (you want to catch this edit event via Rules BEFORE the edit session actually allowed to be started!).

  • Slightly adapt the regular expression, by adding /edit to it.

  • Add an extra condition to verify if the EIP-flag for this node is ALREADY turned on (which indicates some other edit session for this node was started already).

  • Adapt the Rules Action to (a) issue an appropriate message (that some other edit session is already started) and (b) redirect the user to whatever other path that fits your needs (e.g: just node/xzy).

Step 3: Create a rule to SET the EIP flag when an edit starts

Create a rule to SET the EIP flag for a node whenever an authorized user requests to start an edit session for a node. To recognize such request, use an URL like node/xyz/edit (with xyz = nid of the node).

Refer to my answer mentioned in Step 2, which contains a rule that is pretty close to what's needed for this rule here:

  • Use the same Rules Event, i.e. "Drupal is initializing" (you want to catch this edit event via Rules BEFORE the edit session actually allowed to be started!).

  • Slightly adapt the regular expression, by adding /edit to it.

  • Add an extra condition to verify if the EIP-flag for this node is NOT turned on (which indicates no other edit session for this node exists).

  • Adapt the Rules Action to SET the EIP flag.

Step 4: Create a rule to UNSET the EIP flag when an edit ends

Create a rule to UNSET the EIP flag for a node whenever an authorized user requests to end an edit session for a node.

Here is a summary of this rule:

  • Use Rules Event "after saving content".
  • Use Rules Action to UNSET the EIP flag.

Step 5: Merge Step 2 and Step 3 (optional)

Should you want to do/consider so, you should be able to merge the 2 Rules from Step 2 and Step 3 in a single Rule, using the Conditional Rules module. Refer to my answer to the question How to prevent a node being saved when using the Rules module? for a sample of how to use this module (for which not a lot of documentation seems to exist).

Option 2: Use other modules

For anybody who does not want (or is unable) to use the combination of the Flag and Rules modules as described above, there are some alternative modules that may be worth considering.

Here are some of them (quotes are from their project pages):

  • Content locking (anti-concurrent editing)

    Features:

    • Block concurrent editing: When a user is editing a node, any other user that attempts to edit the same node will be blocked from doing so, and notified that the content is already being edited.
    • By node type: This module can be configured to only lock certain content types against concurrent editing.
    • By format: This module can be configured to only lock specific input formats against concurrent editing.
    • Avoid losing content by accident: If a user is editing content and attempts to leave the edit form without first saving it (e.g., closing the browser window/tab, clicking on a link, etc.), they will be notified. If the user confirms that they want to leave without saving the node, the edit lock gets automatically removed by an ajax call. This behavior can be disabled on the modules settings page.
    • Avoid forgotten locks: Users are notified if they forget to unlock a node. They will be prompted to click on a link to unlock the node.
    • Views integration: All open locks can be listed using Views.
  • Conflict.

    This module allows parallel editing of nodes with field-level conflict detection and a trick to prevent accidental overwriting of fields when they are changed by another session while a node is being edited.

    This behavior is useful for things like issue queues where multiple people might be following up to an issue simultaneously. The original problem that lead to this module is #1559578: D7 solution for when multiple users are updating an issue simultaneously.

    It works by doing a 3-way comparison between the field data of the original node, the node as edited by us, and the node as edited by others.

2

You can do it by using the node_validate function.

Your relevant part code is that :

if (isset($node->nid) && (node_last_changed($node->nid) > $node->changed)) {
    form_set_error('changed', t('The content on this page has either been modified by another user, or you have already submitted modifications using this form. As a result, your changes cannot be saved.'));
  }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.