6

In Drupal 7, if we need to define multiple instances of blocks from a database table, we use a foreach loop to return the array key, and then we we get all the multiple instances of our block using hook_block_info().

function mymodule_block_info() {
    $blocks = array();
    $add = db_select('mymodule_blocks_data', 'n')
                  ->fields('n', array('delta', 'title', 'content_type'))
                  ->condition('n.form_type', 'Add', '=')
                  ->execute();
    foreach ($add as $row) {
        $blocks[$row->delta] = array(
      'info' => t('@name (Add Form Block) Type: @type', array('@name' => $row->title, '@type' => $row->content_type)),
      'cache' => DRUPAL_NO_CACHE,
        );
    }

    $edit = db_select('mymodule_blocks_data', 'n')
                  ->fields('n', array('delta', 'title', 'content_type', 'nid'))
                  ->condition('n.form_type', 'Edit', '=')
                  ->execute();
    foreach ($edit as $row) {
        $blocks[$row->delta] = array(
      'info' => t('@name (Edit Form Block) Nid: @nid', array('@name' => $row->title, '@nid' => $row->nid)),
      'cache' => DRUPAL_NO_CACHE,
        );
    }

    return $blocks;
}

In Drupal 8, I suppose hook_block_info() is replaced by plugin derivatives. Though I found a way here where they display nodes by using the entity manager, I have still to understand what I need to do when I am using my custom database table, and not an entity.

What is the equivalent code in Drupal 8 for hook_block_info()?

4
+50

Create a file ex : YourBlock.php in Drupal\mymodule\Plugin\Derivative\YourBlock.php

<?php

namespace Drupal\mymodule\Plugin\Derivative;

use Drupal\Component\Plugin\Derivative\DeriverBase;

class YourBlock extends DeriverBase
{
    public function getDerivativeDefinitions($base_plugin_definition)
    {
        $results = \Drupal::database()->select('mymodule_blocks_data', 'n')
                  ->fields('n', array('delta','title', 'content_type'))
                  ->condition('n.form_type', 'Add', '=')
                  ->execute();
        foreach ($results as $result) {
            $this->derivatives[$result->delta] = $base_plugin_definition;
            $this->derivatives[$result->delta]['admin_label'] = t('Title: @name content type: @ctype ', array('@name' => $result->title, '@ctype' => $result->content_type));
        }

        return $this->derivatives;
    }
}

Also add the necessary annotation to display all the dynamic block instances in

Drupal\mymodule\Plugin\Block\YourBlock.php
<?php

namespace Drupal\mymodule\Plugin\Block;

use Drupal\Core\Block\BlockBase;

/**
 * Display all instances for 'YourBlock' block plugin.
 *
 * @Block(
 *   id = "mymodule_block",
 *   admin_label = @Translation("Your block"),
 *   deriver = "Drupal\mymodule\Plugin\Derivative\YourBlock"
 * )
 */

Rebuild the cache as well

  • can i make necessary changes to your code ?? for future readers ? @m1r1k – make-me-alive May 30 '16 at 12:24
  • how to print these variable into template? – Fawwad Mar 1 '18 at 3:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.