How to create multiple blocks in Drupal 8

04/07/2018


Hi I would like to create multiple blocks using the data rendered from variable table in Drupal 8.

I was able to achieve it in Drupal 7 but can’t find a way to do the same in Drupal 8.

Can anyone suggest me a way to achieve this.

Code for Drupal 7 is as follows:

<?php
/**
 * Create a block that will display the calendar feed
 * Implements hook_block_info
 */
function my_module_block_info() {
  $blocks = array();
  $block_urls = variable_get('my_module_content');
  $block_regions = variable_get('my_module_content_block_region');
  $number_of_blocks = count($block_urls);
  if ( $number_of_blocks > 0 ) {
    foreach ( $block_urls as $key => $block_url ) {
      $blocks['eventblock-' . $key] = array(
        'info' => t('my_module_widget_block_' . $key), 
        'status' => TRUE,
        'region' => $block_regions[$key],
        'cache' => DRUPAL_NO_CACHE,
      );
    }

  }
  return $blocks;
}

/**
 * Render the my_module block
 * Implements hook_block_view
 */
function my_module_block_view($delta = '') {

  $block = array();
  $block_urls = variable_get('my_module_content' , array());
    foreach ( $block_urls as $key => $block_url ) {

        switch ($delta) {
          case 'eventblock-' . $key:
            $widgetURL= $block_urls[$key];

            $block['content'] = my_module_content_generate($widgetURL); // Some function to generate block content.
          break;
        }

    }
  return $block;
}

قالب وردپرس