Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add index page title, add mechanism to clear title from defaultRoute. #2047

Merged
merged 11 commits into from
Jun 27, 2020
3 changes: 2 additions & 1 deletion js/src/common/Application.js
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,8 @@ export default class Application {
}

updateTitle() {
document.title = (this.titleCount ? `(${this.titleCount}) ` : '') + (this.title ? this.title + ' - ' : '') + this.forum.attribute('title');
document.title =
(this.titleCount ? `(${this.titleCount}) ` : '') + (this.title && m.route() !== '/' ? this.title + ' - ' : '') + this.forum.attribute('title');
askvortsov1 marked this conversation as resolved.
Show resolved Hide resolved
}

/**
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 @@ -95,7 +95,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)
dsevillamartin marked this conversation as resolved.
Show resolved Hide resolved
{
$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