Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

add transformer documentation

  • Loading branch information...
commit d0f068cb38dcd1339924622789321c04d0564cf0 1 parent 46a13cf
Trent Steel authored

Showing 1 changed file with 74 additions and 0 deletions. Show diff stats Hide diff stats

  1. +74 0 Resources/doc/transformers.md
74 Resources/doc/transformers.md
Source Rendered
... ... @@ -0,0 +1,74 @@
  1 +## Creating your own transformers
  2 +
  3 +Transformers are used to change the html data before it is saved.
  4 +
  5 +For an example transformer see [Transformer/StripJS.php](Transformer/StripJS.php)
  6 +
  7 +### Step 1: Create your transformer
  8 +
  9 +src/AcmeDemoBundle/Transformer/RemoveImages.php
  10 +
  11 +```php
  12 +<?php
  13 +
  14 +namespace Acme\DemoBundle\Transformer;
  15 +
  16 +use Symfony\Component\Form\DataTransformerInterface;
  17 +
  18 +class RemoveImages implements DataTransformerInterface
  19 +{
  20 + /**
  21 + * {@inheritDoc}
  22 + */
  23 + public function transform($data)
  24 + {
  25 + return $data;
  26 + }
  27 +
  28 + /**
  29 + * {@inheritDoc}
  30 + */
  31 + public function reverseTransform($data)
  32 + {
  33 + return preg_replace('/<img[^>]+\>/is', '', $data);
  34 + }
  35 +}
  36 +```
  37 +
  38 +### Step 2: Tag your transformer so it can be detected.
  39 +
  40 +src/AcmeDemoBundle/config/services.yml
  41 +
  42 +```yaml
  43 + acme_demo_ckeditor.transformer.strip_comments:
  44 + class: Acme\DemoBundle\Transformer\RemoveImages
  45 + tags:
  46 + - { name: trsteel_ckeditor.transformer, alias: remove_images }
  47 +```
  48 +
  49 +Note: The alias must be unique.
  50 +
  51 +### Step 3: Enable your custom transformer
  52 +
  53 +Enable the transformer globally in config.yml
  54 +
  55 +```yaml
  56 +trsteel_ckeditor:
  57 + transformers: ['remove_images']
  58 +```
  59 +
  60 +Or enable it on a specific form
  61 +
  62 +```php
  63 +<?php
  64 +
  65 +$form = $this->createFormBuilder($post)
  66 + ->add('title', 'text')
  67 + ->add('content', 'ckeditor', array(
  68 + 'transformers' => array('remove_images'),
  69 + ))
  70 + ->getForm()
  71 +;
  72 +```
  73 +
  74 +Note: If you override the transformers value none of the default transformers will be including (eg strip_js, strip_css etc)

0 comments on commit d0f068c

Please sign in to comment.
Something went wrong with that request. Please try again.