How to programmatically add links to the links variable

Public

How to programmatically add links to the links variable.

</> CopyGet raw version
php
  1. function example_node_view_alter(&$build) {
  2. // Remove the read more link
  3. unset($build['links']['node']['#links']['node-readmore']);
  4.  
  5. // Add your own custom link
  6. $build['links']['node']['#links']['example-mylink'] = array(
  7. 'title' => t('Test link'),
  8. 'href' => 'foo',
  9. 'html' => TRUE,
  10. 'attributes' => array(
  11. 'title' => 'Test link',
  12. ),
  13. );
  14.  
  15. // Move read more link to first slot
  16. $link_read_more = $build['links']['node']['#links']['node_read_more'];
  17. unset($build['links']['node']['#links']['node_read_more']);
  18. $links = $build['links']['node']['#links'];
  19. $build['links']['node']['#links'] = array(
  20. 'node_read_more' => $link_read_more,
  21. ) + $links;
  22.  
  23. // Move link to the last slot
  24. $link_read_more = $build['links']['node']['#links']['node_read_more'];
  25. unset($build['links']['node']['#links']['node_read_more']);
  26. $build['links']['node']['#links']['node_read_more'] = $link_read_more;
  27. }