$this->document is set when no document is called directly

Hi,

I’m not sure if this is intended or a bug.

I have a document on /fluege/weltweit/
but when i call /fluege/weltweit/at.oesterreich (based on a static route)
that document is also loaded into $this->document

Is this working as intended? Any way to disable this?

Best greetings,

Christian

1 Like

That is indeed intended, and I have no idea if it possible to change that :D, would need to check the code for that.

Well it makes sense in some cases and in some it doesn’t. How do you set your Meta Descriptions / Titles for “Object”-Pages / Static Routes?

1 Like
1 Like

Or simply add the two lines at the top of your view template:

{% do document.setTitle('My Title for a Static Route') %}
{% do document.setDescription('My Description for a Static Route') %}

Overwriting attributes in a object that, pretty much, has nothing todo with the current request, seems not like the right way.

@dpfaffenbauer That looks nice! Thanks!

1 Like

I agree with @cmaerz that it is “not clean”.

I tried to use @aarongerig’s approach but it didn’t work since the static route is “inside” a folder, not a page document, Pimcore gives error:

Error: Call to undefined method setTitle in class Pimcore\Model\Document\Folder

I think there are two alternatives:

  1. Make sure a route is always inside page document, not folder.
  2. Tweak the layout.html.twig so it checks for Document instance. Else use our own $this->metaTitle and $this->metaDescription variables.

Currently I opt for approach 2, in app/Resources/views/layout.html.twig:

{% if document is instanceof('\\Pimcore\\Model\\Document\\Page') %}
    {% if document.getTitle() is not empty %}
        {% do pimcore_head_title().set(document.getTitle()) %}
    {% endif %}

    {% if document.getDescription() is not empty %}
        {% do pimcore_head_meta().setDescription(document.getDescription()) %}
    {% endif %}
{% endif %}
{% if metaTitle is defined and metaTitle is not empty %}
    {% do pimcore_head_title().set(metaTitle) %}
{% endif %}
{% if metaDescription is defined and metaDescription is not empty %}
    {% do pimcore_head_meta().setDescription(metaDescription) %}
{% endif %}

and in controller:

$translator = $this->get("translator");
$this->view->metaTitle = $translator->trans('lovia.edu.preschool.loc.title',
    ['{{place}}' => $populatedPlace->getName()]);