Skip to content

Create custom view matcher

In addition to the built-in view matchers, you can also create custom matchers to use in template configuration.

To do it, create a matcher class that extends eZ\Publish\Core\MVC\Symfony\Matcher\ContentBased\MultipleValued.

Matcher class

The matcher class must implement the following methods:

  • matchLocation - checks if a Location object matches.
  • matchContentInfo - checks if a ContentInfo object matches.
  • match - checks if the View object matches.

The following example shows how to implement an Owner matcher. This matcher identifies Content items that have the provided owner or owners.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
<?php declare(strict_types=1);

namespace App\View\Matcher;

use eZ\Publish\API\Repository\Values\Content\ContentInfo;
use eZ\Publish\API\Repository\Values\Content\Location;
use eZ\Publish\Core\MVC\Symfony\Matcher\ContentBased\MultipleValued;
use eZ\Publish\Core\MVC\Symfony\View\ContentValueView;
use eZ\Publish\Core\MVC\Symfony\View\LocationValueView;
use eZ\Publish\Core\MVC\Symfony\View\View;

class Owner extends MultipleValued
{
    public function matchLocation(Location $location)
    {
        return $this->hasOwner($location->getContentInfo());
    }

    public function matchContentInfo(ContentInfo $contentInfo)
    {
        return $this->hasOwner($contentInfo);
    }

    public function match(View $view): ?bool
    {
        $location = null;

        if ($view instanceof LocationValueView) {
            return $this->matchLocation($view->getLocation());
        }

        if ($view instanceof ContentValueView) {
            return $this->matchContentInfo($view->getContent()->contentInfo);
        }

        return false;
    }

    private function hasOwner(ContentInfo $contentInfo): bool
    {
        $owner = $this->getRepository()->getUserService()->loadUser($contentInfo->ownerId);

        if (\array_key_exists($owner->login, $this->values)) {
            return true;
        }

        return false;
    }
}

The matcher checks whether the owner of the current content (by its ContentInfo or Location) matches any of the values passed in configuration (line 48).

View configuration

To apply the matcher in view configuration, indicate the matcher by its fully qualified class name, preceded by \.

The following configuration uses a special template to render articles owned by the users with provided logins:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
ezplatform:
    system:
        site_group:
            content_view:
                full:
                    editor_articles:
                        template: '@ezdesign/full/featured_article.html.twig'
                        match:
                            Identifier\ContentType: article
                            \App\View\Matcher\Owner: [johndoe, janedoe]

Note

If you use a matcher that is a service instead of a simple class, tag the service with ezplatform.view.matcher.