Define a Block in Drupal 8

Public

</> CopyGet raw version
php
  1. namespace Drupal\example_block\Plugin\Block;
  2. use Drupal\block\BlockBase;
  3.  
  4. class blockExampleBlock extends BlockBase {
  5. /**
  6.   * Implements \Drupal\block\BlockBase::build().
  7.   */
  8. public function build() {
  9. $build = array();
  10.  
  11. // Define block content:
  12. $content = ‘Example block content.;
  13. $build['#markup'] = $content;
  14.  
  15. // Add any JS or perhaps CSS
  16. $module_path = drupal_get_path('module', 'example_block');
  17.  
  18. $build['#attached']['css'][] = $module_path .
  19. '/css/jquery_example_block.css';
  20.  
  21. $build['#attached']['js'][] = $module_path .
  22. '/js/jquery_example_block.js';
  23.  
  24. return $build;
  25. }
  26. }