Skip to content

Commit

Permalink
Merge pull request #5 from DorsetDigital/dev-env
Browse files Browse the repository at this point in the history
Add operation via environment variable
  • Loading branch information
thomasbnielsen authored Sep 25, 2020
2 parents 1a290bf + 75c9864 commit 0bfb3eb
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 10 deletions.
20 changes: 18 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,18 @@
Block site from being indexed when site is en dev or test mode.
Shows a warning in the CMS.
This module adds a robots metatag to discourage well-behaved search engine spiders from crawling and indexing your site.

###### Installation
Install via composer: `composer require nobrainerweb/silverstripe-robots-noindex`

Don't forget to run a `dev/build?flush`



The module will automatically add the tag when your site is in dev or test mode. It can also be activated regardless of the site mode with the addition of an environment variable called 'SEO_PREVENT_INDEXING' which can be set to true.

For example, if you are using a .env file, you would add the following line to enable the module:

`SEO_PREVENT_INDEXING = true`

A warning is shown in the CMS when the module is active and indexing is discouraged.


6 changes: 5 additions & 1 deletion lang/en.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
en:
NobrainerWeb\NoIndex\NoIndexExtension:
NO_INDEX_WARNING: 'Warning: No indexing! This website is running in development mode, and is not being indexed by search engines'
NO_INDEX_WARNING: 'Warning: No indexing!'
NO_INDEX_WARNING_DEV: 'This website is running in development mode, and is not being indexed by search engines'
NO_INDEX_WARNING_TEST: 'This website is running in test mode, and is not being indexed by search engines'
NO_INDEX_WARNING_ENV: 'Search engine indexing is disabled by an environment setting'

41 changes: 34 additions & 7 deletions src/NoIndexExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace NobrainerWeb\NoIndex;

use SilverStripe\Control\Director;
use SilverStripe\Core\Environment;
use SilverStripe\Core\Extension;
use SilverStripe\Forms\LiteralField;

Expand All @@ -11,7 +12,7 @@ class NoIndexExtension extends Extension

public function MetaTags(&$tags)
{
if (Director::isDev() || Director::isTest()) {
if ($this->isActive()) {
$tags .= '<meta name="robots" content="noindex, nofollow" />';
}

Expand All @@ -20,17 +21,43 @@ public function MetaTags(&$tags)

public function updateCMSFields($fields)
{
if (Director::isLive()) {
if (! $this->isActive()) {
return;
}
$fields->unshift(LiteralField::create(
'NoIndexWarningHeader',
'<div class="alert alert-warning">' . _t(
self::class . '.NO_INDEX_WARNING',
"Warning: No indexing! This website is running in development mode, and is not being indexed by search engines"
)
. '</div>'
'<div class="alert alert-warning">' . $this->getWarningMessage() . '</div>'
));
}

/**
* Returns a warning message based on the current configuration of the site
* @return string
*/
private function getWarningMessage() {
$message = _t(self::class . '.NO_INDEX_WARNING', 'Warning: No indexing!');

if (Director::isDev()) {
$message .= '<br>'. _t(self::class . '.NO_INDEX_WARNING_DEV', 'This website is running in development mode, and is not being indexed by search engines');
return $message;
}

if (Director::isTest()) {
$message .= '<br>'. _t(self::class . '.NO_INDEX_WARNING_TEST', 'This website is running in test mode, and is not being indexed by search engines');
}

if (Environment::getEnv('SEO_PREVENT_INDEXING') === true) {
$message .= '<br>'. _t(self::class . '.NO_INDEX_WARNING_ENV', 'Search engine indexing is disabled by an environment setting');
}

return $message;
}

/**
* Decides whether the module should be enabled
* @return bool
*/
private function isActive() {
return (Director::isDev() || Director::isTest() || (Environment::getEnv('SEO_PREVENT_INDEXING') === true));
}
}

0 comments on commit 0bfb3eb

Please sign in to comment.