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

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

bkosborne created an issue. See original summary.

joshahubbers’s picture

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.

bkosborne’s picture

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.

joshahubbers’s picture

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

joshahubbers’s picture

Did you use this in combination with workflows?

bkosborne’s picture

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

joshahubbers’s picture

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...

joshahubbers’s picture

Status: Active » Fixed

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.