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

Support meta_title overrides from page model #58

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,8 @@ Sometimes, you want special behavior for certain templates. The easiest way to a
| Key | Type | Description |
|:----|:-----|:------------|
| `meta_description` | `string` | Provide a default description that is used, when the user had not entered a dedicated description for this page. This could e.g. be a truncated version of the page's text content. |
| `meta_title` | `string` | Provide a default override page title that is used, when the user has not entered a title override for this page. This could be used to provide an autogenrated title. |
| `meta_title_full` | `string` | Provide a page title override that does not append the title separate and site title. This exact title is used for the page. If this is passed, no other field is checked by this plugin. But your page model can check the plugin's fields like `meta_title`. |
| `og_title_prefix` | `string` | Will be put in front of the page's OpenGraph title, e.g. `'ℹ️ '` or `'[Recipe ]` |
| `og_image File` | `Kirby\Cms\File` | A `File` object, that sets the default OpenGraph image for this page. You can even generate custom images programatically and Wrap them in a `File` object, e.g. for the docs of your product (getkirby.com does this for the reference pages).
| `@graph` | `array` | Things to add to the JSON-LD metadata in the page's head. If you need to reference the organization or person behind the website, use `url('/#owner')`. If you need to reference the website itself, use `url('/#website')`. |
Expand Down
16 changes: 9 additions & 7 deletions src/PageMeta.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace FabianMichael\Meta;

use Kirby\Cms\App as Kirby;
use Kirby\Cms\Field;
use Kirby\Content\Field;
use Kirby\Cms\File;
use Kirby\Cms\Language;
use Kirby\Cms\Page;
Expand Down Expand Up @@ -75,6 +75,7 @@ public function get(
bool $configFallback = false,
mixed $fallback = null
): Field {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@srijan Please add a "to do" comment, so I can add the type check again for later.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've replaced the Field namespace in the use section instead. This, I think, makes it incompatible with v3.


// From content file ...
$field = $this->page->content($this->languageCode)->get($key);

Expand Down Expand Up @@ -421,14 +422,15 @@ public function title(): Field
$title = [];
$siteTitle = $this->page->site()->title();

if ($this->page->isHomePage() === true) {
$title[] = $this->page->content($this->languageCode)->get('meta_title')
->or($siteTitle)->toString();
if ($metaTitleFull = $this->metadata('meta_title_full')) {
$title[] = $metaTitleFull;
} else if ($this->page->isHomePage() === true) {
$title[] = $this->get(key: 'meta_title',
fallback: $siteTitle)->toString();
} else {
// TODO: Support pagination
$title[] = $this->page->content($this->languageCode)->get('meta_title')
->or($this->page->title())->toString();

$title[] = $this->get(key: 'meta_title',
fallback: $this->page->title())->toString();
$title[] = SiteMeta::titleSeparator();
$title[] = $siteTitle->toString();
}
Expand Down