-
Notifications
You must be signed in to change notification settings - Fork 3
/
exportword0351
822 lines (785 loc) · 31.5 KB
/
exportword0351
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
Message-ID: <367505573.3774.1485855284550.JavaMail.confluence@ip-10-127-227-164>
Subject: Exported From Confluence
MIME-Version: 1.0
Content-Type: multipart/related;
boundary="----=_Part_3773_1178572581.1485855284550"
------=_Part_3773_1178572581.1485855284550
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable
Content-Location: file:///C:/exported.html
<html xmlns:o=3D'urn:schemas-microsoft-com:office:office'
xmlns:w=3D'urn:schemas-microsoft-com:office:word'
xmlns:v=3D'urn:schemas-microsoft-com:vml'
xmlns=3D'urn:w3-org-ns:HTML'>
<head>
<meta http-equiv=3D"Content-Type" content=3D"text/html; charset=3Dutf-8=
">
<title>Implement the Tweet\Type class</title>
<!--[if gte mso 9]>
<xml>
<o:OfficeDocumentSettings>
<o:TargetScreenSize>1024x640</o:TargetScreenSize>
<o:PixelsPerInch>72</o:PixelsPerInch>
<o:AllowPNG/>
</o:OfficeDocumentSettings>
<w:WordDocument>
<w:View>Print</w:View>
<w:Zoom>90</w:Zoom>
<w:DoNotOptimizeForBrowser/>
</w:WordDocument>
</xml>
<![endif]-->
<style>
<!--
@page Section1 {
size: 8.5in 11.0in;
margin: 1.0in;
mso-header-margin: .5in;
mso-footer-margin: .5in;
mso-paper-source: 0;
}
td {
page-break-inside: avoid;
}
tr {
page-break-after: avoid;
}
div.Section1 {
page: Section1;
}
/* Confluence print stylesheet. Common to all themes for print medi=
a */
/* Full of !important until we improve batching for print CSS */
@media print {
#main {
padding-bottom: 1em !important; /* The default padding of 6em is to=
o much for printouts */
}
body {
font-family: Arial, Helvetica, FreeSans, sans-serif;
font-size: 10pt;
line-height: 1.2;
}
body, #full-height-container, #main, #page, #content, .has-personal-sid=
ebar #content {
background: #fff !important;
color: #000 !important;
border: 0 !important;
width: 100% !important;
height: auto !important;
min-height: auto !important;
margin: 0 !important;
padding: 0 !important;
display: block !important;
}
a, a:link, a:visited, a:focus, a:hover, a:active {
color: #000;
}
#content h1,
#content h2,
#content h3,
#content h4,
#content h5,
#content h6 {
font-family: Arial, Helvetica, FreeSans, sans-serif;
page-break-after: avoid;
}
pre {
font-family: Monaco, "Courier New", monospace;
}
#header,
.aui-header-inner,
#navigation,
#sidebar,
.sidebar,
#personal-info-sidebar,
.ia-fixed-sidebar,
.page-actions,
.navmenu,
.ajs-menu-bar,
.noprint,
.inline-control-link,
.inline-control-link a,
a.show-labels-editor,
.global-comment-actions,
.comment-actions,
.quick-comment-container,
#addcomment {
display: none !important;
}
.comment .date::before {
content: none !important; /* remove middot for print view */
}
h1.pagetitle img {
height: auto;
width: auto;
}
.print-only {
display: block;
}
#footer {
position: relative !important; /* CONF-17506 Place the footer at en=
d of the content */
margin: 0;
padding: 0;
background: none;
clear: both;
}
#poweredby {
border-top: none;
background: none;
}
#poweredby li.print-only {
display: list-item;
font-style: italic;
}
#poweredby li.noprint {
display: none;
}
/* no width controls in print */
.wiki-content .table-wrap,
.wiki-content p,
.panel .codeContent,
.panel .codeContent pre,
.image-wrap {
overflow: visible !important;
}
/* TODO - should this work? */
#children-section,
#comments-section .comment,
#comments-section .comment .comment-body,
#comments-section .comment .comment-content,
#comments-section .comment p {
page-break-inside: avoid;
}
#page-children a {
text-decoration: none;
}
/**
hide twixies
the specificity here is a hack because print styles
are getting loaded before the base styles. */
#comments-section.pageSection .section-header,
#comments-section.pageSection .section-title,
#children-section.pageSection .section-header,
#children-section.pageSection .section-title,
.children-show-hide {
padding-left: 0;
margin-left: 0;
}
.children-show-hide.icon {
display: none;
}
/* personal sidebar */
.has-personal-sidebar #content {
margin-right: 0px;
}
.has-personal-sidebar #content .pageSection {
margin-right: 0px;
}
}
-->
</style>
</head>
<body>
<h1>Implement the Tweet\Type class</h1>
<div class=3D"Section1">
<div class=3D"contentLayout2">=20
<div class=3D"columnLayout two-right-sidebar" data-layout=3D"two-right-side=
bar">=20
<div class=3D"cell normal" data-type=3D"normal">=20
<div class=3D"innerCell">=20
<p>As said in the introduction, the Type class of a Field Type must impleme=
nt <code>eZ\Publish\SPI\FieldType\FieldType</code> (later referred to as "F=
ield Type interface").</p>
<p>All native Field Types also extend the <code>eZ\Publish\Core\FieldType\F=
ieldType</code> abstract class that implements this interface and provides =
implementation facilities through a set of abstract methods of its own. In =
this case, Type classes implement a mix of methods from the Field Type inte=
rface and from the abstract Field Type.</p>
<p>Let=E2=80=99s go over those methods and their implementation.</p>
<h4 id=3D"ImplementtheTweet\Typeclass-Identificationmethod:getFieldTypeIden=
tifier()">Identification method: <code>getFieldTypeIdentifier()</code></h4>
<p>It must return the string that uniquely identifies this Field Type (Data=
TypeString in legacy). We will use "<code>eztweet</code>":</p>
<div class=3D"code panel pdl" style=3D"border-width: 1px;">
<div class=3D"codeHeader panelHeader pdl" style=3D"border-bottom-width: 1px=
;">
<b>eZ/FieldType/Tweet/Type</b>
</div>
<div class=3D"codeContent panelContent pdl">=20
<pre class=3D"brush: php; gutter: false; theme: Eclipse" style=3D"font-size=
:12px;">public function getFieldTypeIdentifier()
{
return 'eztweet';
}</pre>=20
</div>
</div>
<h4 id=3D"ImplementtheTweet\Typeclass-Valuehandlingmethods:createValueFromI=
nput()andcheckValueStructure()">Value handling methods: <code>createVa=
lueFromInput()</code> and <code>checkValueStructure()</code></h4>
<p>Both methods are used by the abstract Field Type implementation of <code=
>acceptValue()</code>. This Field Type interface method checks and tra=
nsforms various input values into the type's own Value class: <code>eZ\Fiel=
dType\Tweet\Value</code>. This method must:</p>
<ul>
<li>either return the Value object it was able to create out of the input v=
alue,</li>
<li>or return this value untouched. The API will detect this and inform tha=
t the input value was not accepted.</li>
</ul>
<p>The only acceptable value for our type is the URL of a tweet (we could o=
f course imagine more possibilities). This should do:</p>
<p> </p>
<div class=3D"code panel pdl" style=3D"border-width: 1px;">
<div class=3D"codeContent panelContent pdl">=20
<pre class=3D"brush: php; gutter: false; theme: Eclipse" style=3D"font-size=
:12px;">protected function createValueFromInput( $inputValue )
{
if ( is_string( $inputValue ) )
{
$inputValue =3D new Value( array(=
'url' =3D> $inputValue ) );
}
return $inputValue;
}
</pre>=20
</div>
</div>
<p><br> Use this method to provide convenient ways to set an attribute=E2=
=80=99s value using the API. This can be anything from primitives to comple=
x business objects.</p>
<p>Next, we will implement <code> checkValueStructure()</code>. It is calle=
d by the abstract Field Type to ensure that the Value fed to the Type is ac=
ceptable. In our case, we want to be sure that <code>Tweet</code> <code> \V=
alue::$url</code> is a string:</p>
<p> </p>
<div class=3D"code panel pdl" style=3D"border-width: 1px;">
<div class=3D"codeContent panelContent pdl">=20
<pre class=3D"brush: php; gutter: false; theme: Eclipse" style=3D"font-size=
:12px;">protected function checkValueStructure( BaseValue $value )
{
if ( !is_string( $value->url ) )
{
throw new eZ\Publish\Core\Base\Ex=
ceptions\InvalidArgumentType(
'$value-&=
gt;url',
'string',
$value-&g=
t;url
);
}
}
</pre>=20
</div>
</div>
<p>Yes, we execute the same check as in <code>createValueFromInput()</code>=
. But both methods aren't responsible for the same thing. The first will, <=
em>if given something else than a Value of its type</em>, try to convert it=
to one. <code>checkValueStructure()</code> will always be used, even =
if the Field Type is directly fed a <code>Value</code> object, and not a st=
ring.</p>
<h4 id=3D"ImplementtheTweet\Typeclass-Valueinitialization:getEmptyValue()">=
Value initialization: <code>getEmptyValue()</code></h4>
<p>This method provides what is considered as an empty value of this type, =
depending on our business requirements. No extra initialization is required=
in our case.</p>
<p> </p>
<div class=3D"code panel pdl" style=3D"border-width: 1px;">
<div class=3D"codeContent panelContent pdl">=20
<pre class=3D"brush: php; gutter: false; theme: Eclipse" style=3D"font-size=
:12px;">public function getEmptyValue()
{
return new Value;
}</pre>=20
</div>
</div>
<p>If you run the unit tests at this point, you should get about five failu=
res, all of them on the <code>fromHash()</code> or <code> toHash()</code> m=
ethods.</p>
<h4 id=3D"ImplementtheTweet\Typeclass-Validationmethods:validateValidatorCo=
nfiguration()andvalidate()">Validation methods: <code>validateValidatorConf=
iguration()</code> and <code>validate()</code></h4>
<p>The Type class is also responsible for validating input data (to a <code=
>Field</code>), as well as configuration input data (to a <code>FieldDefini=
tion</code>). In this tutorial, we will run two validation operations on in=
put data:</p>
<ul>
<li><p>validate submitted urls, ensuring they actually reference a twitter =
status;</p></li>
<li><p>limit input to a known list of authors, as an optional validation st=
ep.</p></li>
</ul>
<p><code> validateValidatorConfiguration()</code> will be called when an in=
stance of the Field Type is added to a Content Type, to ensure that the val=
idator configuration is valid. For a TextLine (length validation), it means=
checking that both min length and max length are positive integers, and th=
at min is lower than max.</p>
<p>When an instance of the type is added to a Content Type, <code>validateV=
alidatorConfiguration()</code> receives the configuration for the vali=
dators used by the Type as an array. It must return an array of error messa=
ges if errors are found in the configuration, and an empty array if no erro=
rs were found.</p>
<p>For TextLine, the provided array looks like this:</p>
<p> </p>
<div class=3D"code panel pdl" style=3D"border-width: 1px;">
<div class=3D"codeContent panelContent pdl">=20
<pre class=3D"brush: php; gutter: false; theme: Eclipse" style=3D"font-size=
:12px;">array(
'StringLengthValidator' =3D> array(
'minStringLength' =3D> 0,
'maxStringLength' =3D> 100
)
);
</pre>=20
</div>
</div>
<p>The structure of this array is totally free, and up to each type impleme=
ntation. We will in this tutorial mimic what is done in native Field Types:=
</p>
<p>Each level one key is the name of a validator, as acknowledged by the Ty=
pe. That key contains a set of parameter name / parameter value rows. We mu=
st check that:</p>
<ul>
<li><p>all the validators in this array are known to the type</p></li>
<li><p>arguments for those validators are valid and have sane values</p></l=
i>
</ul>
<p>We do not need to include mandatory validators if they don=E2=80=99t hav=
e options. Here is an example of what our Type expects as validation config=
uration:</p>
<p> </p>
<div class=3D"code panel pdl" style=3D"border-width: 1px;">
<div class=3D"codeContent panelContent pdl">=20
<pre class=3D"brush: php; gutter: false; theme: Eclipse" style=3D"font-size=
:12px;">array(
=E2=80=98TweetAuthorValidator=E2=80=99 =3D> array(
=E2=80=98AuthorList=E2=80=99 =3D&=
gt; array( =E2=80=98johndoe=E2=80=99, =E2=80=98janedoe=E2=80=99 )
)
);
</pre>=20
</div>
</div>
<p><br> The configuration says that tweets must be either by johndoe o=
r by janedoe. If we had not provided TweetAuthorValidator at all, it would =
have been ignored.</p>
<p>We will iterate over the items in <code>$validatorConfiguration</code> a=
nd:</p>
<ul>
<li><p>add errors for those we don=E2=80=99t know about;</p></li>
<li><p>check that provided arguments are known and valid:</p></li>
<ul>
<li><p>TweetAuthorValidator accepts a non-empty array of valid Twitter user=
names</p></li>
</ul>
</ul>
<p> </p>
<div class=3D"code panel pdl" style=3D"border-width: 1px;">
<div class=3D"codeContent panelContent pdl">=20
<pre class=3D"brush: php; gutter: false; theme: Eclipse" style=3D"font-size=
:12px;">public function validateValidatorConfiguration( $validatorConfigura=
tion )
{
$validationErrors =3D array();
foreach ( $validatorConfiguration as $validatorIdentifier=
=3D> $constraints )
{
// Report unknown validators
if ( !$validatorIdentifier !=3D '=
TweetAuthorValidator' )
{
$validati=
onErrors[] =3D new ValidationError( "Validator '$validatorIdentifier' is un=
known" );
continue;
}
// Validate arguments from TweetA=
uthorValidator
if ( !isset( $constraints['Author=
List'] ) || !is_array( $constraints['AuthorList'] ) )
{
$validati=
onErrors[] =3D new ValidationError( "Missing or invalid AuthorList argument=
" );
continue;
}
foreach ( $constraints['AuthorLis=
t'] as $authorName )
{
if ( !pre=
g_match( '/^[a-z0-9_]{1,15}$/i', $authorName ) )
{
&nb=
sp; $validationErrors[] =3D new ValidationError( "Invalid twitte=
r username" );
}
}
}
return $validationErrors;
}</pre>=20
</div>
</div>
<p><code> validate()</code> is the method that runs the actual validation o=
n data, when a content item is created with a Field of this type:</p>
<p> </p>
<div class=3D"code panel pdl" style=3D"border-width: 1px;">
<div class=3D"codeContent panelContent pdl">=20
<pre class=3D"brush: php; gutter: false; theme: Eclipse" style=3D"font-size=
:12px;"> public function validate( FieldDefinition $fieldD=
efinition, SPIValue $fieldValue )
{
$errors =3D array();
if ( $this->isEmptyValue( $fie=
ldValue ) )
{
return $e=
rrors;
}
// Tweet Url validation
if ( !preg_match( '#^https?://twi=
tter.com/([^/]+)/status/[0-9]+$#', $fieldValue->url, $m ) )
$errors[]=
=3D new ValidationError( "Invalid twitter status url %url%", null, array( =
$fieldValue->url ) );
$validatorConfiguration =3D $fiel=
dDefinition->getValidatorConfiguration();
if ( isset( $validatorConfigurati=
on['TweetAuthorValidator'] ) )
{
if ( !in_=
array( $m[1], $validatorConfiguration['TweetAuthorValidator']['AuthorList']=
) )
{
&nb=
sp; $errors[] =3D new ValidationError(
&nb=
sp; "Twitter user %user% is not in the a=
pproved author list",
&nb=
sp; null,
&nb=
sp; array( $m[1] )
&nb=
sp; );
}
}
return $errors;
}
</pre>=20
</div>
</div>
<p>First, we validate the url with a regular expression. If it doesn=E2=80=
=99t match, we add an instance of <code> ValidationError </code> to the ret=
urn array. Note that the tested value isn=E2=80=99t directly embedded in th=
e message but passed as an argument. This ensures that the variable is prop=
erly encoded in order to prevent attacks, and allows for singular/plural ph=
rases using the second parameter.</p>
<p>Then, if our Field Type instance=E2=80=99s configuration contains a <cod=
e> TweetAuthorValidator </code> key, we check that the username in the stat=
us url matches one of the valid authors.</p>
<h4 id=3D"ImplementtheTweet\Typeclass-Metadatahandlingmethods:getName()andg=
etSortInfo().">Metadata handling methods: <code>getName()</code> and <code>=
getSortInfo()</code>.</h4>
<p>Field Types require two methods related to Field metadata:</p>
<ul>
<li><p><code> getName() </code> is used to generate a name out of a Field v=
alue, either to name a Content item (naming pattern in legacy) or to genera=
te a part for an URL Alias.</p></li>
<li><p><code> getSortInfo() </code> is used by the persistence layer to obt=
ain the value it can use to sort & filter on a Field of this type</p></=
li>
</ul>
<p>Obviously, a tweet=E2=80=99s full URL isn=E2=80=99t really suitable as a=
name. Let=E2=80=99s use a subset of it: <code> <username>-<tweetI=
d> </code> should be reasonable enough, and suitable for both sorting an=
d naming.</p>
<p> </p>
<p>We can assume that this method will not be called if the Field is empty,=
and will assume that the URL is a valid twitter URL:</p>
<p> </p>
<div class=3D"code panel pdl" style=3D"border-width: 1px;">
<div class=3D"codeContent panelContent pdl">=20
<pre class=3D"brush: php; gutter: false; theme: Eclipse" style=3D"font-size=
:12px;">public function getName( SPIValue $value )
{
return preg_replace(
'#^https?://twitter\.com/([^/]+)/=
status/([0-9]+)$#',
'$1-$2',
(string)$value->url );
}
protected function getSortInfo( CoreValue $value )
{
return $this->getName( $value );
}</pre>=20
</div>
</div>
<p>In <code> getName()</code> we run a regular expression replace on the UR=
L to extract the part we=E2=80=99re interested in.</p>
<p>This name is a perfect match for <code> getSortInfo()</code> as it allow=
s us to sort on the tweet=E2=80=99s author and on the tweet=E2=80=99s ID.</=
p>
<h4 id=3D"ImplementtheTweet\Typeclass-FieldTypeserializationmethods:fromHas=
h()andtoHash()">Field Type serialization methods: <code>fromHash()</code> a=
nd <code>toHash()</code></h4>
<p>Both methods defined in the Field Type interface, are core to the REST A=
PI. They are used to export values to serializable hashes.</p>
<p>In our case, it is quite easy:</p>
<ul>
<li><p><code> toHash() </code> will build a hash with every property from <=
code>Tweet\Value</code>;</p></li>
<li><p><code> fromHash() </code> will instantiate a <code>Tweet\Value</code=
> with the hash it receives. </p></li>
</ul>
<p> </p>
<div class=3D"code panel pdl" style=3D"border-width: 1px;">
<div class=3D"codeContent panelContent pdl">=20
<pre class=3D"brush: php; gutter: false; theme: Eclipse" style=3D"font-size=
:12px;">public function fromHash( $hash )
{
if ( $hash =3D=3D=3D null )
{
return $this->getEmptyValue();
}
return new Value( $hash );
}
public function toHash( SPIValue $value )
{
if ( $this->isEmptyValue( $value ) )
{
return null;
}
return array(
'url' =3D> $value->url
);
}
</pre>=20
</div>
</div>
<h4 id=3D"ImplementtheTweet\Typeclass-Persistencemethods:fromPersistenceVal=
ueandtoPersistenceValue">Persistence methods: <code>fromPersistenceVal=
ue</code> and <code>toPersistenceValue</code></h4>
<p>Storage of Field Type data is done through the persistence layer (SPI).<=
/p>
<p>Field Types use their own Value objects to expose their contents using t=
heir own domain language. However, to store those objects, the Type needs t=
o map this custom object to a structure understood by the persistence layer=
: <code>PersistenceValue</code>. This simple value object has three pr=
operties:</p>
<ul style=3D"list-style-type: square;">
<li><code>data</code> =E2=80=93 standard data, stored using the storage eng=
ine's native features</li>
<li><code>externalData</code> =E2=80=93 external data, stored using a custo=
m storage handler</li>
<li><code>sortKey</code> =E2=80=93 sort value used for sorting</li>
</ul>
<p>The role of those mapping methods is to convert a <code>Value</code>&nbs=
p;of the Field Type into a <code>PersistenceValue</code> and the other way =
around.</p>
<div class=3D"confluence-information-macro confluence-information-macro-inf=
ormation">
<p class=3D"title">About external storage</p>
<span class=3D"aui-icon aui-icon-small aui-iconfont-info confluence-informa=
tion-macro-icon"></span>
<div class=3D"confluence-information-macro-body">
<p>Whatever is stored in {{externalData}} requires an external storage hand=
ler to be written. Read more about external storage on <a href=3D"/display/=
DEVELOPER/Field+Type+API+and+best+practices">Field Type API and best practi=
ces</a>.</p>
<p>External storage is beyond the scope of this tutorial, but many examples=
can be found in existing Field Types.</p>
</div>
</div>
<p>We will follow a simple implementation here: the <code>Tweet\Value</code=
> object will be serialized as an array to the <code>code</code> =
property using <code>fromHash()</code> and <code>toHash()</code>:=
</p>
<div class=3D"code panel pdl" style=3D"border-width: 1px;">
<div class=3D"codeHeader panelHeader pdl" style=3D"border-bottom-width: 1px=
;">
<b>Tweet\Type</b>
</div>
<div class=3D"codeContent panelContent pdl">=20
<pre class=3D"brush: php; gutter: false; theme: Eclipse" style=3D"font-size=
:12px;">/**
* @param \EzSystems\TweetFieldTypeBundle\eZ\Publish\FieldType\Tweet\Value =
$value
* @return \eZ\Publish\SPI\Persistence\Content\FieldValue
*/
public function toPersistenceValue( SPIValue $value )
{
if ( $value =3D=3D=3D null )
{
return new PersistenceValue(
array(
"data" =3D> null,
"externalData" =3D> null,
"sortKey" =3D> null,
)
);
}
return new PersistenceValue(
array(
"data" =3D> $this->toHash( $value ),
"sortKey" =3D> $this->getSortInfo( $value ),
)
);
}
/**
* @param \eZ\Publish\SPI\Persistence\Content\FieldValue $fieldValue
* @return \EzSystems\TweetFieldTypeBundle\eZ\Publish\FieldType\Tweet\Value
*/
public function fromPersistenceValue( PersistenceValue $fieldValue )
{
if ( $fieldValue->data =3D=3D=3D null )
{
return $this->getEmptyValue();
}
return new Value( $fieldValue->data );
}</pre>=20
</div>
</div>
<p> </p>
<h5 id=3D"ImplementtheTweet\Typeclass-FetchingdatafromtheTwitterAPI">Fetchi=
ng data from the Twitter API</h5>
<p>As explained in the tutorial's introduction, we will enrich our tweet's =
URL with the embed version, fetched using the Twitter API. To do so, we wil=
l, when <code>toPersistenceValue()</code> is called, fill in the value=
's contents property from this method, before creating the <code>Persistenc=
eValue</code> object.</p>
<p>First, we need a twitter client in <code>Tweet\Type</code>. For con=
venience, we provide one in this tutorial's bundle:</p>
<div>
<ul style=3D"list-style-type: square;">
<li>The <code>Twitter\TwitterClient</code> class:</li>
<li>The <code>Twitter\TwitterClientInterface</code> interface</li>
<li>An <code>ezsystems.tweetbundle.twitter.client</code> service that =
uses the class above.</li>
</ul>
<p>The interface has one method: <code>getEmbed( $statusUrl )</code> that, =
given a tweet's URL, returns the embed code as a string. The implementation=
is very simple, for the sake of simplicity, but gets the job done. Ideally=
, it should at the very least handle errors, but it is not necessary here.<=
/p>
<h6 id=3D"ImplementtheTweet\Typeclass-InjectingtheTwitterclientintoTweet\Ty=
pe">Injecting the Twitter client into <code>Tweet\Type</code></h6>
<p>Our Field Type doesn't have a constructor yet. We will create one, with =
an instance of <code>Twitter\TwitterClientInterface</code> as the argu=
ment, and store it in a new protected property:</p>
<div class=3D"code panel pdl" style=3D"border-width: 1px;">
<div class=3D"codeHeader panelHeader pdl" style=3D"border-bottom-width: 1px=
;">
<b>eZ/Publish/FieldType/Tweet/Type.php:</b>
</div>
<div class=3D"codeContent panelContent pdl">=20
<pre class=3D"brush: php; gutter: false; theme: Eclipse" style=3D"font-size=
:12px;">use EzSystems\TweetFieldTypeBundle\Twitter\TwitterClientInterface;
class Type extends FieldType
{
/** @var TwitterClientInterface */
protected $twitterClient;
public function __construct( TwitterClientInterface $twitterClient )
{
$this->twitterClient =3D $twitterClient;
}
}</pre>=20
</div>
</div>
<h6 id=3D"ImplementtheTweet\Typeclass-Completingthevalueusingthetwitterclie=
nt">Completing the value using the twitter client</h6>
<p>As described above, before creating the <code>PersistenceValue</code> ob=
ject in <code>toPersistenceValue</code>, we will fetch the tweet's embed co=
ntents using the client, and assign it to <code>Tweet\Value::$data</co=
de>:</p>
<div class=3D"code panel pdl" style=3D"border-width: 1px;">
<div class=3D"codeHeader panelHeader pdl" style=3D"border-bottom-width: 1px=
;">
<b>eZ/Publish/FieldType/Tweet/Type.php</b>
</div>
<div class=3D"codeContent panelContent pdl">=20
<pre class=3D"brush: php; gutter: false; theme: Eclipse" style=3D"font-size=
:12px;"> public function toPersistenceValue( SPIValue $value )
{
// if ( $value =3D=3D=3D null )
// {...}
if ( $value->contents =3D=3D=3D null )
{
$value->contents =3D $this->twitterClient->getEmbed( $valu=
e->url );
}
return new PersistenceValue(
// array(...)
}
</pre>=20
</div>
</div>
<p>And that's it! When the persistence layer stores content from our type, =
the value will be completed with what the twitter API returns.</p>
<p> </p>
</div>
</div>=20
</div>=20
<div class=3D"cell aside" data-type=3D"aside">=20
<div class=3D"innerCell">=20
<div class=3D"panel" style=3D"border-width: 1px;">
<div class=3D"panelHeader" style=3D"border-bottom-width: 1px;">
<b>Tutorial path</b>
</div>
<div class=3D"panelContent">=20
<p> </p>
<div class=3D"plugin_pagetree">=20
<ul class=3D"plugin_pagetree_children_list plugin_pagetree_children_list_no=
leftspace">=20
<div class=3D"plugin_pagetree_children">=20
</div>=20
</ul>=20
<fieldset class=3D"hidden">=20
<input type=3D"hidden" name=3D"treeId" value=3D"">=20
<input type=3D"hidden" name=3D"treeRequestId" value=3D"/plugins/pagetree/na=
turalchildren.action?decorator=3Dnone&excerpt=3Dfalse&sort=3Dpositi=
on&reverse=3Dfalse&disableLinks=3Dfalse&expandCurrent=3Dfalse">=
=20
<input type=3D"hidden" name=3D"treePageId" value=3D"">=20
<input type=3D"hidden" name=3D"noRoot" value=3D"false">=20
<input type=3D"hidden" name=3D"rootPageId" value=3D"31429766">=20
<input type=3D"hidden" name=3D"rootPage" value=3D"">=20
<input type=3D"hidden" name=3D"startDepth" value=3D"0">=20
<input type=3D"hidden" name=3D"spaceKey" value=3D"DEVELOPER">=20
<input type=3D"hidden" name=3D"i18n-pagetree.loading" value=3D"Loading...">=
=20
<input type=3D"hidden" name=3D"i18n-pagetree.error.permission" value=3D"Una=
ble to load page tree. It seems that you do not have permission to view the=
root page.">=20
<input type=3D"hidden" name=3D"i18n-pagetree.eeror.general" value=3D"There =
was a problem retrieving the page tree. Please check the server log file fo=
r more information.">=20
<input type=3D"hidden" name=3D"loginUrl" value=3D"/login.action?os_destinat=
ion=3D%2Fexportword%3FpageId%3D31429772">=20
<input type=3D"hidden" name=3D"mobile" value=3D"false">=20
<fieldset class=3D"hidden">=20
<input type=3D"hidden" name=3D"ancestorId" value=3D"31429766">=20
</fieldset>=20
</fieldset>=20
</div>=20
<p></p>=20
</div>
</div>
</div>=20
</div>=20
</div>=20
</div>
</div>
</body>
</html>
------=_Part_3773_1178572581.1485855284550--