Skip to content

Commit

Permalink
Add index page title, add mechanism to clear title from defaultRoute. (
Browse files Browse the repository at this point in the history
…#2047)

* Add "All Descriptions title to index

* Added system to clear custom title if we're on the default route
  • Loading branch information
askvortsov1 committed Jun 27, 2020
1 parent 0c645a6 commit a33fbbf
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 6 deletions.
5 changes: 4 additions & 1 deletion js/src/common/Application.js
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,10 @@ export default class Application {
}

updateTitle() {
document.title = (this.titleCount ? `(${this.titleCount}) ` : '') + (this.title ? this.title + ' - ' : '') + this.forum.attribute('title');
const count = this.titleCount ? `(${this.titleCount}) ` : '';
const pageTitleWithSeparator = this.title && m.route() !== '/' ? this.title + ' - ' : '';
const title = this.forum.attribute('title');
document.title = count + pageTitleWithSeparator + title;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion js/src/forum/components/IndexPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ export default class IndexPage extends Page {

extend(context, 'onunload', () => $('#app').css('min-height', ''));

app.setTitle('');
app.setTitle(app.translator.trans('core.forum.index.meta_title_text'));
app.setTitleCount(0);

// Work out the difference between the height of this hero and that of the
Expand Down
6 changes: 5 additions & 1 deletion src/Forum/Content/Index.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Flarum\Http\UrlGenerator;
use Flarum\Settings\SettingsRepositoryInterface;
use Flarum\User\User;
use Illuminate\Contracts\Translation\Translator;
use Illuminate\Contracts\View\Factory;
use Illuminate\Support\Arr;
use Psr\Http\Message\ServerRequestInterface as Request;
Expand Down Expand Up @@ -46,13 +47,15 @@ class Index
* @param Factory $view
* @param SettingsRepositoryInterface $settings
* @param UrlGenerator $url
* @param Translator $translator
*/
public function __construct(Client $api, Factory $view, SettingsRepositoryInterface $settings, UrlGenerator $url)
public function __construct(Client $api, Factory $view, SettingsRepositoryInterface $settings, UrlGenerator $url, Translator $translator)
{
$this->api = $api;
$this->view = $view;
$this->settings = $settings;
$this->url = $url;
$this->translator = $translator;
}

public function __invoke(Document $document, Request $request)
Expand All @@ -74,6 +77,7 @@ public function __invoke(Document $document, Request $request)
$apiDocument = $this->getApiDocument($request->getAttribute('actor'), $params);
$defaultRoute = $this->settings->get('default_route');

$document->title = $this->translator->trans('core.forum.index.meta_title_text');
$document->content = $this->view->make('flarum.forum::frontend.content.index', compact('apiDocument', 'page'));
$document->payload['apiDocument'] = $apiDocument;
$document->canonicalUrl = $defaultRoute === '/all' ? $this->url->to('forum')->base() : $request->getUri()->withQuery('');
Expand Down
13 changes: 11 additions & 2 deletions src/Frontend/Document.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Illuminate\Contracts\View\Factory;
use Illuminate\Contracts\View\View;
use Illuminate\Support\Arr;
use Psr\Http\Message\ServerRequestInterface as Request;

/**
* A view which renders a HTML skeleton for Flarum's frontend app.
Expand Down Expand Up @@ -131,14 +132,20 @@ class Document implements Renderable
*/
protected $forumApiDocument;

/**
* @var Request
*/
protected $request;

/**
* @param Factory $view
* @param array $forumApiDocument
*/
public function __construct(Factory $view, array $forumApiDocument)
public function __construct(Factory $view, array $forumApiDocument, Request $request)
{
$this->view = $view;
$this->forumApiDocument = $forumApiDocument;
$this->request = $request;
}

/**
Expand Down Expand Up @@ -173,7 +180,9 @@ protected function makeView(): View
*/
protected function makeTitle(): string
{
return ($this->title ? $this->title.' - ' : '').Arr::get($this->forumApiDocument, 'data.attributes.title');
$onHomePage = rtrim($this->request->getUri()->getPath(), '/') === '';

return ($this->title && ! $onHomePage ? $this->title.' - ' : '').Arr::get($this->forumApiDocument, 'data.attributes.title');
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Frontend/Frontend.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public function document(Request $request): Document
{
$forumDocument = $this->getForumDocument($request);

$document = new Document($this->view, $forumDocument);
$document = new Document($this->view, $forumDocument, $request);

$this->populate($document, $request);

Expand Down

0 comments on commit a33fbbf

Please sign in to comment.