-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.html
1006 lines (942 loc) · 40 KB
/
index.html
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
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>30 Drupal 8 API Functions You Should Already Know</title>
<meta name="description" content="A quick look at the common API functions you should already be using in Drupal 8!">
<meta name="author" content="Fredric Mitchell">
<meta name="apple-mobile-web-app-capable" content="yes" />
<meta name="apple-mobile-web-app-status-bar-style" content="black-translucent" />
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
<link rel="stylesheet" href="css/reveal.min.css">
<link rel="stylesheet" href="phase2/phase2.css" id="theme">
<!-- For syntax highlighting -->
<link rel="stylesheet" href="lib/css/zenburn.css">
<!-- If the query includes 'print-pdf', use the PDF print sheet -->
<script>
document.write( '<link rel="stylesheet" href="css/print/' + ( window.location.search.match( /print-pdf/gi ) ? 'pdf' : 'paper' ) + '.css" type="text/css" media="print">' );
</script>
<!--[if lt IE 9]>
<script src="lib/js/html5shiv.js"></script>
<![endif]-->
</head>
<body>
<div class="reveal">
<!-- Any section element inside of this container is displayed as a slide -->
<div class="slides">
<section class="front branding-front">
<h1>30 Awesome Drupal 8 API Functions You Should Already Know</h1>
<p>
<small>Created by <a href="http://www.phase2technology.com/author/fmitchell/">Fredric Mitchell</a> / <a href="http://twitter.com/fredricmitchell">@fredricmitchell</a></small>
</p>
<p>
<small>http://bit.ly/d8func-austin</small>
</p>
</section>
<section>
<h2>Assumptions</h2>
<ol>
<li class="fragment">Show Me the Code!</li>
<li class="fragment">Written something custom (familiar with PHP)</li>
<li class="fragment">You like using APIs</li>
<li class="fragment">Drupal as framework</li>
<li class="fragment">KDS: http://bit.ly/p2-kds</li>
</ol>
</section>
<section>
<section>
<h2>Quick Intro</h2>
<h3>Paid Gig</h3>
<p>
<img src="http://distilleryimage8.ak.instagram.com/039fa8ee425811e3939222000a9f1385_8.jpg" height="450px"/>
</p>
<p>Senior Developer at Phase2 Technology</p>
</section>
<section>
<h2>Quick Intro</h2>
<h3>Passions</h3>
<p>
<img src="http://images.ak.instagram.com/media/2011/08/03/9834110b48784fcf95e9c5f3e96c4da9_7.jpg" height="450px"/>
</p>
<p>Drupal, DRY / KISS, Strat. Comm., Graphic Novels</p>
</section>
</section>
<section>
<section>
<h2>Disclaimer! Disclaimer!</h2>
<h3 class="fragment">In D7, "API functions" == Common procedural functions</h3>
<h3 class="fragment">In D8, "API functions" == Common class methods</h3>
<h3 class="fragment">Some cases, procedurals are wrappers for methods</h3>
<h3 class="fragment">Then, and now</h3>
</section>
<section>
<h2>Additional Disclaimer! Disclaimer!</h2>
<h3 class="fragment">All recommendations are based on D8 as of June 3, 2014</h3>
<h3 class="fragment">Things may (and probably will) change</h3>
</section>
</section>
<section>
<section>
<h2>Strings</h2>
<h3>Print stuff.</h3>
</section>
<section>
<h2>String::checkPlain()</h2>
<h3>D7: check_plain()</h3>
<p>Wrapper for htmlspecialchars with UTF-8 encoding</p>
<p class="fragment">Still in bootstrap <br /><code>use Drupal\Component\Utility\String;</code></p>
</section>
<section>
<h2>String::placeholder()</h2>
<h3>D7: drupal_placeholder()</h3>
<p>Wrap <em> around String:checkPlain()</p>
</section>
<section>
<h2>t()</h2>
<h3>translate, sanitize</h3>
<pre>
<code data-trim contenteditable>
$text = t(“@name's blog”, array('@name' => $value))
// Note:
@name (String::checkPlain())
vs. %name (String::placeholder())
vs. !name
</code>
</pre>
</section>
</section>
<section>
<h2>l()</h2>
<h3>internal, external url</h3>
<pre>
<code data-trim contenteditable>
$link = l(t('Link text'), 'node/34', array(
'attributes' => array(
'class' => array('about-link'),
)
));
// Note: automatic alias replacement via url()
</code>
</pre>
</section>
</section>
<section>
<section>
<h2>Rendering</h2>
<h3>Make stuff pretty.</h3>
</section>
<section>
<h2>drupal_render()</h2>
<h3>given render array, return HTML</h3>
<pre>
<code data-trim contenteditable>
$render = array(
'first_para' => array(
'#type' => 'markup',
'#markup' => 'A first paragraph',
),
'second_para' => array(
array('first item', 'second item', 'third item'),
'#theme' => 'item_list',
),
);
return drupal_render($render);
</code>
</pre>
</section>
<section>
<h2>D8: drupal_render() is EVERYTHING!</h2>
<h3>Bye-bye theme()</h3>
<p>_theme() is an internal function</p>
<p>#type defined in hook_element_info</p>
</section>
<section>
<p>D7</p>
<pre>
<code data-trim contenteditable>
// Theme a table with header and row data.
$markup = theme('table', array(
'header' => $header,
'rows' => $rows,
'attributes' => array('id' => 'my-module-table')
));
// Render a pager.
$markup .= theme('pager');
</code>
</pre>
</section>
<section>
<p>D8</p>
<pre>
<code data-trim contenteditable>
$table = array(
'#type' => 'table',
'#header' => $header,
'#rows' => $rows,
'#attributes' => array('id' => 'my-module-table'),
);
$markup = drupal_render($table);
// Pager is not an element type, use #theme directly.
$pager = array('#theme' => 'pager');
$markup .= drupal_render($pager);
</code>
</pre>
</section>
</section>
<section>
<section>
<h2>Nodes</h2>
<h3>Store content, data.</h3>
</section>
<section>
<h2>Wait...what!??! Hello? Entities!</h2>
<h3>Everything is entities now, right?</h3>
<p class="fragment">Yes. I was just getting there.</p>
</section>
<section>
<h2>entity_load_multiple()</h2>
<h3>D7: node_load_multiple()</h3>
<h3>load nodes from db</h3>
<p class="fragment">node_load_multiple wraps around entity_load_multiple</p>
<p class="fragment">entity_load_multiple wraps around $controller->loadMultiple()</p>
<p class="fragment">$controller is the StorageController for the entity</p>
<p class="fragment">The storage controller is likely EntityDatabaseStorage class</p>
</section>
<section>
<h2>entity_load_multiple()</h2>
<pre>
<code data-trim contenteditable>
$nodes = entity_load_multiple('node', $nids = array());
// Simpler
use \Drupal\node\Entity\Node;
$node = Node::load(1);
$nodes = Node::loadMultiple(array(1, 2, 3));
</code>
</pre>
</section>
<section>
<h2>entity_view_multiple()</h2>
<h3>D7: node_view_multiple()</h3>
<h3>a render array of many nodes</h3>
<p class="fragment">node_view_multiple wraps around entity_view_multiple</p>
<p class="fragment">entity_view_multiple wraps around <br>$render_controller->viewMultiple()</p>
<p class="fragment">$render_controller is the getViewBuilder for the entity</p>
<p class="fragment">The view builder controller is likely EntityViewBuilder class</p>
</section>
<section>
<h2>entity_view_multiple()</h2>
<pre>
<code data-trim contenteditable>
$nodes = entity_view_multiple($nodes);
</code>
</pre>
</section>
<section>
<h2>entity_create()</h2>
<h3>D7: node_save()</h3>
<h3>save a node to db</h3>
<p class="fragment">node_save() is gone</p>
<p class="fragment">entity_create wraps around $controller->create()</p>
<p class="fragment">$controller is the StorageController for the entity</p>
<p class="fragment">The storage controller is likely EntityDatabaseStorage class</p>
</section>
<section>
<h2>entity_create()</h2>
<pre>
<code data-trim contenteditable>
// Create a new forum instance.
$node = entity_create('node', array('otherValue' => 'foo'));
// Simpler
$node = Node::create(array('title' => 'My title'));
// Save entity to the database.
$node->save();
// Note: nothing is returned
</code>
</pre>
</section>
</section>
<section>
<section>
<h2>Menus</h2>
<h3>Allow users to navigate to content, data.</h3>
</section>
<section>
<h2>module.routing.yml</h2>
<h3>D7: hook_menu()</h3>
<h3>define menu items and callbacks</h3>
<p>D7</p>
<pre>
<code data-trim contenteditable>
function mymodule_menu() {
$items['abc/def'] = array(
'page callback' => 'mymodule_abc_view',
);
return $items;
}
function mymodule_abc_view($ghi = 0, $jkl = '') {
// ...
}
</code>
</pre>
</section>
<section>
<h2>module.routing.yml</h2>
<p>D8</p>
<pre>
<code data-trim contenteditable>
mymodule.add_page:
path: 'abc/def'
defaults:
_content: '\Drupal\mymodule\Controller\MyModuleController::addPage'
_title: 'My Module'
requirements:
_permission: 'administer mymodule foobar'
</code>
</pre>
</section>
<section>
<h2>$request()->attributes->get()</h2>
<h3>D7: menu_get_item()</h3>
<h3>get a router item</h3>
<p>Uses the new routing system from Symfony</p>
<pre>
<code data-trim contenteditable>
$request = \Drupal::request();
$route = $request()->attributes->get(RouteObjectInterface::ROUTE_OBJECT);
</code>
</pre>
</section>
<section>
<h2>$request()->attributes->get()</h2>
<h3>D7: menu_get_object()</h3>
<h3>load an object from router item</h3>
<pre>
<code data-trim contenteditable>
$request = \Drupal::request();
$node = $request()->attributes->get('node');
$type = $node->type;
// Note: Use instead of arg()
</code>
</pre>
<p class="fragment">As of 6 hours ago: https://drupal.org/node/2238217</p>
<p class="fragment">\Drupal::routeMatch() service wrapper</p>
</section>
<section>
<h2>menu_tree_page_data()</h2>
<h3>get data for menu tree</h3>
<pre>
<code data-trim contenteditable>
$tree = menu_tree_page_data('main-menu');
</code>
</pre>
</section>
<section>
<h2>menu_tree_output()</h2>
<h3>return a menu tree render array</h3>
<pre>
<code data-trim contenteditable>
// Build a HTML menu
$tree = menu_tree_page_data('main-menu');
$menu = menu_tree_output($tree, 1);
return drupal_render($menu);
</code>
</pre>
</section>
</section>
<section>
<section>
<h2>Taxonomy</h2>
<h3>Categorize content, data.</h3>
</section>
<section>
<h3>entity_load('taxonomy_vocabulary', 'forums')</h3>
<h4>D7: taxonomy_vocabulary_machine_name_load()</h4>
<h5>get vocabulary by machine name</h5>
<p>D7</p>
<pre>
<code data-trim contenteditable>
// Get a taxonomy object.
$vocabulary = taxonomy_vocabulary_machine_name_load('forums');
$options[$vocabulary->vid] = $vocabulary->name;
</code>
</pre>
<p>D8</p>
<pre>
<code data-trim contenteditable>
// Get a taxonomy object.
$vocabulary = entity_load('taxonomy_vocabulary', 'forums');
$options[$vocabulary->id()] = $vocabulary->label();
</code>
</pre>
</section>
<section>
<h2>taxonomy_get_tree()</h2>
<h3>create hierarchy from vocab</h3>
<pre>
<code data-trim contenteditable>
// Get a taxonomy object.
$v = entity_load('taxonomy_vocabulary', 'forums');
// Build an array of taxonomy terms.
$terms = taxonomy_get_tree($v->id());
foreach ($terms as $term) {
$options[$term->tid] = $term->name;
}
</code>
</pre>
</section>
</section>
<section>
<section>
<h2>Fields</h2>
<h3>Organize content, data at various pivot points.</h3>
</section>
<section>
<h2>$fieldInfo->getField()</h2>
<h3>D7: field_info_field()</h3>
<h3>return field data</h3>
<p class="fragment">Wraps around entity_load('field_config', ...)</p>
<pre>
<code data-trim contenteditable>
// Get info a field name.
$fieldInfo = \Drupal\field\Field::fieldInfo();
$fieldInfo->getField($entity_type, $field_name);
</code>
</pre>
<p class="fragment">\Drupal::service('field.info'); (bonus)</p>
</section>
<section>
<h2>$fieldInfo->getInstance()</h2>
<h3>D7: field_info_instance()</h3>
<h3>return field instance data</h3>
<p class="fragment">Wraps around entity_load_multiple('field_instance_config', ...)</p>
<pre>
<code data-trim contenteditable>
// Get info on field name.
$fieldInfo = \Drupal\field\Field::fieldInfo();
// Get info on instance of field name from a bundle.
$fieldInfo->getInstance('node', 'article', 'field_name');
</code>
</pre>
</section>
<section>
<h2>entity_create('field_config', $values)</h2>
<h3>D7: field_create_field()</h3>
<h3>creates a field</h3>
<p class="fragment">The field configuration (field_config) is now an entity type</p>
<p class="fragment">entity_create() invokes the create method of the EntityStorageBase (abstract)</p>
<p class="fragment">The field_config maps to class FieldConfig and uses FieldConfigStorage</p>
<p class="fragment">All other methods are in \Drupal\field\Entity\FieldConfig</p>
</section>
<section>
<h2>field_create_field()</h2>
<p>D7</p>
<pre>
<code data-trim contenteditable>
// Note: Requires proper field / instance data array which you can get from a features export.
$field_bases = array();
// Exported field_base: 'field_sc_html'
$field_bases['field_sc_html'] = array(
'active' => 1,
'cardinality' => 1,
'deleted' => 0,
'entity_types' => array(),
'field_name' => 'field_sc_html',
'foreign keys' => array(
'format' => array(
'columns' => array(
'format' => 'format',
),
'table' => 'filter_format',
),
),
'indexes' => array(
'format' => array(
0 => 'format',
),
),
'locked' => 0,
'module' => 'text',
'settings' => array(),
'translatable' => 0,
'type' => 'text_long',
);
field_create_field($field_bases);
</code>
</pre>
</section>
<section>
<h2>entity_create('field_config', $values)</h2>
<p>D8</p>
<pre>
<code data-trim contenteditable>
$field = entity_create('field_config', array(
'name' => 'field_description',
'entity_type' => 'taxonomy_term',
'type' => 'text',
));
$field->save();
</code>
</pre>
</section>
<section>
<h2>entity_create(<br>'field_instance_config', $values)</h2>
<h3>D7: field_create_instance()</h3>
<h3>create instance of field, binding it to a bundle</h3>
<p class="fragment">The field instance configuration (field_instance_config) is now an entity type</p>
<p class="fragment">entity_create() invokes the create method of the EntityStorageBase (abstract)</p>
<p class="fragment">The field_instance_config maps to class FieldInstanceConfig and uses FieldInstanceConfigStorage</p>
<p class="fragment">All other methods are in \Drupal\field\Entity\FieldInstanceConfig</p>
</section>
<section>
<h2>field_create_instance()</h2>
<p>D7</p>
<pre>
<code data-trim contenteditable>
// Note: Requires proper field / instance data array which you can get from a features export.
$field_instances = array();
// Exported field_instance: 'bean-field_sc_html'
$field_instances['bean-field_sc_html'] = array(
'bundle' => 'oms_sidebar_content',
'default_value' => NULL,
'deleted' => 0,
'description' => '',
'display' => array(
'default' => array(
'label' => 'above',
'settings' => array(),
'type' => 'hidden',
'weight' => 0,
),
),
'entity_type' => 'bean',
'field_name' => 'field_sc_html',
'label' => 'HTML',
'required' => 0,
'settings' => array(
'text_processing' => 1,
'user_register_form' => FALSE,
),
'widget' => array(
'active' => 1,
'module' => 'text',
'settings' => array(
'rows' => 5,
),
'type' => 'text_textarea',
'weight' => 2,
),
);
field_create_instance($field_instances);
</code>
</pre>
</section>
<section>
<h2>entity_create(<br>'field_instance_config', $values)</h2>
<p>D8</p>
<pre>
<code data-trim contenteditable>
$field_instance = entity_create('field_instance_config', array(
'field_name' => 'field_description',
'entity_type' => 'taxonomy_term',
'bundle' => 'tags',
'label' => 'Custom tag description',
));
$field_instance->save();
</code>
</pre>
</section>
<section>
<h2>save()</h2>
<h3>D7: field_update_field()</h3>
<h3>D7: field_update_instance()</h3>
<h3>update field, field instance configs</h3>
<pre>
<code data-trim contenteditable>
// Updating field.
$field->cardinality = 2;
$field->save();
// Deleting field.
entity_load('field_config', 'field_name')->delete();
// Deleting instance.
entity_load('field_instance_config', 'field_instance_name')->delete();
</code>
</pre>
</section>
</section>
<section>
<section>
<h2>Alters</h2>
<h3>Change the assumptions.</h3>
</section>
<section>
<h2>ModuleHandler::alter()</h2>
<h3>D7: drupal_alter()</h3>
<h3>passes variables to be changed</h3>
<pre>
<code data-trim contenteditable>
$data = array(
'key1' => 'value1',
'key2' => 'value2',
);
// Call all modules implementing hook_my_data_alter
\Drupal::moduleHandler()->alter('my_data', $data);
// Note: Use sparingly. hook_theme() is preferred for overriding styles.
</code>
</pre>
</section>
<section>
<h2>hook_form_alter()</h2>
<h3>change a form before rendered</h3>
<pre>
<code data-trim contenteditable>
function mymodule_form_alter(&$form, &$form_state) {
$form['new_checkbox'] = array(
'#type' => 'checkbox',
'#title' => t ('Subscribe'),
);
);
</code>
</pre>
</section>
<section>
<h2>hook_form_FORM_ID_alter()</h2>
<h3>change a specific form by FORM ID before rendered</h3>
<p class="fragment">getFormId() method defines id to alter</p>
</section>
<section>
<h2>hook_form_FORM_ID_alter()</h2>
<pre>
<code data-trim contenteditable>
/**
* @file
* Contains \Drupal\example\Form\ExampleForm.
*/
namespace Drupal\example\Form;
use Drupal\Core\Form\FormBase;
/**
* Implements an example form.
*/
class ExampleForm extends FormBase {
/**
* {@inheritdoc}.
*/
public function getFormId() {
return 'example_form';
}
}
</code>
</pre>
</section>
<section>
<h2>hook_form_FORM_ID_alter()</h2>
<pre>
<code data-trim contenteditable>
function mymodule_form_example_form_alter() {
...
);
</code>
</pre>
</section>
<section>
<h2>More Info on Form API</h2>
<p>https://drupal.org/node/2117411</p>
</section>
</section>
<section>
<section>
<h2>Hooks</h2>
<h3>Engage other processes</h3>
</section>
<section>
<h2>moduleHandler::invokeAll()</h2>
<h3>D7: module_invoke_all()</h3>
<h3>Invoke the hooks</h3>
<pre>
<code data-trim contenteditable>
\Drupal::moduleHandler->invokeAll($hook, $args);
</code>
</pre>
</section>
<section>
<h2>moduleHandler<br>::getImplementations()</h2>
<h3>D7: module_implements()</h3>
<h3>Check a module invokes the hook</h3>
<pre>
<code data-trim contenteditable>
\Drupal::moduleHandler->getImplementations($hook);
</code>
</pre>
</section>
</section>
<section>
<section>
<h2>URLs</h2>
<h3>Grab the $_GET</h3>
</section>
<section>
<h3>UrlHelper::filterQueryParameters()</h3>
<h3>D7: drupal_get_query_parameters()</h3>
<h4>process url query</h4>
<pre>
<code data-trim contenteditable>
use Drupal\Component\Utility\UrlHelper;
// URL: http://mysite.com?text=foo&date=2013-09-14
$url = \Drupal::request()->query->all(); // May not be best way
$param = UrlHelper::filterQueryParameters($url);
// Values
// $param['text'] = foo
// $param['date'] = 2013-09-14
</code>
</pre>
</section>
</section>
<section>
<section>
<h2>Paths</h2>
<h3>Where am I?</h3>
</section>
<section>
<h2>drupal_get_path()</h2>
<h3>returns path to a system item</h3>
<pre>
<code data-trim contenteditable>
// Path to a file
$path = drupal_get_path('theme', 'mytheme') . '/mycustom.js';
</code>
</pre>
</section>
<section>
<h2>User</h2>
<h3>Who am I?</h3>
<p>D7: global $user</p>
</section>
<section>
<h2>User</h2>
<h3>Who am I?</h3>
<p>D8: \Drupal::currentUser()</p>
<p class="fragment">https://drupal.org/node/2032447</p>
</section>
<section>
<h2>User</h2>
<h3>Who am I?</h3>
<pre>
<code data-trim contenteditable>
$account = \Drupal::currentUser();
if ($account->id() == 1) {
return "Hiya, boss!";
}
// $account is not an actual User object, but UserSession object
// Extends AccountInterface class
</code>
</pre>
</section>
</section>
<section>
<h2>moduleHandler->loadInclude()</h2>
<h3>D7: module_load_include()</h3>
<h3>loads a module include file</h3>
<pre>
<code data-trim contenteditable>
// Load node.admin.inc from the node module.
\Drupal::moduleHandler->loadInclude('node', 'inc', 'node.admin');
</code>
</pre>
</section>
<section>
<section>
<h2>Querying Entities</h2>
<h3>Join ALL the tables!</h3>
</section>
<section>
<h2>\Drupal::entityQuery()</h2>
<h3>D7: EntityFieldQuery</h3>
<h3>search a single entity</h3>
</section>
<section>
<h2>D7: EntityFieldQuery</h2>
<pre>
<code data-trim contenteditable>
// Query articles
$query = new EntityFieldQuery();
$query
->entityCondition('entity_type', 'node')
->entityCondition('bundle', 'article')
->propertyCondition('status', 1);
$result = $query->execute();
$nids = array_keys($result['node']);
$nodes = node_load_multiple($nids);
</code>
</pre>
<p class="fragment">EFQ presentation at http://bit.ly/top10efq</p>
</section>
<section>
<h2>\Drupal::entityQuery()</h2>
<pre>
<code data-trim contenteditable>
$query = \Drupal::entityQuery('taxonomy_term')
->condition('tid', $term->id())
->condition('vid', $term->bundle())
->range(0, 1)
->count()
->execute();
</code>
</pre>
<p class="fragment">https://drupal.org/node/1827278</p>
</section>
</section>
<section>
<section>
<h2>Was that 30?</h2>
<h3>Kind of didn't feel like it.</h3>
<p class="fragment">What about functions completely removed?</p>
</section>
<section>
<h2>#attached</h2>
<h3>D7: drupal_add_js, drupal_add_css, drupal_add_library</h3>
<p class="fragment">Gone!</p>
<p class="fragment">https://drupal.org/node/2169605</p>
</section>
<section>
<h2>#attached</h2>
<pre>
<code data-trim contenteditable>
// within CKEditor
public function settingsForm(array $form, array &$form_state, EditorEntity $editor) {
$ckeditor_settings_toolbar = array(
'#theme' => 'ckeditor_settings_toolbar',
'#editor' => $editor,
'#plugins' => $this->ckeditorPluginManager->getButtons(),
);
$form['toolbar'] = array(
'#type' => 'container',
'#attached' => array(
'library' => array('ckeditor/drupal.ckeditor.admin'),
'js' => array(
array(
'type' => 'setting',
'data' => array('ckeditor' => array(
'toolbarAdmin' => drupal_render($ckeditor_settings_toolbar),
)),
)
),
),
'#attributes' => array('class' => array('ckeditor-toolbar-configuration')),
);
</code>
</pre>
</section>
</section>
<section>
<section>
<h2>1 - 10</h2>
<ul>
<li>t()</li>
<li>String::checkPlain()</li>
<li>String::placeholder()</li>
<li>l()</li>
<li>drupal_render()</li>
<li><strike>node_load_multiple()</strike> entity_load_multiple()</li>
<li><strike>node_view_multiple()</strike> entity_view_multiple()</li>
<li><strike>node_save()</strike> entity_create()</li>
<li><strike>hook_menu()</strike> module.routing.yml</li>
<li><strike>menu_get_item()</strike> $request()->attributes->get()</li>
</ul>
</section>
<section>
<h2>11 - <strike>20</strike> 17</h2>
<ul>
<li><strike>menu_get_object()</strike> $request()->attributes->get()</li>
<li>menu_tree_page_data()</li>
<li>menu_tree_output()</li>
<li>taxonomy_get_tree()</li>
<li><strike>taxonomy_vocabulary_machine_name_load()</strike> entity_load('taxonomy_vocabulary', 'forums')</li>
<li><strike>field_info_field()</strike> $fieldInfo->getField()</li>
<li><strike>field_info_instance()</strike> $fieldInfo->getInstance()</li>
<li><strike>field_create_field()</strike> entity_create('field_config', $values)</li>
<li><strike>field_create_instance()</strike> entity_create('field_instance_config', $values)</li>
<li><strike>field_update_field()</strike> save()</li>
</ul>
</section>
<section>
<h2>18 - 28</h2>
<ul>
<li><strike>field_update_instance()</strike> save()</li>
<li><strike>drupal_alter()</strike> ModuleHandler::alter()</li>
<li>hook_form_alter()</li>
<li>hook_form_FORM_ID_alter()</li>
<li>moduleHandler::invokeAll()</li>
<li>moduleHandler::getImplementations()</li>
<li><strike>drupal_get_query_parameters()</strike> UrlHelper::filterQueryParameters()</li>
<li>drupal_get_path()</li>
<li><strike>module_load_include()</strike> moduleHandler->loadInclude()</li>
<li><strike>EntityFieldQuery</strike> \Drupal::entityQuery()</li>
</ul>
</section>
<section>
<h2>So, it wasn't 30</h2>
<p class="fragment">Technically, no.</p>
<p class="fragment">#attached could be a +3</p>
<p class="fragment"><strike>drupal_add_js, drupal_add_css, drupal_add_library</strike></p>
</section>
</section>
<section>
<section>
<h2>More Bonus!</h2>
<h3>arg() is gone!!!</h3>
<img src="https://zomgwtfbbqq.files.wordpress.com/2010/09/omgwtfbbq.jpg" height="400px"/>
<p class="fragment">https://drupal.org/node/2274705</p>
</section>
<section>
<h2>More Bonus!</h2>
<h3>\Drupal</h3>
<p class="fragment">Static service container wrapper</p>
<p class="fragment">A unified global accessor to arbitrary services</p>
<p class="fragment">Ex: hasRequest(), httpClient(), queue()</p>
</section>
</section>
<section>
<section>
<h2>What's Next?</h2>
<p class="fragment">Contribute to Examples: https://drupal.org/project/examples</p>
<p class="fragment">Read docs: https://drupal.org/developing/api/8</p>
<p class="fragment">Scaffold: https://github.com/hechoendrupal/DrupalAppConsole</p>
</section>
<section>
<h2>What's Next (at Drupalcon)?</h2>
<img src="https://drupal.org/files/issues/drupal-con-austin-sprint-slide.jpg" height="550px"/>
</section>
</section>
<section>
<h2>Questions?</h2>
<h3>By <a href="http://www.phase2technology.com/author/fmitchell/">Fredric Mitchell</a> / <a href="http://twitter.com/fredricmitchell">@fredricmitchell</a></h3>
<p><strong>Slides:</strong> http://bit.ly/d8func-austin</p>
<p><strong>Slides Repo:</strong> https://github.com/fmitchell/d8apifunc-austin</p>
</section>
</div>
</div>
<script src="lib/js/head.min.js"></script>
<script src="js/reveal.min.js"></script>
<script>
// Full list of configuration options available here:
// https://github.com/hakimel/reveal.js#configuration
Reveal.initialize({
controls: true,
progress: true,
history: true,
center: true,
theme: Reveal.getQueryHash().theme, // available themes are in /css/theme
transition: Reveal.getQueryHash().transition || 'default', // default/cube/page/concave/zoom/linear/fade/none
// Optional libraries used to extend on reveal.js
dependencies: [
{ src: 'lib/js/classList.js', condition: function() { return !document.body.classList; } },
{ src: 'plugin/markdown/marked.js', condition: function() { return !!document.querySelector( '[data-markdown]' ); } },
{ src: 'plugin/markdown/markdown.js', condition: function() { return !!document.querySelector( '[data-markdown]' ); } },
{ src: 'plugin/highlight/highlight.js', async: true, callback: function() { hljs.initHighlightingOnLoad(); } },
{ src: 'plugin/zoom-js/zoom.js', async: true, condition: function() { return !!document.body.classList; } },
{ src: 'plugin/notes/notes.js', async: true, condition: function() { return !!document.body.classList; } },
{ src: 'phase2/branding.js', async: true, condition: function() {return !!document.body.classList; }}
]