Php 创建一个简单的Drupal8自定义服务参数应该是一个类的实例,可以识别为数组
我对Drupal8开发和面向对象的PHP基本上是新手。我一直在尝试制作一个简单的自定义块模块,其中包括一个带有简单依赖项注入的服务。然而,我似乎不明白为什么构造方法没有收到一个合适的参数——已经盯着代码看了几个小时了。我的代码: 事件\u倒计时.services.ymlPhp 创建一个简单的Drupal8自定义服务参数应该是一个类的实例,可以识别为数组,php,oop,dependency-injection,drupal-modules,drupal-8,Php,Oop,Dependency Injection,Drupal Modules,Drupal 8,我对Drupal8开发和面向对象的PHP基本上是新手。我一直在尝试制作一个简单的自定义块模块,其中包括一个带有简单依赖项注入的服务。然而,我似乎不明白为什么构造方法没有收到一个合适的参数——已经盯着代码看了几个小时了。我的代码: 事件\u倒计时.services.yml services: event_countdown.date_calculator: class: Drupal\event_countdown\DateCalculator 事件\u倒计时.路由
services:
event_countdown.date_calculator:
class: Drupal\event_countdown\DateCalculator
事件\u倒计时.路由.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));
}
}
<?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,
],
);
}
}
我将感谢任何形式的帮助。谢谢大家! 在中找到了解决方案。由于插件不知道容器,不得不实现ContainerFactoryPluginInterface。DateCalculator类中的构造函数在哪里?您好。在这个构造函数中,我实际上需要包含什么?类的实例不是通过EventCountdownBlock.php中的create/construct方法创建的吗?可能是我把整个事情搞错了。。。
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 [...]