Uncategorized

Blocks and Forms

Drupal 8 blocks

A cool new change to the block API in D8 has been a switch to making blocks more prominent, by making them plugins (a brand new concept). What this means is that they are reusable pieces of functionality (under the hood) as you can now create a block in the UI and reuse it across the site – you are no longer limited to using a block only one time.

Let’s go ahead and create a simple block type that prints to the screen Hello World! by default. All we need to work with is one class file located in the src/Plugin/Block folder of our module’s root directory. Let’s call our new block type DemoBlock, and naturally it needs to reside in a file called DemoBlock.php. Inside this file, we can start with the following:

<?php

namespace Drupal\demo\Plugin\Block;

use Drupal\block\BlockBase;
use Drupal\Core\Session\AccountInterface;

/**
 * Provides a 'Demo' block.
 *
 * @Block(
 *   id = "demo_block",
 *   admin_label = @Translation("Demo block"),
 * )
 */

class DemoBlock extends BlockBase {
  
  /**
   * {@inheritdoc}
   */
  public function build() {    
    return array(
      '#markup' => $this->t('Hello World!'),
    );
  }
  
  /**
   * {@inheritdoc}
   */
  public function access(AccountInterface $account) {
    return $account->hasPermission('access content');
  }  
  
}

Like with all other class files we start by namespacing our class. Then we use the BlockBase class so that we can extend it, as well as the AccountInterface class so that we can get access to the currently logged in user. Then follows something you definitely have not seen in Drupal 7: annotations.

Annotations are a PHP discovery tool located in the comment block of the same file as the class definition. Using these annotations we let Drupal know that we want to register a new block type (@Block) with the id of demo_block and the admin_label of Demo block (passed through the translation system).

Next, we extend the BlockBase class into our own DemoBlock, inside of which we implement two methods (the most common ones you’ll implement). The build() method is the most important as it returns a renderable array the block will print out. The access() method controls access rights for viewing this block. The parameter passed to it is an instance of the AccountInterface class which will be in this case the current user.

Another interesting thing to note is that we are no longer using the t() function globally for translation but we reference the t() method implemented in the class parent.

And that’s it, you can clear the caches and go to the Block layout configuration page. The cool thing is that you have the block types on the right (that you can filter through) and you can place one or more blocks of those types to various regions on the site.

Drupal 8 block configuration

Now that we’ve seen how to create a new block type to use from the UI, let’s tap further into the API and add a configuration form for it. We will make it so that you can edit the block, specify a name in a textfield and then the block will say hello to that name rather than the world.

 

 

 

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

Related Articles

Leave a Reply

Back to top button