View source
<?php
namespace Drupal\ds\Plugin\DsField\Node;
use Drupal\Component\Datetime\TimeInterface;
use Drupal\Core\Datetime\DateFormatterInterface;
use Drupal\ds\Plugin\DsField\Date;
use Drupal\Core\Render\Renderer;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class NodeSubmittedBy extends Date {
protected $renderer;
public function __construct($configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entity_type_manager, Renderer $renderer, DateFormatterInterface $date_service, TimeInterface $time_service) {
$this->renderer = $renderer;
parent::__construct($configuration, $plugin_id, $plugin_definition, $entity_type_manager, $date_service, $time_service);
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('entity_type.manager'), $container
->get('renderer'), $container
->get('date.formatter'), $container
->get('datetime.time'));
}
public function build() {
$field = $this
->getFieldConfiguration();
$node = $this
->entity();
$account = $node
->getOwner();
$date_format = str_replace('ds_post_date_', '', $field['formatter']);
$user_name = [
'#theme' => 'username',
'#account' => $account,
];
return [
'#markup' => $this
->t('Submitted by @user on @date.', [
'@user' => $this->renderer
->render($user_name),
'@date' => $this->dateFormatter
->format($this
->entity()->created->value, $date_format),
]),
'#cache' => [
'tags' => $account
->getCacheTags(),
],
];
}
}