Skip to content
Snippets Groups Projects
Commit fe3301f0 authored by Josha Hubbers's avatar Josha Hubbers
Browse files

Merge branch '8.x-1.x-dev' into 8.x-1.x

parents 3764475e 2c8ff097
No related branches found
No related tags found
No related merge requests found
CONTENTS OF THIS FILE
---------------------
* Introduction
* Requirements
* Installation
......@@ -11,7 +11,7 @@ INTRODUCTION
------------
Prevent homepage deletion
This module provides a new permission: delete homepage node.
This module provides a new permission: delete_homepage_node.
Only users with this permission can delete the node that is currently configured
as the home page of the site. Other users will not see the delete-tab, nor the
delete option in the content overview.
......@@ -25,9 +25,9 @@ No
INSTALLATION
------------
- Install with composer
- Install with composer
$ composer require 'drupal/prevent_homepage_deletion:^1.0'
- Give someone the permission to delete the homepage, or don't if nobody should
- Give someone the permission to delete the homepage, or don't if nobody should
delete it ;-).
CONFIGURATION
......
{
"name": "drupal/prevent_homepage_deletion",
"description": "This module adds a permission 'delete homepage node'.",
"description": "This module adds a permission 'delete_homepage_node'.",
"type": "drupal-module",
"keywords": ["Drupal", "prevent_homepage_deletion"],
"homepage": "https://www.drupal.org/project/prevent_homepage_deletion",
......
......@@ -20,9 +20,9 @@ function prevent_homepage_deletion_help($route_name, RouteMatchInterface $route_
case 'help.page.prevent_homepage_deletion':
$output = '';
$output .= '<h3>' . t('About') . '</h3>';
$output .= '<p>' . t("This module provides a new permission: delete
homepage node. Only users with this permission can delete the node that
is currently configured as the home page of the site. Other users will
$output .= '<p>' . t("This module provides a new permission: delete
homepage node. Only users with this permission can delete the node that
is currently configured as the home page of the site. Other users will
not see the delete-tab, nor the delete option in the content overview.")
. '</p>';
return $output;
......@@ -46,7 +46,12 @@ function prevent_homepage_deletion_node_access(EntityInterface $entity, $operati
}
/**
* Implements _prevent_homepage_deletion_check().
* Helper function that checks if the current user can delete this node.
*
* @param \Drupal\Core\Entity\EntityInterface $entity
* @param \Drupal\Core\Session\AccountInterface $account
*
* @return bool
*/
function _prevent_homepage_deletion_check(EntityInterface $entity, AccountInterface $account) {
$candelete = TRUE;
......@@ -65,7 +70,7 @@ function _prevent_homepage_deletion_check(EntityInterface $entity, AccountInterf
if (\Drupal::service('path.matcher')->isFrontPage() || $entity_id === $frontpage_id) {
// Check for permission.
if ($account->hasPermission('delete homepage node')) {
if ($account->hasPermission('delete_homepage_node')) {
$candelete = TRUE;
}
else {
......
delete homepage node:
delete_homepage_node:
title: 'Delete homepage node'
description: 'Prevent users from deleting a homepage node.'
restrict access: false
<?php
namespace Drupal\Tests\prevent_homepage_deletion\Functional;
use Drupal\Tests\BrowserTestBase;
/**
* Tests the functionality of this module.
*
* @group prevent_homepage_deletion
*
*/
class DeleteHomepageTest extends BrowserTestBase {
/**
* {@inheritdoc}
*/
public static $modules = ['node', 'user', 'prevent_homepage_deletion'];
/**
* {@inheritdoc}
*/
protected $defaultTheme = 'stark';
/**
* Our node type.
*
* @var \Drupal\node\Entity\NodeType
*/
protected $contentType;
/**
* Our two nodes.
* @var \Drupal\node\NodeInterface
*/
protected $page_home;
protected $page_not_home;
/**
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp();
// Add a content type.
$this->contentType = $this->createContentType(['type' => 'page']);
// Create a first page (homepage).
$this->page_home = $this->drupalCreateNode(['type' => 'page']);
// Set page to be homepage.
\Drupal::configFactory()
->getEditable('system.site')
->set('page.front', '/node/'.$this->page_home->id())
->save(TRUE);
// Create a second page (not homepage).
$this->page_not_home = $this->drupalCreateNode(['type' => 'page']);
}
/**
* Test to check if the homepage can be deleted by various users.
*/
public function testDeleteHomepage() {
// Step 1: Log in a user who can delete the homepage.
$this->drupalLogin(
$this->createUser([
'delete any page content',
'delete_homepage_node',
])
);
// Step 2: Try to delete the homepage.
$this->drupalGet('node/' . $this->page_home->id() . '/delete');
$this->assertSession()->statusCodeEquals(200);
// Step 3: Logout, and login as user without the permission.
$this->drupalLogout();
$this->drupalLogin(
$this->createUser([
'delete any page content',
])
);
// Step 4: Try to delete the homepage.
$this->drupalGet('node/' . $this->page_home->id() . '/delete');
$this->assertSession()->statusCodeEquals(403);
// Step 5: Try to delete the non-homepage.
$this->drupalGet('node/' . $this->page_not_home->id() . '/delete');
$this->assertSession()->statusCodeEquals(200);
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment