The Drupal 8 cycle has entered the API freeze since the 1st of July, which means it’s time to start porting modules or simply play around with the API. While there are exceptions to change the API, you can safely assume that 95% (or even more) will remain as it is today.
This is the first article which will be part of a series of changes in Field API for Drupal 8: field formatters. In case there are updates to the API, we will update the articles, but also the change records on drupal.org. Watch out for the next couple of weeks and months and hopefully you’re prepared for Drupal 8 and Field API.
Plugins
Creating field formatters in Drupal 7 was done by implementing four hooks. In Drupal 8, formatters are now plugins using the new Plugin API. Hooks are replaced by methods in classes, which means that your module file will be empty if you only provide a formatter (unless you also implement one of the field formatter alter hooks). Being classes, this means that field formatters can now extend on each other. A good example in core is the image field formatter extending the file field formatter class. Discovery and class instantiation is managed by the new formatter plugin manager.
Create a file like ‘{your_module}/lib/Drupal/{your_module}/Plugin/field/FieldFormatter/{NameOfYourFormatter}.php. That’s a lot of directories right ? Welcome to the world of PSR-0, namespaces and plugins in D8. This is most likely going to change, feel free to read, or even help along in https://drupal.org/node/1971198. Also, plugin managers can now control where plugins reside, see https://drupal.org/node/2043379, so we’ll probably change this at some point.
In most cases, you will want to extend the FormatterBase class which does most of the heavy lifting for you. Following classes will usually be imported at the top of your file:
<?php
// FormatterBase class.
Drupal\Core\Field\FormatterBase;
// FieldItemInterface
use Drupal\Core\Field\FieldItemListInterface;
?>
1. hook_field_formatter_info() are now annotations
hook_field_formatter_info() is replaced by annotation-based plugin discovery, using the \Drupal\field\Annotation\FieldFormatter annotation class. As for other plugin types, the accepted properties are documented in the annotation class. Other modules can extend this by implementing hook_field_formatter_info_alter(). In core, the edit module adds the edit property so it knows which in-place editor it has to use. Note that some property names have changed since Drupal 7 (spaces replaces by underscores). This is how an annotation looks like, which is placed right above the class
keyword.
<?php
/**
* Plugin implementation of the 'foo_formatter' formatter
*
* @FieldFormatter(
* id = "foo_formatter",
* label = @Translation("Foo formatter"),
* field_types = {
* "text",
* "text_long"
* },
* settings = {
* "trim_length" = "600",
* },
* edit = {
* "editor" = "form"
* }
* )
*/
class FooFormatter extends FormatterBase { }
?>
2. hook_field_formatter_settings_form() becomes FormatterInterface::settingsForm()
Next up is to create a settingsForm() method. If you have an old settings form, you can simply move the code to this method. Settings are automatically saved and can be accessed by calling $this->getSetting('settings_key');
. Remember to always start with an empty $elements array and not with the $form argument from the function arguments.
<?php
/**
* {@inheritdoc}
*/
public function settingsForm(array $form, array &$form_state) {
$element = array();
$element[‘trim_length’] = array(‘#title’ => t(‘Trim length’),
‘#type’ => ‘number’,
‘#default_value’ => $this->getSetting(‘trim_length’),
‘#min’ => 1,
‘#required’ => TRUE,
);
return $element;
}
?>
3. hook_field_formatter_settings_summary() becomes FormatterInterface::settingsSummary()
Settings are accessed by calling $this->getSetting('settings_key');
. Another change is that the summary now needs to return an array instead of a string.
<?php
/**
* {@inheritdoc}
*/
public function settingsSummary() {
$summary = array();
$summary[] = t('Trim length: @trim_length', array('@trim_length' => $this->getSetting('trim_length')));
return $summary;
}
?>
Read More 1501
Read More 1502
Read More 1503
Read More 1504
Read More 1505
Read More 1506
Read More 1507
Read More 1508
Read More 1509
Read More 1510
Read More 1511
Read More 1512
Read More 1513
Read More 1514
Read More 1515
Read More 1516
Read More 1517
Read More 1518
Read More 1519
Read More 1520
Read More 1521
Read More 1522
Read More 1523
Read More 1524
Read More 1525
Read More 1526
Read More 1527
Read More 1528
Read More 1529
Read More 1530
Read More 1531
Read More 1532
Read More 1533
Read More 1534
Read More 1535
Read More 1536
Read More 1537
Read More 1538
Read More 1539
Read More 1540
Read More 1541
Read More 1542
Read More 1543
Read More 1544
Read More 1545
Read More 1546
Read More 1547
Read More 1548
Read More 1549
Read More 1550
Read More 1551
Read More 1552
Read More 1553
Read More 1554
Read More 1555
Read More 1556
Read More 1557
Read More 1558
Read More 1559
Read More 1560
Read More 1561
Read More 1562
Read More 1563
Read More 1564
Read More 1565
Read More 1566
Read More 1567
Read More 1568
Read More 1569
Read More 1570
Read More 1571
Read More 1572
Read More 1573
Read More 1574
Read More 1575
Read More 1576
Read More 1577
Read More 1578
Read More 1579
Read More 1580
Read More 1581
Read More 1582
Read More 1583
Read More 1584
Read More 1585
Read More 1586
Read More 1587
Read More 1588
Read More 1589
Read More 1590
Read More 1591
Read More 1592
Read More 1593
Read More 1594
Read More 1595
Read More 1596
Read More 1597
Read More 1598
Read More 1599
Read More 1600
Read More 1601
Read More 1602
Read More 1603
Read More 1604
Read More 1605
Read More 1606
Read More 1607
Read More 1608
Read More 1609
Read More 1610
Read More 1611
Read More 1612
Read More 1613
Read More 1614
Read More 1615
Read More 1616
Read More 1617
Read More 1618
Read More 1619
Read More 1620
Read More 1621
Read More 1622
Read More 1623
Read More 1624
Read More 1625
Read More 1626
Read More 1627
Read More 1628
Read More 1629
Read More 1630
Read More 1631
Read More 1632
Read More 1633
Read More 1634
Read More 1635
Read More 1636
Read More 1637
Read More 1638
Read More 1639
Read More 1640
Read More 1641
Read More 1642
Read More 1643
Read More 1644
Read More 1645
Read More 1646
Read More 1647
Read More 1648
Read More 1649
Read More 1650
Read More 1651
Read More 1652
Read More 1653
Read More 1654
Read More 1655
Read More 1656
Read More 1657
Read More 1658
Read More 1659
Read More 1660
Read More 1661
Read More 1662
Read More 1663
Read More 1664
Read More 1665
Read More 1666
Read More 1667
Read More 1668
Read More 1669
Read More 1670
Read More 1671
Read More 1672
Read More 1673
Read More 1674
Read More 1675
Read More 1676
Read More 1677
Read More 1678
Read More 1679
Read More 1680
Read More 1681
Read More 1682
Read More 1683
Read More 1684
Read More 1685
Read More 1686
Read More 1687
Read More 1688
Read More 1689
Read More 1690
Read More 1691
Read More 1692
Read More 1693
Read More 1694
Read More 1695
Read More 1696
Read More 1697
Read More 1698
Read More 1699
Read More 1700
Read More 1701
Read More 1702
Read More 1703
Read More 1704
Read More 1705
Read More 1706
Read More 1707
Read More 1708
Read More 1709
Read More 1710
Read More 1711
Read More 1712
Read More 1713
Read More 1714
Read More 1715
Read More 1716
Read More 1717
Read More 1718
Read More 1719
Read More 1720
Read More 1721
Read More 1722
Read More 1723
Read More 1724
Read More 1725
Read More 1726
Read More 1727
Read More 1728
Read More 1729
Read More 1730
Read More 1731
Read More 1732
Read More 1733
Read More 1734
Read More 1735
Read More 1736
Read More 1737
Read More 1738
Read More 1739
Read More 1740
Read More 1741
Read More 1742
Read More 1743
Read More 1744
Read More 1745
Read More 1746
Read More 1747
Read More 1748
Read More 1749
Read More 1750
Read More 1751
Read More 1752
Read More 1753
Read More 1754
Read More 1755
Read More 1756
Read More 1757
Read More 1758
Read More 1759
Read More 1760
Read More 1761
Read More 1762
Read More 1763
Read More 1764
Read More 1765
Read More 1766
Read More 1767
Read More 1768
Read More 1769
Read More 1770
Read More 1771
Read More 1772
Read More 1773
Read More 1774
Read More 1775
Read More 1776
Read More 1777
Read More 1778
Read More 1779
Read More 1780
Read More 1781
Read More 1782
Read More 1783
Read More 1784
Read More 1785
Read More 1786
Read More 1787
Read More 1788
Read More 1789
Read More 1790
Read More 1791
Read More 1792
Read More 1793
Read More 1794
Read More 1795
Read More 1796
Read More 1797
Read More 1798
Read More 1799
Read More 1800
Read More 1801
Read More 1802
Read More 1803
Read More 1804
Read More 1805
Read More 1806
Read More 1807
Read More 1808
Read More 1809
Read More 1810
Read More 1811
Read More 1812
Read More 1813
Read More 1814
Read More 1815
Read More 1816
Read More 1817
Read More 1818
Read More 1819
Read More 1820
Read More 1821
Read More 1822
Read More 1823
Read More 1824
Read More 1825
Read More 1826
Read More 1827
Read More 1828
Read More 1829
Read More 1830
Read More 1831
Read More 1832
Read More 1833
Read More 1834
Read More 1835
Read More 1836
Read More 1837
Read More 1838
Read More 1839
Read More 1840
Read More 1841
Read More 1842
Read More 1843
Read More 1844
Read More 1845
Read More 1846
Read More 1847
Read More 1848
Read More 1849
Read More 1850
Read More 1851
Read More 1852
Read More 1853
Read More 1854
Read More 1855
Read More 1856
Read More 1857
Read More 1858
Read More 1859
Read More 1860
Read More 1861
Read More 1862
Read More 1863
Read More 1864
Read More 1865
Read More 1866
Read More 1867
Read More 1868
Read More 1869
Read More 1870
Read More 1871
Read More 1872
Read More 1873
Read More 1874
Read More 1875
Read More 1876
Read More 1877
Read More 1878
Read More 1879
Read More 1880
Read More 1881
Read More 1882
Read More 1883
Read More 1884
Read More 1885
Read More 1886
Read More 1887
Read More 1888
Read More 1889
Read More 1890
Read More 1891
Read More 1892
Read More 1893
Read More 1894
Read More 1895
Read More 1896
Read More 1897
Read More 1898
Read More 1899
Read More 1900
Read More 1901
Read More 1902
Read More 1903
Read More 1904
Read More 1905
Read More 1906
Read More 1907
Read More 1908
Read More 1909
Read More 1910
Read More 1911
Read More 1912
Read More 1913
Read More 1914
Read More 1915
Read More 1916
Read More 1917
Read More 1918
Read More 1919
Read More 1920
Read More 1921
Read More 1922
Read More 1923
Read More 1924
Read More 1925
Read More 1926
Read More 1927
Read More 1928
Read More 1929
Read More 1930
Read More 1931
Read More 1932
Read More 1933
Read More 1934
Read More 1935
Read More 1936
Read More 1937
Read More 1938
Read More 1939
Read More 1940
Read More 1941
Read More 1942
Read More 1943
Read More 1944
Read More 1945
Read More 1946
Read More 1947
Read More 1948
Read More 1949
Read More 1950
Read More 1951
Read More 1952
Read More 1953
Read More 1954
Read More 1955
Read More 1956
Read More 1957
Read More 1958
Read More 1959
Read More 1960
Read More 1961
Read More 1962
Read More 1963
Read More 1964
Read More 1965
Read More 1966
Read More 1967
Read More 1968
Read More 1969
Read More 1970
Read More 1971
Read More 1972
Read More 1973
Read More 1974
Read More 1975
Read More 1976
Read More 1977
Read More 1978
Read More 1979
Read More 1980
Read More 1981
Read More 1982
Read More 1983
Read More 1984
Read More 1985
Read More 1986
Read More 1987
Read More 1988
Read More 1989
Read More 1990
Read More 1991
Read More 1992
Read More 1993
Read More 1994
Read More 1995
Read More 1996
Read More 1997
Read More 1998
Read More 1999
Read More 2000