Skip to content
Snippets Groups Projects

Used Dependency Injection

1 file
+ 28
1
Compare changes
  • Side-by-side
  • Inline
@@ -4,6 +4,7 @@ namespace Drupal\optional_date\Plugin\Field\FieldFormatter;
use Drupal\Core\Datetime\DrupalDateTime;
use Drupal\Core\Datetime\Entity\DateFormat;
use Drupal\Core\Datetime\DateFormatterInterface;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\FormatterBase;
use Drupal\optional_date\Element\OptionalDate;
@@ -27,6 +28,32 @@ abstract class OptionalDateFormatterBase extends FormatterBase {
*/
const ISO_DATETIME_FORMAT = "Y-m-d\TH:i:s\Z";
/**
* The date formatter service.
*
* @var \Drupal\Core\Datetime\DateFormatterInterface
*/
protected $dateFormatter;
/**
* Constructs a OptionalDateFormatterBase object.
*
* @param \Drupal\Core\Datetime\DateFormatterInterface $date_formatter
* The date formatter service.
*/
public function __construct(DateFormatterInterface $date_formatter) {
$this->dateFormatter = $date_formatter;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('date.formatter'),
);
}
/**
* @param $timeIsEmpty bool
* @param $timestamp int
@@ -71,7 +98,7 @@ abstract class OptionalDateFormatterBase extends FormatterBase {
*/
protected function getFormats() {
$dateTime = new DrupalDateTime();
$formats = DateFormat::loadMultiple();
$formats = $this->dateFormatter->loadMultiple();
$options = [];
foreach ($formats as $id => $format) {
Loading