comparison core/modules/node/src/NodeForm.php @ 5:12f9dff5fda9 tip

Update to Drupal core 8.7.1
author Chris Cannam
date Thu, 09 May 2019 15:34:47 +0100
parents a9cd425dd02b
children
comparison
equal deleted inserted replaced
4:a9cd425dd02b 5:12f9dff5fda9
1 <?php 1 <?php
2 2
3 namespace Drupal\node; 3 namespace Drupal\node;
4 4
5 use Drupal\Component\Datetime\TimeInterface; 5 use Drupal\Component\Datetime\TimeInterface;
6 use Drupal\Core\Datetime\DateFormatterInterface;
6 use Drupal\Core\Entity\ContentEntityForm; 7 use Drupal\Core\Entity\ContentEntityForm;
7 use Drupal\Core\Entity\EntityRepositoryInterface; 8 use Drupal\Core\Entity\EntityRepositoryInterface;
8 use Drupal\Core\Entity\EntityTypeBundleInfoInterface; 9 use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
9 use Drupal\Core\Form\FormStateInterface; 10 use Drupal\Core\Form\FormStateInterface;
10 use Drupal\Core\Session\AccountInterface; 11 use Drupal\Core\Session\AccountInterface;
29 * The Current User object. 30 * The Current User object.
30 * 31 *
31 * @var \Drupal\Core\Session\AccountInterface 32 * @var \Drupal\Core\Session\AccountInterface
32 */ 33 */
33 protected $currentUser; 34 protected $currentUser;
35
36 /**
37 * The date formatter service.
38 *
39 * @var \Drupal\Core\Datetime\DateFormatterInterface
40 */
41 protected $dateFormatter;
34 42
35 /** 43 /**
36 * Constructs a NodeForm object. 44 * Constructs a NodeForm object.
37 * 45 *
38 * @param \Drupal\Core\Entity\EntityRepositoryInterface $entity_repository 46 * @param \Drupal\Core\Entity\EntityRepositoryInterface $entity_repository
43 * The entity type bundle service. 51 * The entity type bundle service.
44 * @param \Drupal\Component\Datetime\TimeInterface $time 52 * @param \Drupal\Component\Datetime\TimeInterface $time
45 * The time service. 53 * The time service.
46 * @param \Drupal\Core\Session\AccountInterface $current_user 54 * @param \Drupal\Core\Session\AccountInterface $current_user
47 * The current user. 55 * The current user.
48 */ 56 * @param \Drupal\Core\Datetime\DateFormatterInterface $date_formatter
49 public function __construct(EntityRepositoryInterface $entity_repository, PrivateTempStoreFactory $temp_store_factory, EntityTypeBundleInfoInterface $entity_type_bundle_info = NULL, TimeInterface $time = NULL, AccountInterface $current_user) { 57 * The date formatter service.
58 */
59 public function __construct(EntityRepositoryInterface $entity_repository, PrivateTempStoreFactory $temp_store_factory, EntityTypeBundleInfoInterface $entity_type_bundle_info = NULL, TimeInterface $time = NULL, AccountInterface $current_user, DateFormatterInterface $date_formatter) {
50 parent::__construct($entity_repository, $entity_type_bundle_info, $time); 60 parent::__construct($entity_repository, $entity_type_bundle_info, $time);
51 $this->tempStoreFactory = $temp_store_factory; 61 $this->tempStoreFactory = $temp_store_factory;
52 $this->currentUser = $current_user; 62 $this->currentUser = $current_user;
63 $this->dateFormatter = $date_formatter;
53 } 64 }
54 65
55 /** 66 /**
56 * {@inheritdoc} 67 * {@inheritdoc}
57 */ 68 */
59 return new static( 70 return new static(
60 $container->get('entity.repository'), 71 $container->get('entity.repository'),
61 $container->get('tempstore.private'), 72 $container->get('tempstore.private'),
62 $container->get('entity_type.bundle.info'), 73 $container->get('entity_type.bundle.info'),
63 $container->get('datetime.time'), 74 $container->get('datetime.time'),
64 $container->get('current_user') 75 $container->get('current_user'),
76 $container->get('date.formatter')
65 ); 77 );
66 } 78 }
67 79
68 /** 80 /**
69 * {@inheritdoc} 81 * {@inheritdoc}
132 '#wrapper_attributes' => ['class' => ['entity-meta__title']], 144 '#wrapper_attributes' => ['class' => ['entity-meta__title']],
133 ]; 145 ];
134 $form['meta']['changed'] = [ 146 $form['meta']['changed'] = [
135 '#type' => 'item', 147 '#type' => 'item',
136 '#title' => $this->t('Last saved'), 148 '#title' => $this->t('Last saved'),
137 '#markup' => !$node->isNew() ? format_date($node->getChangedTime(), 'short') : $this->t('Not saved yet'), 149 '#markup' => !$node->isNew() ? $this->dateFormatter->format($node->getChangedTime(), 'short') : $this->t('Not saved yet'),
138 '#wrapper_attributes' => ['class' => ['entity-meta__last-saved']], 150 '#wrapper_attributes' => ['class' => ['entity-meta__last-saved']],
139 ]; 151 ];
140 $form['meta']['author'] = [ 152 $form['meta']['author'] = [
141 '#type' => 'item', 153 '#type' => 'item',
142 '#title' => $this->t('Author'), 154 '#title' => $this->t('Author'),
143 '#markup' => $node->getOwner()->getUsername(), 155 '#markup' => $node->getOwner()->getAccountName(),
144 '#wrapper_attributes' => ['class' => ['entity-meta__author']], 156 '#wrapper_attributes' => ['class' => ['entity-meta__author']],
145 ]; 157 ];
146 158
147 $form['status']['#group'] = 'footer'; 159 $form['status']['#group'] = 'footer';
148 160
277 */ 289 */
278 public function save(array $form, FormStateInterface $form_state) { 290 public function save(array $form, FormStateInterface $form_state) {
279 $node = $this->entity; 291 $node = $this->entity;
280 $insert = $node->isNew(); 292 $insert = $node->isNew();
281 $node->save(); 293 $node->save();
282 $node_link = $node->link($this->t('View')); 294 $node_link = $node->toLink($this->t('View'))->toString();
283 $context = ['@type' => $node->getType(), '%title' => $node->label(), 'link' => $node_link]; 295 $context = ['@type' => $node->getType(), '%title' => $node->label(), 'link' => $node_link];
284 $t_args = ['@type' => node_get_type_label($node), '%title' => $node->link($node->label())]; 296 $t_args = ['@type' => node_get_type_label($node), '%title' => $node->toLink()->toString()];
285 297
286 if ($insert) { 298 if ($insert) {
287 $this->logger('content')->notice('@type: added %title.', $context); 299 $this->logger('content')->notice('@type: added %title.', $context);
288 $this->messenger()->addStatus($this->t('@type %title has been created.', $t_args)); 300 $this->messenger()->addStatus($this->t('@type %title has been created.', $t_args));
289 } 301 }