This repository has been archived by the owner on Nov 16, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Override homepage and removes the "home" link from toolbar (#21)
- Loading branch information
Showing
8 changed files
with
150 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,7 @@ distribution: | |
dependencies: | ||
- block | ||
- headless_ui | ||
- json_content | ||
|
||
themes: | ||
- seven | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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']); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
52
modules/json_content/tests/src/Functional/HomepageTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
} | ||
|
||
} |