Important

You are browsing the documentation for version 4.1 of OroCommerce, OroCRM and OroPlatform, which is no longer maintained. Read version 5.1 (the latest LTS version) of the Oro documentation to get up-to-date information.

See our Release Process documentation for more information on the currently supported and upcoming releases.

How to Use the Draft Filter

To restrict access to draft entities on pages, use a query modification mechanism that is implemented in Doctrine Filters.

Follow the instructions provided in the Doctrine Filters topic for more details.

The system already has the DraftableFilterManager auxiliary manager that can be used to turn on/off the draft filter.

This example describes how to disable the draft filter. The following validator checks both unique and draft entities.

 1<?php
 2
 3namespace ACME\Bundle\CMSBundle\Validator\Constraints;
 4
 5use ACME\Bundle\CMSBundle\Entity\Block;
 6use Doctrine\ORM\EntityManager;
 7use Doctrine\Persistence\ManagerRegistry;
 8use Oro\Bundle\DraftBundle\Manager\DraftableFilterManager;
 9use Symfony\Component\Validator\Constraint;
10use Symfony\Component\Validator\ConstraintValidator;
11use Symfony\Contracts\Translation\TranslatorInterface;
12
13/**
14 * Checks if at least one record exists (includes draft entities).
15 */
16class UniqueTitleDraftValidator extends ConstraintValidator
17{
18    /**
19     * @var ManagerRegistry
20     */
21    private $registry;
22
23    /**
24     * @var DraftableFilterManager
25     */
26    private $filterManager;
27
28    /**
29     * @var TranslatorInterface
30     */
31    private $translator;
32
33    /**
34     * @param ManagerRegistry $registry
35     * @param DraftableFilterManager $filterManager
36     * @param TranslatorInterface $translator
37     */
38    public function __construct(
39        ManagerRegistry $registry,
40        DraftableFilterManager $filterManager,
41        TranslatorInterface $translator
42    ) {
43        $this->registry = $registry;
44        $this->filterManager = $filterManager;
45        $this->translator = $translator;
46    }
47
48    /**
49     * @param string|null $value
50     * @param UniqueTitleDraft|Constraint $constraint
51     */
52    public function validate($value, Constraint $constraint): void
53    {
54        /** @var EntityManager $entityManager */
55        $entityManager = $this->registry->getManagerForClass(Block::class);
56        $this->filterManager->disable(Block::class);
57        $result = $entityManager
58            ->getRepository(Block::class)
59            ->findBy(['title' => $value]);
60
61        $countResult = count($result);
62
63        $this->filterManager->enable(Block::class);
64
65        if (0 === $countResult || (1 === $countResult && $this->context->getObject() === current($result))) {
66            return;
67        }
68
69        $message = $this->translator->trans($constraint->message);
70        $this->context->buildViolation($message)->addViolation();
71    }
72}