SimplePageVariant.php 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. <?php
  2. namespace Drupal\Core\Render\Plugin\DisplayVariant;
  3. use Drupal\Core\Display\PageVariantInterface;
  4. use Drupal\Core\Display\VariantBase;
  5. /**
  6. * Provides a page display variant that simply renders the main content.
  7. *
  8. * @PageDisplayVariant(
  9. * id = "simple_page",
  10. * admin_label = @Translation("Simple page")
  11. * )
  12. */
  13. class SimplePageVariant extends VariantBase implements PageVariantInterface {
  14. /**
  15. * The render array representing the main content.
  16. *
  17. * @var array
  18. */
  19. protected $mainContent;
  20. /**
  21. * The page title: a string (plain title) or a render array (formatted title).
  22. *
  23. * @var string|array
  24. */
  25. protected $title = '';
  26. /**
  27. * {@inheritdoc}
  28. */
  29. public function setMainContent(array $main_content) {
  30. $this->mainContent = $main_content;
  31. return $this;
  32. }
  33. /**
  34. * {@inheritdoc}
  35. */
  36. public function setTitle($title) {
  37. $this->title = $title;
  38. return $this;
  39. }
  40. /**
  41. * {@inheritdoc}
  42. */
  43. public function build() {
  44. $build = [
  45. 'content' => [
  46. 'messages' => [
  47. '#type' => 'status_messages',
  48. '#weight' => -1000,
  49. '#include_fallback' => TRUE,
  50. ],
  51. 'page_title' => [
  52. '#type' => 'page_title',
  53. '#title' => $this->title,
  54. '#weight' => -900,
  55. ],
  56. 'main_content' => ['#weight' => -800] + $this->mainContent,
  57. ],
  58. ];
  59. return $build;
  60. }
  61. }