[英]Creating a simple Drupal 8 custom service - argument should be an instance of a class, recognised as an array
总的来说,我是Drupal 8开发和面向对象的PHP的新手。 我一直在尝试制作一个简单的自定义块模块,其中包括带有简单依赖项注入的服务。 但是,我似乎无法弄清楚为什么构造方法没有收到适当的参数-盯着代码看了几个小时。 我的代码:
event_countdown.services.yml
services:
event_countdown.date_calculator:
class: Drupal\event_countdown\DateCalculator
event_countdown.routing.yml
event_countdown.content:
defaults:
_title: 'Event Countdown'
requirements:
_permission: 'access content'
options:
no_cache: 'TRUE'
src / DateCalculator.php
<?php
namespace Drupal\event_countdown;
class DateCalculator {
// forms the block's output based on given date
public function daysUntilEventStarts($date) {
// get difference in days between now and the given date
$difference = $this->getDifferenceInDaysFromCurrentDate($date);
// if event date is in the future
if($difference >= 1) {
return "Days left until the event starts: " . $difference;
// if event date and current date are on the same day
} else if($difference == 0) {
return "The event is in progress.";
// if event date is in the past
} else {
return "The event has already ended.";
}
}
// calculates difference in days between now and given date
public function getDifferenceInDaysFromCurrentDate($date) {
// current time
$now = time();
// event datetime field converted to timestamp
$event_date = strtotime($date);
// timestamp difference
$difference = $event_date - $now;
// timestamp difference rounded down to days
return round($difference / (60 * 60 * 24));
}
}
src /插件/块/EventCountdownBlock.php
<?php
namespace Drupal\event_countdown\Plugin\Block;
use Drupal\Core\Block\BlockBase;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\event_countdown\DateCalculator;
/**
* Provides an event countdown block.
*
* @Block(
* id = "event_countdown_block",
* admin_label = @Translation("Event countdown block"),
* category = @Translation("Event countdown block"),
* )
*/
class EventCountdownBlock extends BlockBase {
protected $dateCalculator;
public function __construct(DateCalculator $dateCalculator) {
$this->dateCalculator = $dateCalculator;
}
public static function create(ContainerInterface $container) {
return new static($container->get('event_countdown.date_calculator'));
}
public function build() {
// get current node based on route
$node = \Drupal::routeMatch()->getParameter('node');
//check whether node type equals 'event' - if it does, get text output, otherwise display an error message
if($node->getType() == "event") {
// get datetime value from node
$date = $node->field_event_date->value;
// call service method to calculate difference in days
$output = $this->dateCalculator->daysUntilEventStarts($date);
} else {
// display error
$output = "Woops! This block is intended only for event pages.";
}
return array(
// output
'#markup' => $output,
// prevent block caching
'#cache' => [
'max-age' => 0,
],
);
}
}
我在Drupal日志中得到的错误是:
Recoverable fatal error: Argument 1 passed to Drupal\event_countdown\Plugin\Block\EventCountdownBlock::__construct() must be an instance of Drupal\event_countdown\DateCalculator, array given, called in D:\drupal8\devdesktop\drupal-8.5.1\core\lib\Drupal\Core\Plugin\Factory\ContainerFactory.php on line 25 and defined in Drupal\event_countdown\Plugin\Block\EventCountdownBlock->__construct() (line 24 of D:\drupal8\devdesktop\drupal-8.5.1\modules\custom\event_countdown\src\Plugin\Block\EventCountdownBlock.php)#0 D:\drupal8\devdesktop\drupal-8.5.1\core\includes\bootstrap.inc(582):_drupal_error_handler_real(4096, 'Argument 1 pass...', 'D:\\drupal8\\devd...', 24, Array) #1 [...]
我将不胜感激。 谢谢!
在https://drupal.stackexchange.com/questions/260187/creating-a-simple-drupal-8-custom-service-argument-should-be-an-instance-of-a找到了解决方案。 由于插件不支持容器,因此必须实现ContainerFactoryPluginInterface。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.