Am having trouble with creating blocks on the fly via my module.
I found information about Derivatives and Block Plugins. So far I can get the blocks to appear in the “Place block” modal,
but when I click on the “Place block” button in the modal I get an error.
TypeError: Drupal\block\BlockForm::getPluginForm(): Argument #1 ($block) must be of type Drupal\Core\Block\BlockPluginInterface, bool given, called in /Users/cy.jobes/Websites/foobar.org-d9-dev/src/web/core/modules/block/src/BlockForm.php on line 128 in Drupal\block\BlockForm->getPluginForm() (line 436 of /Users/cy.jobes/Websites/foobar.org-d9-dev/src/web/core/modules/block/src/BlockForm.php).
I get this error in the Drupal logs
Warning: Trying to access array offset on value of type null in Drupal\Core\Block\BlockBase->getMachineNameSuggestion() (line 242 of /Users/cy.jobes/Websites/foobar.org-d9-dev/src/web/core/lib/Drupal/Core/Block/BlockPluginTrait.php).
I’ve been following the tutorials I can find, but nothing seems to work. Can anyone tell me what I am doing wrong, or how to fix this?
Below are the two files used:
/Plugin/Block
<?php
namespace Drupal\foobar_gallery\Plugin\Block;
use Drupal\Core\Block\BlockBase;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\foobar_gallery\Controller\ImageDataController;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Provides a generic Menu block.
*
* @Block(
* id = "foobar_gallery_blocks",
* admin_label = @Translation("Gallery Block"),
* deriver = "Drupal\foobar_gallery\Plugin\Derivative\FoobarGalleryBlocks"
* )
*/
class FoobarGalleryBlocks extends BlockBase implements ContainerFactoryPluginInterface
{
private $gallery_data;
private $blockMachineName;
public function __construct($gallery_id, $block_machine_name) {
// Get the data object for this gallery
$imageDataController = new ImageDataController();
$this->gallery_data = $imageDataController->getGalleryData($gallery_id);
$this->blockMachineName = $block_machine_name;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return false;
}
/**
* {@inheritdoc}
*/
public function build() {
return [
'#theme' => 'gallery_slick_accessible',
'#gallery_data' => $this->gallery_data,
];
}
}
/Plugin/Derivative
<?php
namespace Drupal\foobar_gallery\Plugin\Derivative;
use Drupal\Component\Plugin\Derivative\DeriverBase;
use Drupal\Core\Plugin\Discovery\ContainerDeriverInterface;
use Drupal\foobar_gallery\Controller\ImageDataController;
use Symfony\Component\DependencyInjection\ContainerInterface;
class FoobarGalleryBlocks extends DeriverBase implements ContainerDeriverInterface
{
/**
* @inheritDoc
*/
public static function create(ContainerInterface $container, $base_plugin_id)
{
return new static(
);
}
/**
* @inheritDoc
*/
public function getDerivativeDefinitions($base_plugin_definition)
{
// Get the n ames of the blocks that are saved in the database
$imageDataController = new ImageDataController();
$blocks = $imageDataController->getBlockNames();
/*
* $blocks output
* array(3) {
[0]=>
object(stdClass)#656 (3) {
["id"]=>
string(1) "1"
["block_name"]=>
string(7) "450x200"
["block_machine_name"]=>
string(15) "gallery_450x200"
}
[1]=>
object(stdClass)#657 (3) {
["id"]=>
string(1) "2"
["block_name"]=>
string(7) "350x100"
["block_machine_name"]=>
string(15) "gallery_350x100"
}
[2]=>
object(stdClass)#658 (3) {
["id"]=>
string(1) "3"
["block_name"]=>
string(4) "fooo"
["block_machine_name"]=>
string(4) "fooo"
}
}
* */
// Create the blocks
foreach ($blocks as $block) {
$this->derivatives[$block->block_machine_name] = $base_plugin_definition;
$this->derivatives[$block->block_machine_name]['admin_label'] = t('Gallery Block: ') . $block->block_name;
}
return $this->derivatives;
}
}