Module only supports "page" content type – generalise form alter hook to support all Mercury Editor enabled node types

Created on 12 May 2025, 4 days ago

Problem/Motivation

The mercury_editor_content_lock module currently hardcodes its hook_form_FORM_ID_alter() implementation to work only with the Page content type:

function mercury_editor_content_lock_form_node_page_mercury_editor_form_alter() { ... }

As a result, nodes of other content types using the Mercury Editor do not benefit from content locking.

Steps to reproduce

  1. Enable the Mercury Editor and Content Lock modules.
  2. Assign a different content type (e.g., Article) to use the Mercury Editor.
  3. Attempt to edit this node while another user also has it open.
  4. No content lock is triggered or enforced for non-Page types.

Proposed resolution

Generalise the hook_form_FORM_ID_alter() to apply to all node types that use the Mercury Editor, rather than just the hardcoded node_page_mercury_editor_form.

For example, use:

function mercury_editor_content_lock_form_alter(&$form, FormStateInterface $form_state, $form_id) {
  if (!str_starts_with($form_id, 'node_') || !str_ends_with($form_id, '_mercury_editor_form')) {
    return;
  }

  // Rest of mercury_editor_content_lock_form_node_page_mercury_editor_form_alter() function.
}

or leverage the `mercury_editor.context` service to confirm the form is in the editor context:

function mercury_editor_content_lock_form_alter(&$form, FormStateInterface $form_state, $form_id) {
  $context = \Drupal::service('mercury_editor.context');
  if (!$context->isEditor()) {
    return;
  }
  // Rest of mercury_editor_content_lock_form_node_page_mercury_editor_form_alter() function.
}

Side note / related discovery:

When calling `mercury_editor_content_lock_form_node_page_mercury_editor_form_alter() `programmatically from a custom module for non-Page content types, the lock modal appears. However, this modal does not show when using the original hardcoded form alter on Page content types, suggesting a potential secondary bug or inconsistency.

🐛 Bug report
Status

Active

Version

1.0

Component

Code

Created by

🇬🇧United Kingdom DrupalGideon London

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Production build 0.71.5 2024