Add additional revision information

This commit is contained in:
Paul Weidner 2023-09-07 16:24:49 -07:00 committed by Michael Stenta
parent 268412bd23
commit 2f8b3e05e8
1 changed files with 55 additions and 0 deletions

View File

@ -2,15 +2,56 @@
namespace Drupal\farm_ui_theme\Form;
use Drupal\Component\Datetime\TimeInterface;
use Drupal\Core\Datetime\DateFormatterInterface;
use Drupal\Core\Entity\ContentEntityForm;
use Drupal\Core\Entity\EntityRepositoryInterface;
use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Render\Element\RenderCallbackInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Entity form for gin content form styling.
*/
class GinContentFormBase extends ContentEntityForm implements RenderCallbackInterface {
/**
* The date formatter service.
*
* @var \Drupal\Core\Datetime\DateFormatterInterface
*/
protected $dateFormatter;
/**
* Constructs a ContentEntityForm object.
*
* @param \Drupal\Core\Entity\EntityRepositoryInterface $entity_repository
* The entity repository service.
* @param \Drupal\Core\Entity\EntityTypeBundleInfoInterface $entity_type_bundle_info
* The entity type bundle service.
* @param \Drupal\Component\Datetime\TimeInterface $time
* The time service.
* @param \Drupal\Core\Datetime\DateFormatterInterface $date_formatter
* The date formatter service.
*/
public function __construct(EntityRepositoryInterface $entity_repository, EntityTypeBundleInfoInterface $entity_type_bundle_info, TimeInterface $time, DateFormatterInterface $date_formatter) {
parent::__construct($entity_repository, $entity_type_bundle_info, $time);
$this->dateFormatter = $date_formatter;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('entity.repository'),
$container->get('entity_type.bundle.info'),
$container->get('datetime.time'),
$container->get('date.formatter'),
);
}
/**
* {@inheritdoc}
*/
@ -114,6 +155,20 @@ class GinContentFormBase extends ContentEntityForm implements RenderCallbackInte
}
}
// Add authoring information for existing entities.
if (!$this->entity->isNew()) {
$changed = $this->dateFormatter->format($this->entity->getChangedTime(), 'short', '', $this->currentUser()->getTimeZone(), '');
$created = $this->dateFormatter->format($this->entity->getCreatedTime(), 'short', '', $this->currentUser()->getTimeZone(), '');
$form['revision_field_group']['revision_meta'] = [
'#theme' => 'item_list',
'#items' => [
$this->t('Author: @author', ['@author' => $this->entity->getOwner()->getAccountName()]),
$this->t('Last saved: @timestamp', ['@timestamp' => $changed]),
$this->t('Created: @timestamp', ['@timestamp' => $created]),
],
];
}
return $form;
}