[Drupal] Use admin_block and admin_block_content templates

This article is about the default admin_page, admin_block and admin_block_content templates which serves as the base of the content display on some system configuration pages such as the one on this image:

Screenshot of the configuration page on a Drupal 8 website
Screenshot of the configuration page on a Drupal 8 website

The code

 'admin_page',
      '#blocks' => [
        // Array of admin_block
        [
          'title' => $this->t('A block, on the left'),
          'position' => 'left',
          'content' => [
            '#theme' => 'admin_block_content',
            '#content' => [
              [
                'title' => 'Do something',
                'url' => Url::fromRoute('your_module.some.example.route'),
                'description' => $this->t('This will link to a route.')
              ],
              [
                'title' => 'Navigate to a nice blog',
                'url' => Url::fromUri('https://blog.dakwamine.fr'),
                'description' => $this->t('This will link to a URI, external in this case.')
              ],
              [
                'title' => 'Go to contents page',
                'url' => Url::fromUri('internal:/admin/content'),
                'description' => $this->t('Also works for internal URI.')
              ]
            ]
          ]
        ],
        [
          'title' => $this->t('Another block, on the right'),
          'position' => 'right',
          'content' => [
            '#theme' => 'admin_block_content',
            '#content' => [
              [
                'title' => 'Another link to a route',
                'url' => Url::fromRoute('your_module.another.route'),
                'description' => $this->t('Yup.')
              ],
              [
                'title' => 'Website example link',
                'url' => Url::fromUri('https://website.example'),
                'description' => $this->t('Goes on website.example.')
              ],
            ]
          ]
        ]
      ],
    ];

    // admin_block style (full width)
    $mainRenderArray[] = [
      '#theme' => 'admin_block',
      '#block' => [
        'title' => $this->t('Full width block'),
        'content' => [
          '#theme' => 'admin_block_content',
          '#content' => [
            [
              'title' => 'Another link',
              'url' => Url::fromUri('internal:/admin/people'),
              'description' => $this->t('A link to the people page.')
            ],
            [
              'title' => 'Another link again',
              'url' => Url::fromUri('https://website.example'),
              'description' => $this->t('Goes on website.example.')
            ],
          ]
        ],
      ],
    ];

    return $mainRenderArray;
  }

}

Explanation

If you use admin_page, you will be able to define the column in which the blocks dwell: left or right.

If you use directly admin_block, there is only a single block and it will span over the full width of the region.

So all in all, here is a pseudo hierarchy to better understand how it works:

(render array base)
│
├── admin_page
│   └── admin_block  <== Half width
│       └── admin_block_content
│
└── admin_block  <== Full width
    └── admin_block_content

Published by Dakwamine

Dakwamine, alias Quang-Minh Dang, né en 1987 en région parisienne. Un type sympa, pas bavard et pragmatique.