This is part 4 of the Drupal 8 Field API series. We skipped part 3 about field types as the dust hasn’t settled yet in terms of API changes. Once we get there, we’ll make sure we’ll talk about the massive changes that have happened in that area.
In this part, we’ll talk about new API’s which are, strictly speaking, not part anymore of Field API, they now belong to the Entity Field API. However, the reason the changes exist, directly comes out of a big problem in Drupal 7 in Field API: scattered storage.
Entity display
In Drupal 7, parameters controlling how to render the various “parts” of an entity view in a given view mode were scattered in many separate locations:
- in $instance[‘display’] for each individual (Field API) field in the bundle
- in the ‘field_bundle_settings_[entity_type]_[bundle]’ variable for the ‘extra fields’ in the bundle
- in other places for contrib additions that are not one of the two above (most notably field_group module)
In Drupal 8, all those parameters (“options”) for all those parts (“components”) are centralised in one single EntityViewDisplay configuration object – which means they can be deployed with CMI – for each entity bundle and view mode. You can access these objects with the entity_get_display() function. All available methods can be found in EntityDisplayInterface.
<?php
entity_get_display('node', 'article', 'default')
// Set 'component_1' to be visible, with weight 1.
->setComponent('component_1', array(
'weight' => 1,
))
// Set 'component_2' to be hidden.
->removeComponent('component_2')
->save();
?>
Also new in D8 is that all entity and field rendering hooks now receive this EntityDisplay object as a parameter. This means you have direct access to the configuration in your functions, instead of having to figure them out by calling functions to get the definition of field instances, extra fields or other configurations. For the full list of all hooks affected, scroll to the resources part at then end of this article for the change record link.
<?php
function hook_node_view(Node $node, EntityDisplay $display, $view_mode, $langcode) {
if ($display->getComponent('mymodule_addition')) {
$node->content['mymodule_addition'] = array(
'#markup' => mymodule_addition($node),
'#theme' => 'mymodule_my_additional_field',
);
}
}
?>
Note that currently only Field API fields and extra fields are handled by the EntityViewDisplay object. This is going to change when we introduce component type plugins so everyone will be able to store and access configuration. For more information, follow https://drupal.org/node/1875974.
Entity form display
Just like with the display settings, form settings are scattered all over the place:
- in $instance[‘widget’] for each individual (Field API) field in the bundle
- in the ‘field_bundle_settings_[entity_type]_[bundle]’ variable for the ‘extra fields’ in the bundle
- in other places for contrib additions that are not one of the two above (most notably field_group module)
In Drupal 8, all those parameters (“options”) for all those parts (“components”) are centralised in one single EntityFormDisplay configuration object for each entity bundle and form mode.This object uses the same EntityDisplayInterface as the EntityViewDisplay object. The consequence of this API is the birth of the counterpart of view modes: form modes.
The direct use case for this was the hacky workaround in Drupal 7 for showing Field API fields on the user registration page. In Drupal 8, you can now configure which fields are available on the user registration page or the edit page. However, more interesting possibilities with this new API will certainly pop up when Drupal 8 is released, most notable inline entity forms.
Read More 501
Read More 502
Read More 503
Read More 504
Read More 505
Read More 506
Read More 507
Read More 508
Read More 509
Read More 510
Read More 511
Read More 512
Read More 513
Read More 514
Read More 515
Read More 516
Read More 517
Read More 518
Read More 519
Read More 520
Read More 521
Read More 522
Read More 523
Read More 524
Read More 525
Read More 526
Read More 527
Read More 528
Read More 529
Read More 530
Read More 531
Read More 532
Read More 533
Read More 534
Read More 535
Read More 536
Read More 537
Read More 538
Read More 539
Read More 540
Read More 541
Read More 542
Read More 543
Read More 544
Read More 545
Read More 546
Read More 547
Read More 548
Read More 549
Read More 550
Read More 551
Read More 552
Read More 553
Read More 554
Read More 555
Read More 556
Read More 557
Read More 558
Read More 559
Read More 560
Read More 561
Read More 562
Read More 563
Read More 564
Read More 565
Read More 566
Read More 567
Read More 568
Read More 569
Read More 570
Read More 571
Read More 572
Read More 573
Read More 574
Read More 575
Read More 576
Read More 577
Read More 578
Read More 579
Read More 580
Read More 581
Read More 582
Read More 583
Read More 584
Read More 585
Read More 586
Read More 587
Read More 588
Read More 589
Read More 590
Read More 591
Read More 592
Read More 593
Read More 594
Read More 595
Read More 596
Read More 597
Read More 598
Read More 599
Read More 600
Read More 601
Read More 602
Read More 603
Read More 604
Read More 605
Read More 606
Read More 607
Read More 608
Read More 609
Read More 610
Read More 611
Read More 612
Read More 613
Read More 614
Read More 615
Read More 616
Read More 617
Read More 618
Read More 619
Read More 620
Read More 621
Read More 622
Read More 623
Read More 624
Read More 625
Read More 626
Read More 627
Read More 628
Read More 629
Read More 630
Read More 631
Read More 632
Read More 633
Read More 634
Read More 635
Read More 636
Read More 637
Read More 638
Read More 639
Read More 640
Read More 641
Read More 642
Read More 643
Read More 644
Read More 645
Read More 646
Read More 647
Read More 648
Read More 649
Read More 650
Read More 651
Read More 652
Read More 653
Read More 654
Read More 655
Read More 656
Read More 657
Read More 658
Read More 659
Read More 660
Read More 661
Read More 662
Read More 663
Read More 664
Read More 665
Read More 666
Read More 667
Read More 668
Read More 669
Read More 670
Read More 671
Read More 672
Read More 673
Read More 674
Read More 675
Read More 676
Read More 677
Read More 678
Read More 679
Read More 680
Read More 681
Read More 682
Read More 683
Read More 684
Read More 685
Read More 686
Read More 687
Read More 688
Read More 689
Read More 690
Read More 691
Read More 692
Read More 693
Read More 694
Read More 695
Read More 696
Read More 697
Read More 698
Read More 699
Read More 700
Read More 701
Read More 702
Read More 703
Read More 704
Read More 705
Read More 706
Read More 707
Read More 708
Read More 709
Read More 710
Read More 711
Read More 712
Read More 713
Read More 714
Read More 715
Read More 716
Read More 717
Read More 718
Read More 719
Read More 720
Read More 721
Read More 722
Read More 723
Read More 724
Read More 725
Read More 726
Read More 727
Read More 728
Read More 729
Read More 730
Read More 731
Read More 732
Read More 733
Read More 734
Read More 735
Read More 736
Read More 737
Read More 738
Read More 739
Read More 740
Read More 741
Read More 742
Read More 743
Read More 744
Read More 745
Read More 746
Read More 747
Read More 748
Read More 749
Read More 750
Read More 751
Read More 752
Read More 753
Read More 754
Read More 755
Read More 756
Read More 757
Read More 758
Read More 759
Read More 760
Read More 761
Read More 762
Read More 763
Read More 764
Read More 765
Read More 766
Read More 767
Read More 768
Read More 769
Read More 770
Read More 771
Read More 772
Read More 773
Read More 774
Read More 775
Read More 776
Read More 777
Read More 778
Read More 779
Read More 780
Read More 781
Read More 782
Read More 783
Read More 784
Read More 785
Read More 786
Read More 787
Read More 788
Read More 789
Read More 790
Read More 791
Read More 792
Read More 793
Read More 794
Read More 795
Read More 796
Read More 797
Read More 798
Read More 799
Read More 800
Read More 801
Read More 802
Read More 803
Read More 804
Read More 805
Read More 806
Read More 807
Read More 808
Read More 809
Read More 810
Read More 811
Read More 812
Read More 813
Read More 814
Read More 815
Read More 816
Read More 817
Read More 818
Read More 819
Read More 820
Read More 821
Read More 822
Read More 823
Read More 824
Read More 825
Read More 826
Read More 827
Read More 828
Read More 829
Read More 830
Read More 831
Read More 832
Read More 833
Read More 834
Read More 835
Read More 836
Read More 837
Read More 838
Read More 839
Read More 840
Read More 841
Read More 842
Read More 843
Read More 844
Read More 845
Read More 846
Read More 847
Read More 848
Read More 849
Read More 850
Read More 851
Read More 852
Read More 853
Read More 854
Read More 855
Read More 856
Read More 857
Read More 858
Read More 859
Read More 860
Read More 861
Read More 862
Read More 863
Read More 864
Read More 865
Read More 866
Read More 867
Read More 868
Read More 869
Read More 870
Read More 871
Read More 872
Read More 873
Read More 874
Read More 875
Read More 876
Read More 877
Read More 878
Read More 879
Read More 880
Read More 881
Read More 882
Read More 883
Read More 884
Read More 885
Read More 886
Read More 887
Read More 888
Read More 889
Read More 890
Read More 891
Read More 892
Read More 893
Read More 894
Read More 895
Read More 896
Read More 897
Read More 898
Read More 899
Read More 900
Read More 901
Read More 902
Read More 903
Read More 904
Read More 905
Read More 906
Read More 907
Read More 908
Read More 909
Read More 910
Read More 911
Read More 912
Read More 913
Read More 914
Read More 915
Read More 916
Read More 917
Read More 918
Read More 919
Read More 920
Read More 921
Read More 922
Read More 923
Read More 924
Read More 925
Read More 926
Read More 927
Read More 928
Read More 929
Read More 930
Read More 931
Read More 932
Read More 933
Read More 934
Read More 935
Read More 936
Read More 937
Read More 938
Read More 939
Read More 940
Read More 941
Read More 942
Read More 943
Read More 944
Read More 945
Read More 946
Read More 947
Read More 948
Read More 949
Read More 950
Read More 951
Read More 952
Read More 953
Read More 954
Read More 955
Read More 956
Read More 957
Read More 958
Read More 959
Read More 960
Read More 961
Read More 962
Read More 963
Read More 964
Read More 965
Read More 966
Read More 967
Read More 968
Read More 969
Read More 970
Read More 971
Read More 972
Read More 973
Read More 974
Read More 975
Read More 976
Read More 977
Read More 978
Read More 979
Read More 980
Read More 981
Read More 982
Read More 983
Read More 984
Read More 985
Read More 986
Read More 987
Read More 988
Read More 989
Read More 990
Read More 991
Read More 992
Read More 993
Read More 994
Read More 995
Read More 996
Read More 997
Read More 998
Read More 999
Read More 1000