Prevent unpublishing homepage

Created on 27 September 2023, 9 months ago
Updated 27 June 2024, about 24 hours ago

Problem/Motivation

We often run into issues where users delete their homepage, or they unpublish it. Unpublishing typically takes longer to notice, because it's only anonymous visitors that would notice the effect as they are met with an access denied message when trying to view the unpublished homepage.

Steps to reproduce

Proposed resolution

This is trickier than preventing a node from being deleted. One option is to add form validation to the node form and the views bulk operation form that's used on the main content dashboard for bulk unpublishing, but of course there are other ways a node could be unpublished.

I wonder if a central way to do this would be with an entity validator β†’ .

Remaining tasks

User interface changes

API changes

Data model changes

✨ Feature request
Status

Fixed

Version

2.0

Component

Code

Created by

πŸ‡ΊπŸ‡ΈUnited States bkosborne New Jersey, USA

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

Comments & Activities

  • Issue created by @bkosborne
  • πŸ‡³πŸ‡±Netherlands JoshaHubbers

    This is a tricky one indeed. It is not very hard to intercept the form and prevent the published setting being set to false, but how will this behave in automated workflows? I have to think about this a little.

  • πŸ‡ΊπŸ‡ΈUnited States bkosborne New Jersey, USA

    We ended up doing this:

    
    /**
     * Implements hook_entity_field_access().
     */
    function HOOK_entity_field_access($operation, FieldDefinitionInterface $field_definition, AccountInterface $account, FieldItemListInterface $items = NULL) {
      $accessResult = AccessResult::neutral();
      if ($operation === "edit" && $items) {
        $entity = $items->getEntity();
        // Prevent users from unpublishing a node that's set their homepage.
        if ($field_definition->getName() === 'status' && $entity instanceof NodeInterface && $items->getEntity()->status->value) {
          $isHomePage = /* function call to check if the entity is the homepage */
          $accessResult = AccessResult::forbiddenIf($isHomePage, 'The homepage may not be unpublished.');
        }
    
      return $accessResult;
    }
    

    This removes the ability to unpublish the page from the UI, like when editing a node (the publish checkbox is removed) or using bulk actions on the main content list view.

  • πŸ‡³πŸ‡±Netherlands JoshaHubbers

    Hm, nice, I will have a look at inserting it in the module and write some tests for it.

  • πŸ‡³πŸ‡±Netherlands JoshaHubbers

    Did you use this in combination with workflows?

  • πŸ‡ΊπŸ‡ΈUnited States bkosborne New Jersey, USA

    I haven't tested it with workflows, but I think it wouldn't work with content moderation.

  • πŸ‡³πŸ‡±Netherlands JoshaHubbers

    Hm, I did add your suggestion to the module, but accidently committed it in the dev branch in stead of in the issue fork. If you want to test the implementation in the dev-branch, it would be great...

  • Status changed to Fixed about 24 hours ago
Production build 0.69.0 2024