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:
The code
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 |
<?php namespace Drupal\your_module\Controller; use Drupal; use Drupal\Core\Controller\ControllerBase; use Drupal\Core\Url; class YourModuleController extends ControllerBase { protected $database; function content() { // Render array to return $mainRenderArray = []; // admin_page style (half width) $mainRenderArray[] = [ '#theme' => '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:
1 2 3 4 5 6 7 8 |
(render array base) │ ├── admin_page │ └── admin_block <== Half width │ └── admin_block_content │ └── admin_block <== Full width └── admin_block_content |