Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
By incursus on
I'm sure I must be missing the obvious - been staring at this for way too long, and am about to pull my hair out lol.
I am trying to inject a dependency for the database connection into a block class. Here are the relevants:
namespace Drupal\mymodule\Plugin\Block;
use Drupal\Core\Block\BlockBase;
use Drupal\Core\Database\Connection;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class MyModuleBlock extends BlockBase implements ContainerFactoryPluginInterface {
protected $connection;
public function __construct( Connection $connection ) {
$this->connection = $connection;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static(
$configuration,
$plugin_id,
$plugin_definition,
$container->get('database')
);
}
...
}
I have this in my services YML file:
services:
mymodule.mymodule_block:
class: Drupal\mymodule\Plugin\Block\MyModuleBlock
arguments: ['@database']
And I keep getting this error:
Recoverable fatal error: Argument 1 passed to Drupal\\mymodule\\Plugin\\Block\\MyModuleBlock::__construct() must be an instance of Drupal\\Core\\Database\\Connection, array given ...
I appreciate any guidance! Thanks in advance!
Comments
Never mind, I walked away for
Never mind, I walked away for a few minutes, came back, and was like WTF? Fixed it .... lol.
Cheers.
For the rest of us - could
For the rest of us - could you kindly
- marked your issue "SOLVED" by editing the title, and
- post what you did to fix it?
Future strugglers will thank you.
============================
Resonetrics: Better Tools for Building Brands
http://resonetrics.com
http://technologyformarketers.com
http://kittenassociates.org
http://www.linkedin.com/in/sammooreatresonetrics
Good point :)
Here is the working code:
A little problem with your code.
Hello, Scott Burkett! Thanks for sharing this information with the community.
First of all, the code you shared has an extra comma at the end of the following line:
$container->get('database'),
I actually have created a Controller in one of my custom modules and tried your code with no success.
I am pretty frustrated after spending a whole day trying to implement "Database Injection" without success. The documentation on this subject is virtually none existent. People talk about it but there is no practical example.
Would you mind making a small example module that shows how to create and use a "Database Injection"?
By the way, I keep getting
By the way, I keep getting the same error message when using your code:
I tried first to create a Controller using this following lines of code I found elsewhere without success:
What is funny is that I got the same error message!
Did you remember to add the
Did you remember to add the service tag entry in your mymodule.services.yml file? It should look something like this:
Working example
In my report.services.yml, I have:
In my dbtest.php controller, I have:
create() method is unnecessary
You don't need the
create()
method as you've listed@database
in your *.services.yml file. This passes the service directly to the constructor. See: Services and Dependency InjectionYou would then specify the service and method in your *.routing.yml instead of the controller. See: https://www.drupal.org/docs/8/api/routing-system/structure-of-routes
File name and class name need to be the same
Also, the filename and class name need to match. For example,
dbTestController.php
andclass dbTestController
.Thanks for this piece of
Thanks for this piece of knowledge.