vaersaagod / seomate

SEO, mate! It's important. That's why SEOMate provides the tools you need to craft all the meta tags, sitemaps and JSON-LD microdata you need - in one highly configurable, open and friendly package - with a super-light footprint.
MIT License
36 stars 8 forks source link

Cannot get global value for specific profile #15

Closed piotrpog closed 5 years ago

piotrpog commented 5 years ago

Here is my config:

<?php
return [
    'cacheEnabled' => false,
    'defaultProfile' => 'standard',

    'defaultMeta' => [
            'description' => ['seo.seoFields:settings.globalDescription'],
            'image' => ['seo.seoFields:settings.seoImage'],
    ],

    'fieldProfiles' => [
        'standard' => [
            'title' => ['title'],
        ],
        'home' => [
            'title' => ['seo.seoFields:settings.globalTitle'],
        ]
    ],

    'profileMap' => [
        'home' => 'home',
    ],   

];

This fragment works correctly, description value is taken from global field:

    'defaultMeta' => [
            'description' => ['seo.seoFields:settings.globalDescription'],
            'image' => ['seo.seoFields:settings.seoImage'],
    ],

This fragment does not work. Title shows site name instead:

        'home' => [
            'title' => ['seo.seoFields:settings.globalTitle'],
        ]
mmikkel commented 5 years ago

Hi @piotrpog – sorry about the late response.

The field profiles use the current element (i.e. the entry or category etc. matched to the current URL request) as their field value context, and do not look at Global Sets for field values at all – unlike defaultMeta and additionalMeta, which use the global Twig context and will drill down into Global Sets looking for values.

I don't think it'd make sense to have field profiles look at Global Sets for field values; this would introduce additional overhead and complexity to achieve something that I'd consider an edge case.

To display the seo.seoFields:settings.globalTitle field value for the homepage entry's title attribute, you can either

  1. Add the seo.seoFields:settings.globalTitle field to the defaultMeta's title attribute, and it'll render for any field profile that doesn't have a valid title attribute:
'defaultMeta' => [
    'description' => ['seo.seoFields:settings.globalDescription'],
    'image' => ['seo.seoFields:settings.seoImage'],
    'title' => ['seo.seoFields:settings.globalTitle'],
],
'fieldProfiles' => [
    'home' => [], // Keep it empty to only render values from `defaultMeta`
],
'profileMap' => [
    'home' => 'home',
],
  1. Override the title attribute in Twig (this would go into the relevant Entry template, e.g. templates/homepage.twig) to pull the Global Set's field (or whatever else):
{% extends '_layouts/default' %}

{% set seomate = {
    meta: {
        title: seo.seoFields.type('settings').one().globalTitle
    }
} %}

{% block content %}
...
piotrpog commented 5 years ago

@mmikkel Sorry for the late reply - just wanted to say thanks for explaining it to me.