Advertisement
Guest User

Untitled

a guest
Sep 26th, 2016
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.60 KB | None | 0 0
  1. <?php
  2. /**
  3. * @file
  4. * Displays children pages as a block
  5. */
  6.  
  7. namespace Drupalchild_pages_displayPluginBlock;
  8. use DrupalCoreBlockBlockBase;
  9. use DrupalCoreUrl;
  10. use DrupalCorePath;
  11. use DrupalCoreMenu;
  12. /**
  13. * Provides a 'Next Previous' block.
  14. *
  15. * @Block(
  16. * id = "child_page_block",
  17. * admin_label = @Translation("Child Page Block"),
  18. * category = @Translation("Blocks")
  19. * )
  20. */
  21. class ChildPageBlock extends BlockBase {
  22. public function build(){
  23. $menu_tree = Drupal::menuTree();
  24. $menu_name = 'main';
  25.  
  26. // Build the typical default set of menu tree parameters.
  27. $parameters = $menu_tree->getCurrentRouteMenuTreeParameters($menu_name);
  28. $expandedParents = $parameters->expandedParents;
  29. reset($expandedParents);
  30. $root = current($expandedParents);
  31. $parameters = new DrupalCoreMenuMenuTreeParameters();
  32. $parameters->setRoot($root);
  33. $parameters->setMaxDepth(3);
  34. $parameters->setMinDepth(1);
  35.  
  36. // Load the tree based on this set of parameters.
  37. $tree = $menu_tree->load($menu_name, $parameters);
  38.  
  39. //Set Cache for block
  40. $cache['max-age'] = 3600;
  41. $cache['contexts'][] = 'url.path';
  42. $cache['tags'][] = $root;
  43.  
  44. //Get node IDs from menu
  45. $nids = array();
  46. foreach($tree as $item){
  47. $nids[] = $item->link->getUrlObject()->getRouteParameters()['node'];
  48. }
  49.  
  50. //Load nodes & generate content from nodes
  51. $nodes = (!empty($nids)) ? entity_load_multiple('node', $nids) : array();
  52. if (!empty($nodes) && is_array($nodes)){
  53. foreach($nodes as $key=>$val) {
  54. $nodeContent = array(
  55. '#prefix' => '<div class="block-child-page-menu">',
  56. '#suffix' => '</div>'
  57. );
  58. $url = Drupal::service('path.alias_manager')->getAliasByPath('/node/'.$key);
  59.  
  60. //Get image thumbnail from content & use specified image style
  61. if(null !== $val->get('field_thumbnail')->entity){
  62. $imgPath = ($val->get('field_thumbnail')->entity->uri->value);
  63. $imgCroppedPath = DrupalimageEntityImageStyle::load('basic_page_thumbnail')->buildUrl($imgPath);
  64. $img = ($imgPath != '') ? '<img src="' . $imgCroppedPath . '" />' : '';
  65. }else{
  66. $img = "";
  67. }
  68. $nodeContent['#markup'] = '<a href="'.$url.'">' .
  69. '<h2>' . $val->getTitle() . '</h2>' .
  70. $img .
  71. '</a>';
  72. $content[] = $nodeContent;
  73. }
  74. }else{
  75. //Menu has no nodes
  76. $content['#markup'] = '';
  77. }
  78. //Add cache & css library to block
  79. $content['#cache'] = $cache;
  80. $content["#attached"] = array('library' => array('child_pages_display/child_pages_display_css'));
  81. return $content;
  82. }
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement