Message-ID: <1515316194.3778.1485855297302.JavaMail.confluence@ip-10-127-227-164> Subject: Exported From Confluence MIME-Version: 1.0 Content-Type: multipart/related; boundary="----=_Part_3777_65107396.1485855297302" ------=_Part_3777_65107396.1485855297302 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Content-Location: file:///C:/exported.html
In order to display data of our Field Type from templates, we need to cr= eate and register a template for it. You can find documentation about FieldType templates, as w= ell as on <= span class=3D"confluence-link">importing settings from a bundle<= span class=3D"confluence-link"> .
In a couple words, such a template must:
extend EzPublishCoreBundle::content_fields.html.twig
<=
/p>
define a dedicated Twig block for the type, named by convention eztweet_field
be registered in parameters
Resources/views/fields/eztweet.tpl
The first thing we will do is create the template. It will basically def= ine the default display of a tweet. Remember that field type template= s can be overridden in order to tweak what is displayed and how.
Each Field Type template receives a set of variables that can be used to=
achieve the desired goal. The variable we care about is field
=
, an instance of eZ\Publish\API\Repository\Values\Content\Field
. In addition to its own metadata (id
, fieldDefIdentifi=
er
, etc.), it exposes the Field Value (Tweet\Value
) thr=
ough the value
property.
This would work as a primitive template:
{% block eztweet_field %} {% spaceless %} {{ field.value.contents|raw }} {% endspaceless %} {% endblock %}=20
field.value.contents
is piped through the r=
aw
twig operator, since the variable contains HTML code. Without it,=
the HTML markup would be visible directly, since twig escapes variables by=
default. Notice that we nest our code within a spaceless
tag, so that we can format our temp=
late in a readable manner without jeopardizing the display with unwanted sp=
aces.
Even though the above will work just fine, a couple of helpers will help=
us get something a bit more flexible. The EzPublishCoreBundle::content_fields.=
html.twig template, where the native Field Type templates are implement=
ed, provides a couple of helpers: simple_block_field
, si=
mple_inline_field
and field_attributes
. The first two a=
re used to display a field either as a block or inline. field_attribu=
tes
makes it easier to use the attr variable that contains additiona=
l (HTML) attributes for the field.
Let's consider that we will display the value as a block element.
First, we need to make our template inherit from content_fields.ht=
ml.twig
. Then, we will create a field_value
variable th=
at will be used by the helper to print out the content inside the markup. A=
nd that's it. The helper will use field_attributes
to add the =
HTML attributes to the generated div
.
{% block eztweet_field %} {% spaceless %} {% set field_value %} {{ field.value.contents|raw }} {% endset %} {{ block( 'simple_block_field' ) }} {% endspaceless %} {% endblock %}=20
fieldValue
is set to the markup we had above, using a =
{% set %}
block. We then call the block
func=
tion to process the simple_block_field
template block.
As explained in the FieldType template documentation, a Fiel=
dType template needs to be registered in the eZ Publish semantic configurat=
ion. The most basic way to do this would be to do so in app/config/ez=
platform.yml
:
ezpublish: global: field_templates: - { template: "EzSystemsTweetFieldTypeBundle:fields:eztweet.htm= l.twig"}=20
However, this is far from ideal. We want this to be part of our bundle, =
so that no manual configuration is required. For that to happen, we need to=
make our bundle extend the eZ Platform semantic configuration. To do so,&n=
bsp;we are going to make our bundle's dependency injection extension (DependencyInjection/EzSystemsTweetFieldTypeExtension.php
) impl=
ement Symfony\Component\DependencyInjection\Extension\PrependExtensio=
nInterface
. This interface will let us prepend bundle configuration:=
use Symfony\Component\DependencyInjection\Extension\PrependExtensio= nInterface; use Symfony\Component\Yaml\Yaml; class EzSystemsTweetFieldTypeExtension extends Extension implements Prepend= ExtensionInterface { public function prepend( ContainerBuilder $container ) { $config =3D Yaml::parse( __DIR__ . '/../Resources/config/ezpublish_= field_templates.yml' ); $container->prependExtensionConfig( 'ezpublish', $config ); } }=20
The last thing to do is move the template mapping from app/config/=
ezplatform.yml
to Resources/config/ezpublish_field_templa=
tes.yml
:
system: default: field_templates: - {template: "EzSystemsTweetFieldTypeBundle:fields:eztweet.html= .twig"}=20
Notice that the ezpublish
yaml block was deleted. This=
is because we already import our configuration under the ezpublish=
code> namespace in the prepend method.
You should now be able to display a content item with this Field Type fr= om the frontoffice, with a fully functional embed: