comparison core/modules/node/src/NodeForm.php @ 4:a9cd425dd02b

Update, including to Drupal core 8.6.10
author Chris Cannam
date Thu, 28 Feb 2019 13:11:55 +0000
parents c75dbcec494b
children 12f9dff5fda9
comparison
equal deleted inserted replaced
3:307d7a7fd348 4:a9cd425dd02b
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\Entity\ContentEntityForm; 6 use Drupal\Core\Entity\ContentEntityForm;
7 use Drupal\Core\Entity\EntityManagerInterface; 7 use Drupal\Core\Entity\EntityRepositoryInterface;
8 use Drupal\Core\Entity\EntityTypeBundleInfoInterface; 8 use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
9 use Drupal\Core\Form\FormStateInterface; 9 use Drupal\Core\Form\FormStateInterface;
10 use Drupal\Core\Session\AccountInterface; 10 use Drupal\Core\Session\AccountInterface;
11 use Drupal\Core\TempStore\PrivateTempStoreFactory; 11 use Drupal\Core\TempStore\PrivateTempStoreFactory;
12 use Symfony\Component\DependencyInjection\ContainerInterface; 12 use Symfony\Component\DependencyInjection\ContainerInterface;
33 protected $currentUser; 33 protected $currentUser;
34 34
35 /** 35 /**
36 * Constructs a NodeForm object. 36 * Constructs a NodeForm object.
37 * 37 *
38 * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager 38 * @param \Drupal\Core\Entity\EntityRepositoryInterface $entity_repository
39 * The entity manager. 39 * The entity repository.
40 * @param \Drupal\Core\TempStore\PrivateTempStoreFactory $temp_store_factory 40 * @param \Drupal\Core\TempStore\PrivateTempStoreFactory $temp_store_factory
41 * The factory for the temp store object. 41 * The factory for the temp store object.
42 * @param \Drupal\Core\Entity\EntityTypeBundleInfoInterface $entity_type_bundle_info 42 * @param \Drupal\Core\Entity\EntityTypeBundleInfoInterface $entity_type_bundle_info
43 * The entity type bundle service. 43 * The entity type bundle service.
44 * @param \Drupal\Component\Datetime\TimeInterface $time 44 * @param \Drupal\Component\Datetime\TimeInterface $time
45 * The time service. 45 * The time service.
46 * @param \Drupal\Core\Session\AccountInterface $current_user 46 * @param \Drupal\Core\Session\AccountInterface $current_user
47 * The current user. 47 * The current user.
48 */ 48 */
49 public function __construct(EntityManagerInterface $entity_manager, PrivateTempStoreFactory $temp_store_factory, EntityTypeBundleInfoInterface $entity_type_bundle_info = NULL, TimeInterface $time = NULL, AccountInterface $current_user) { 49 public function __construct(EntityRepositoryInterface $entity_repository, PrivateTempStoreFactory $temp_store_factory, EntityTypeBundleInfoInterface $entity_type_bundle_info = NULL, TimeInterface $time = NULL, AccountInterface $current_user) {
50 parent::__construct($entity_manager, $entity_type_bundle_info, $time); 50 parent::__construct($entity_repository, $entity_type_bundle_info, $time);
51 $this->tempStoreFactory = $temp_store_factory; 51 $this->tempStoreFactory = $temp_store_factory;
52 $this->currentUser = $current_user; 52 $this->currentUser = $current_user;
53 } 53 }
54 54
55 /** 55 /**
56 * {@inheritdoc} 56 * {@inheritdoc}
57 */ 57 */
58 public static function create(ContainerInterface $container) { 58 public static function create(ContainerInterface $container) {
59 return new static( 59 return new static(
60 $container->get('entity.manager'), 60 $container->get('entity.repository'),
61 $container->get('tempstore.private'), 61 $container->get('tempstore.private'),
62 $container->get('entity_type.bundle.info'), 62 $container->get('entity_type.bundle.info'),
63 $container->get('datetime.time'), 63 $container->get('datetime.time'),
64 $container->get('current_user') 64 $container->get('current_user')
65 ); 65 );
100 $node = $this->entity; 100 $node = $this->entity;
101 101
102 if ($this->operation == 'edit') { 102 if ($this->operation == 'edit') {
103 $form['#title'] = $this->t('<em>Edit @type</em> @title', [ 103 $form['#title'] = $this->t('<em>Edit @type</em> @title', [
104 '@type' => node_get_type_label($node), 104 '@type' => node_get_type_label($node),
105 '@title' => $node->label() 105 '@title' => $node->label(),
106 ]); 106 ]);
107 } 107 }
108 108
109 // Changed must be sent to the client, for later overwrite error checking. 109 // Changed must be sent to the client, for later overwrite error checking.
110 $form['changed'] = [ 110 $form['changed'] = [
215 * The "Publish" button was removed. 215 * The "Publish" button was removed.
216 */ 216 */
217 public function updateStatus($entity_type_id, NodeInterface $node, array $form, FormStateInterface $form_state) { 217 public function updateStatus($entity_type_id, NodeInterface $node, array $form, FormStateInterface $form_state) {
218 $element = $form_state->getTriggeringElement(); 218 $element = $form_state->getTriggeringElement();
219 if (isset($element['#published_status'])) { 219 if (isset($element['#published_status'])) {
220 $node->setPublished($element['#published_status']); 220 $element['#published_status'] ? $node->setPublished() : $node->setUnpublished();
221 } 221 }
222 } 222 }
223 223
224 /** 224 /**
225 * {@inheritdoc} 225 * {@inheritdoc}
283 $context = ['@type' => $node->getType(), '%title' => $node->label(), 'link' => $node_link]; 283 $context = ['@type' => $node->getType(), '%title' => $node->label(), 'link' => $node_link];
284 $t_args = ['@type' => node_get_type_label($node), '%title' => $node->link($node->label())]; 284 $t_args = ['@type' => node_get_type_label($node), '%title' => $node->link($node->label())];
285 285
286 if ($insert) { 286 if ($insert) {
287 $this->logger('content')->notice('@type: added %title.', $context); 287 $this->logger('content')->notice('@type: added %title.', $context);
288 drupal_set_message(t('@type %title has been created.', $t_args)); 288 $this->messenger()->addStatus($this->t('@type %title has been created.', $t_args));
289 } 289 }
290 else { 290 else {
291 $this->logger('content')->notice('@type: updated %title.', $context); 291 $this->logger('content')->notice('@type: updated %title.', $context);
292 drupal_set_message(t('@type %title has been updated.', $t_args)); 292 $this->messenger()->addStatus($this->t('@type %title has been updated.', $t_args));
293 } 293 }
294 294
295 if ($node->id()) { 295 if ($node->id()) {
296 $form_state->setValue('nid', $node->id()); 296 $form_state->setValue('nid', $node->id());
297 $form_state->set('nid', $node->id()); 297 $form_state->set('nid', $node->id());
310 $store->delete($node->uuid()); 310 $store->delete($node->uuid());
311 } 311 }
312 else { 312 else {
313 // In the unlikely case something went wrong on save, the node will be 313 // In the unlikely case something went wrong on save, the node will be
314 // rebuilt and node form redisplayed the same way as in preview. 314 // rebuilt and node form redisplayed the same way as in preview.
315 drupal_set_message(t('The post could not be saved.'), 'error'); 315 $this->messenger()->addError($this->t('The post could not be saved.'));
316 $form_state->setRebuild(); 316 $form_state->setRebuild();
317 } 317 }
318 } 318 }
319 319
320 } 320 }