nortmas
9/27/2017 - 2:55 PM

Render arrays

/*
Common values for #theme

item_list
  (array) items
  (string) title
  (string) list_type UL or OL
link
  (string) title
  (string) URL, or internal path
  links
  (array) links
table
  (string) header
  (array) rows
  (array) colgroups
  (boolean) sticky
  (boolean) responsive
  (string) empty
time
  (int) timestamp
  (string) text
*/

return array(
  '#markup' => $this->t('Hello, World!'),
  '#prefix' => '<blockquote>',
  '#suffix' => '</blockquote>',
  '#attached' => [
    'library' => ['retro/retro']
  ],
);

return array(
  '#plain_text' => $this->t('Hello world!)
);

return array(
  '#type' => 'link',
  '#title' => 'About',
  '#url' => 'about',
);

return array(
  '#theme' = 'item_list',
  '#title' => Drupal::t('A list of items'),
  '#items' => array(
    array('value' => Drupal::t('Item one')),
    array('value' => Drupal::t('Item two'), 'attributes' => array('class' => 'blue')),
  ),
  '#attributes' => array('class' => array('my-item-list')),
);

return [
  '#type' => 'table',
  '#caption' => $this->t('Our favorite colors.'),
  '#header' => [$this->t('Name'), $this->t('Favorite color')],
  '#rows' => [
    [$this->t('Amber'), $this->t('teal')],
    [$this->t('Addi'), $this->t('green')],
    [$this->t('Blake'), $this->t('#063')],
    [$this->t('Enid'), $this->t('indigo')],
    [$this->t('Joe'), $this->t('green')],
  ],
  '#description' => $this->t('Example of using #type.'),
];

return [
  '#theme' => 'node',
  ...
  '#attributes' => [
    'data-node-id' => $node->id(),
  ],
  '#content_attributes' => [
    'class' => ['custom-node-class'],
    'id' => 'node-' . $node->id() . '-content',
  ],
];