|
| 1 | +.. index:: |
| 2 | + single: Resource; Description Enhancer |
| 3 | + |
| 4 | +Resource Description Enhancers |
| 5 | +============================== |
| 6 | + |
| 7 | +The resources retrieved from the :doc:`resource repositories <repositories>` |
| 8 | +can be enhanced with descriptions: Bits of information about the resource. This |
| 9 | +is done by so-called *description enhancers*. |
| 10 | + |
| 11 | +Configuring Description Enhancers |
| 12 | +--------------------------------- |
| 13 | + |
| 14 | +In order to use a description enhancers, enable it in your configuration: |
| 15 | + |
| 16 | +.. configuration-block:: |
| 17 | + |
| 18 | + .. code-block:: yaml |
| 19 | +
|
| 20 | + # app/config/config.yml |
| 21 | + cmf_resource: |
| 22 | + description: |
| 23 | + # enables two enhancers |
| 24 | + enhancers: [doctrine_phpcr_odm, your_custom_enhancer] |
| 25 | +
|
| 26 | + repositories: |
| 27 | + # ... |
| 28 | +
|
| 29 | + .. code-block:: xml |
| 30 | +
|
| 31 | + <!-- app/config/config.xml --> |
| 32 | + <?xml version="1.0" encoding="UTF-8" ?> |
| 33 | + <container xmlns="http://symfony.com/schema/dic/services"> |
| 34 | +
|
| 35 | + <config xmlns="http://cmf.symfony.com/schema/dic/resource"> |
| 36 | + <description> |
| 37 | + <!-- enables two enhancers --> |
| 38 | + <enhancer>doctrine_phpcr_odm</enhancer> |
| 39 | + <enhancer>your_custom_enhancer</enhancer> |
| 40 | + </description> |
| 41 | +
|
| 42 | + <!-- ... --> |
| 43 | + </config> |
| 44 | + </container> |
| 45 | +
|
| 46 | + .. code-block:: php |
| 47 | +
|
| 48 | + // app/config/config.yml |
| 49 | + $container->loadFromExtension('cmf_resource', [ |
| 50 | + 'description' => [ |
| 51 | + // enables two enhancers |
| 52 | + 'enhancers' => ['doctrine_phpcr_odm', 'your_custom_enhancer'], |
| 53 | + ], |
| 54 | +
|
| 55 | + 'repositories' => [ |
| 56 | + // ... |
| 57 | + ], |
| 58 | + ]); |
| 59 | +
|
| 60 | +Retrieving the Resource Description |
| 61 | +----------------------------------- |
| 62 | + |
| 63 | +The description for a specific resource can be retrieved using the |
| 64 | +``cmf_resource.description.factory`` service:: |
| 65 | + |
| 66 | + namespace AppBundle\Controller; |
| 67 | + |
| 68 | + use Symfony\Cmf\Component\Resource\Description\Descriptor; |
| 69 | + |
| 70 | + class PageController extends Controller |
| 71 | + { |
| 72 | + public function indexAction() |
| 73 | + { |
| 74 | + $homepageResource = $this->get('cmf_resource.repository.default')->get('/pages/homepage'); |
| 75 | + |
| 76 | + $descriptionFactory = $this->get('cmf_resource.description.factory'); |
| 77 | + $resourceDescription = $descriptionFactory->getPayloadDescriptionFor($homepageResource); |
| 78 | + |
| 79 | + // check if there is a title descriptor |
| 80 | + if ($resourceDescription->has(Descriptor::TITLE)) { |
| 81 | + // get a descriptor (i.e. the title) |
| 82 | + $title = $resourceDescription->get(Descriptor::TITLE); |
| 83 | + } |
| 84 | + |
| 85 | + // get all descriptors |
| 86 | + $descriptors = $resourceDescription->all(); |
| 87 | + |
| 88 | + // ... |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | +Descriptors can contain any type and consist of an identifier and the value. |
| 93 | +Some common identifiers are defined in the ``Descriptor`` class, but any |
| 94 | +descriptor identifier is allowed. |
| 95 | + |
| 96 | +CMF Description Enhancers |
| 97 | +------------------------- |
| 98 | + |
| 99 | +Some CMF bundles ship description enhancers to add specific descriptors used by that bundle: |
| 100 | + |
| 101 | +:doc:`../tree_browser/introduction` |
| 102 | + Ships a ``cmf_tree_icons`` enhancer, which sets an ``icon`` description to |
| 103 | + an icon used in the tree. |
| 104 | + |
| 105 | +:doc:`../sonata_phpcr_admin_integration/introduction` |
| 106 | + Ships a ``sonata_phpcr_admin`` enhancer, which sets edit links to the admin |
| 107 | + dashboard and payload title and type aliases using the related Admin class. |
| 108 | + |
| 109 | +:doc:`introduction` |
| 110 | + Ships a ``doctrine_phpcr_odm`` enhancer, which sets allowed children classes. |
| 111 | + |
| 112 | +:doc:`introduction` |
| 113 | + Ships a ``sylius_resource`` enhancer, adding CRUD links for the SyliusResourceBundle_. |
| 114 | + |
| 115 | +Creating a Custom Enhancer |
| 116 | +-------------------------- |
| 117 | + |
| 118 | +You can create your own enhancer by implementing ``DescriptionEnhancerInterface``:: |
| 119 | + |
| 120 | + // src/AppBundle/Description/PageEnhancer.php |
| 121 | + namespace AppBundle\Description; |
| 122 | + |
| 123 | + use AppBundle\Document\Page; |
| 124 | + use Symfony\Cmf\Component\Resource\Description\Descriptor; |
| 125 | + use Symfony\Cmf\Component\Resource\Description\Description; |
| 126 | + use Symfony\Cmf\Component\Resource\Description\DescriptionEnhancerInterface; |
| 127 | + use Symfony\Cmf\Component\Resource\Puli\Api\PuliResource; |
| 128 | + |
| 129 | + class PageEnhancer implements DescriptionEnhancerInterface |
| 130 | + { |
| 131 | + public function supports(PuliResource $resource) |
| 132 | + { |
| 133 | + // check if the resource is supported by this enhancer (i.e. whether it's an app page). |
| 134 | + return $resource->getPayload() instanceof Page; |
| 135 | + } |
| 136 | + |
| 137 | + public function enhance(Description $description) |
| 138 | + { |
| 139 | + $resource = $description->getResource(); |
| 140 | + |
| 141 | + // set the payload title descriptor to ``Page#getTitle()`` |
| 142 | + $description->set(Descriptor::PAYLOAD_TITLE, $resource->getTitle()); |
| 143 | + } |
| 144 | + } |
| 145 | + |
| 146 | +Then, create a service and tag it with ``cmf_resource.description.enhancer``: |
| 147 | + |
| 148 | +.. configuration-block:: |
| 149 | + |
| 150 | + .. code-block:: yaml |
| 151 | +
|
| 152 | + # app/config/services.yml |
| 153 | + services: |
| 154 | + app.page_enhancer: |
| 155 | + class: AppBundle\Description\PageEnhancer |
| 156 | + tags: |
| 157 | + - { name: cmf_resource.description.enhancer, alias: app_page } |
| 158 | +
|
| 159 | + .. code-block:: xml |
| 160 | +
|
| 161 | + <!-- app/config/services.xml --> |
| 162 | + <?xml version="1.0" encoding="UTF-8" ?> |
| 163 | + <container xmlns="http://symfony.com/schema/dic/services"> |
| 164 | +
|
| 165 | + <services> |
| 166 | + <service id="app.page_enhancer" class="AppBundle\Description\PageEnhancer"> |
| 167 | + <tag name="cmf_resource.description.enhancer" alias="app_page" /> |
| 168 | + </service> |
| 169 | + </services> |
| 170 | + </container> |
| 171 | +
|
| 172 | + .. code-block:: php |
| 173 | +
|
| 174 | + // app/config/services.php |
| 175 | + use AppBundle\Description\PageEnhancer; |
| 176 | +
|
| 177 | + $container->register('app.page_enhancer', PageEnhancer::class) |
| 178 | + ->addTag('cmf_resource.description.enhancer', [ |
| 179 | + 'alias' => 'app_page', |
| 180 | + ]) |
| 181 | + ; |
| 182 | +
|
| 183 | +After this, you can enable your enhancer using it's alias (``app_page``). |
| 184 | + |
| 185 | +.._SyliusResourceBundle: http://docs.sylius.org/en/latest/bundles/SyliusResourceBundle |
0 commit comments