Add title and description meta tags (#72)

This commit is contained in:
Alexander Skvortsov 2020-03-06 11:52:30 -05:00 committed by GitHub
parent c2f8aeeecc
commit 6117c1231a
4 changed files with 48 additions and 4 deletions

View File

@ -23,8 +23,14 @@ export default function() {
extend(IndexPage.prototype, 'view', function(vdom) {
const tag = this.currentTag();
if (tag) vdom.attrs.className += ' IndexPage--tag'+tag.id();
});
extend(IndexPage.prototype, 'config', function() {
const tag = this.currentTag();
if (tag) {
vdom.attrs.className += ' IndexPage--tag'+tag.id();
app.setTitle(tag.name());
}
});

View File

@ -84,4 +84,11 @@ export default class TagsPage extends Page {
</div>
);
}
config(...args) {
super.config(...args);
app.setTitle(app.translator.trans('flarum-tags.forum.meta.tags_title'));
app.setTitleCount(0);
}
}

View File

@ -14,6 +14,7 @@ use Flarum\Api\Controller\ListDiscussionsController;
use Flarum\Frontend\Document;
use Flarum\Tags\TagRepository;
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;
@ -35,15 +36,23 @@ class Tag
*/
protected $tags;
/**
* @var Translator
*/
protected $translator;
/**
* @param Client $api
* @param Factory $view
* @param TagRepository $tags
* @param Translator $translator
*/
public function __construct(Client $api, Factory $view, TagRepository $tags)
public function __construct(Client $api, Factory $view, TagRepository $tags, Translator $translator)
{
$this->api = $api;
$this->view = $view;
$this->tags = $tags;
$this->translator = $translator;
}
public function __invoke(Document $document, Request $request)
@ -71,6 +80,12 @@ class Tag
$apiDocument = $this->getApiDocument($actor, $params);
$document->title = $tag->name;
if ($tag->description) {
$document->meta['description'] = $tag->description;
} else {
$document->meta['description'] = $this->translator->trans('flarum-tags.forum.meta.tag_description', ['{tag}' => $tag->name]);
}
$document->content = $this->view->make('tags::frontend.content.tag', compact('apiDocument', 'page', 'tag'));
$document->payload['apiDocument'] = $apiDocument;

View File

@ -14,6 +14,7 @@ use Flarum\Frontend\Document;
use Flarum\Http\UrlGenerator;
use Flarum\Settings\SettingsRepositoryInterface;
use Flarum\Tags\TagRepository;
use Illuminate\Contracts\Translation\Translator;
use Illuminate\Contracts\View\Factory;
use Illuminate\Support\Arr;
use Psr\Http\Message\ServerRequestInterface as Request;
@ -35,6 +36,11 @@ class Tags
*/
protected $tags;
/**
* @var Translator
*/
protected $translator;
/**
* @var SettingsRepositoryInterface
*/
@ -49,15 +55,23 @@ class Tags
* @param Client $api
* @param Factory $view
* @param TagRepository $tags
* @param Translator $translator
* @param SettingsRepositoryInterface $settings
* @param UrlGenerator $url
*/
public function __construct(Client $api, Factory $view, TagRepository $tags, SettingsRepositoryInterface $settings, UrlGenerator $url)
{
public function __construct(
Client $api,
Factory $view,
TagRepository $tags,
Translator $translator,
SettingsRepositoryInterface $settings,
UrlGenerator $url
) {
$this->api = $api;
$this->view = $view;
$this->tags = $tags;
$this->settings = $settings;
$this->translator = $translator;
$this->url = $url;
}
@ -77,6 +91,8 @@ class Tags
];
});
$document->title = $this->translator->trans('flarum-tags.forum.meta.tags_title');
$document->meta['description'] = $this->translator->trans('flarum-tags.forum.meta.tags_description');
$document->content = $this->view->make('tags::frontend.content.tags', compact('primaryTags', 'secondaryTags', 'children'));
$document->canonicalUrl = $defaultRoute === '/tags' ? $this->url->to('forum')->base() : $request->getUri()->withQuery('');