Skip to content
This repository has been archived by the owner on Nov 16, 2021. It is now read-only.

Commit

Permalink
Override homepage and removes the "home" link from toolbar (#21)
Browse files Browse the repository at this point in the history
  • Loading branch information
balsama authored and Adam committed Aug 4, 2017
1 parent 80b5175 commit d8aef90
Show file tree
Hide file tree
Showing 8 changed files with 150 additions and 0 deletions.
1 change: 1 addition & 0 deletions headless_lightning.info.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ distribution:
dependencies:
- block
- headless_ui
- json_content

themes:
- seven
Expand Down
23 changes: 23 additions & 0 deletions headless_lightning.install
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

/**
* @file
* Install and uninstall functions for the JSON Content module.
*/

/**
* Implements hook_install().
*/
function headless_lightning_install() {
// Don't do anything else during config sync.
if (\Drupal::isConfigSyncing()) {
return;
}

if (\Drupal::moduleHandler()->moduleExists('json_content')) {
\Drupal::configFactory()
->getEditable('system.site')
->set('page.front', '/frontpage')
->save();
}
}
4 changes: 4 additions & 0 deletions modules/json_content/json_content.info.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
name: 'JSON Content'
type: module
core: 8.x
description: 'Presentation layer blanket for content that is only consumed via an API.'
10 changes: 10 additions & 0 deletions modules/json_content/json_content.module
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

/**
* Implements hook_toolbar().
*/
function json_content_toolbar_alter(array &$items) {
// The front page redirects to admin/content and content is generally not
// viewed outside of the API so this link makes no sense.
unset($items['home']);
}
7 changes: 7 additions & 0 deletions modules/json_content/json_content.routing.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
d8contrib.frontpage:
path: '/frontpage'
defaults:
_title: 'Login'
_controller: '\Drupal\json_content\Controller\FrontController::frontpage'
requirements:
_access: 'TRUE'
35 changes: 35 additions & 0 deletions modules/json_content/src/Controller/FrontController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace Drupal\json_content\Controller;

use Drupal\Core\Controller\ControllerBase;
use Drupal\json_content\Form\UserLoginForm;

class FrontController extends ControllerBase {

/**
* Displays the login form on the homepage and redirects authenticated users.
*/
public function frontpage() {
$build = [];
if ($this->currentUser()->isAnonymous()) {
$build['heading'] = [
'#type' => 'markup',
'#markup' => $this->t('Please log in for access to the content repository.'),
];
$build['form'] = $this->formBuilder()->getForm(UserLoginForm::class);
}
else {
if ($this->currentUser()->hasPermission('access content overview')) {
// Permitted users are directed to the admin content page.
return $this->redirect('view.content.page_1');
}
$build['heading'] = [
'#type' => 'markup',
'#markup' => $this->t('This site has no homepage content.'),
];
}
return $build;
}

}
18 changes: 18 additions & 0 deletions modules/json_content/src/Form/UserLoginForm.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace Drupal\json_content\Form;

use Drupal\user\Form\UserLoginForm as BaseUserLoginForm;
use Drupal\Core\Form\FormStateInterface;

class UserLoginForm extends BaseUserLoginForm {

/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
parent::submitForm($form, $form_state);
$form_state->setRedirect('<front>');
}

}
52 changes: 52 additions & 0 deletions modules/json_content/tests/src/Functional/HomepageTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

namespace Drupal\Tests\headless_ui\Functional;

use Drupal\Tests\BrowserTestBase;

/**
* @group headless
* @group json_content
*/
class HomepageTest extends BrowserTestBase {

/**
* {@inheritdoc}
*/
protected $profile = 'lightning_headless';

public function test() {
$assert = $this->assertSession();

// Anon users see login form on homepage
$this->drupalGet('<front>');
$assert->statusCodeEquals(200);
$form = $assert->elementExists('css', '#user-login-form');
$assert->fieldExists('Username', $form);
$assert->fieldExists('Password', $form);

// Authenticated users without the "access content overview" permission are
// not redirected from the homepage.
$account = $this->drupalCreateUser();
$this->drupalLogin($account);
$this->drupalGet('<front>');
$assert->pageTextContains('This site has no homepage content');
$assert->addressEquals('/');

// Authenticated users with the "access content overview" permission are
// redirected to the /admin/content page.
$account = $this->drupalCreateUser(['access content overview']);
$this->drupalLogin($account);
$this->drupalGet('<front>');
$assert->addressEquals('/admin/content');

// The "Back to site" link does not appear in the toolbar when on an admin
// page.
$account = $this->drupalCreateUser([], NULL, TRUE);
$this->drupalLogin($account);
$this->drupalGet('/admin');
$assert->elementExists('css', 'nav#toolbar-bar');
$assert->linkNotExists('Back to site');
}

}

0 comments on commit d8aef90

Please sign in to comment.