SocialPostEntityDeleteForm.php in Social Post 3.x
File
src/Form/SocialPostEntityDeleteForm.php
View source
<?php
namespace Drupal\social_post\Form;
use Drupal\Component\Datetime\TimeInterface;
use Drupal\Core\Entity\ContentEntityDeleteForm;
use Drupal\Core\Entity\EntityRepositoryInterface;
use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Messenger\MessengerInterface;
use Drupal\Core\Routing\CurrentRouteMatch;
use Drupal\Core\Url;
use Symfony\Component\DependencyInjection\ContainerInterface;
class SocialPostEntityDeleteForm extends ContentEntityDeleteForm {
protected $routeMatch;
protected $uid;
protected $provider;
protected $messenger;
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('current_route_match'), $container
->get('messenger'));
}
public function __construct(EntityRepositoryInterface $entity_repository, EntityTypeBundleInfoInterface $entity_type_bundle_info, TimeInterface $time, CurrentRouteMatch $route_match, MessengerInterface $messenger) {
parent::__construct($entity_repository, $entity_type_bundle_info, $time);
$this->routeMatch = $route_match;
$this->messenger = $messenger;
$this->uid = $this->routeMatch
->getParameter('user');
$this->provider = $this->routeMatch
->getParameter('provider');
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$entity = $this
->getEntity();
$entity
->delete();
$form_state
->setRedirectUrl($this
->getRedirectUrl());
$this->messenger
->addMessage($this
->getDeletionMessage());
$this
->logDeletionMessage();
}
protected function getRedirectUrl() {
if ($this->uid) {
return Url::fromRoute('entity.user.edit_form', [
'user' => $this->uid,
]);
}
return Url::fromRoute('social_post_' . $this->provider . '.user.collection');
}
public function getCancelUrl() {
if ($this->uid) {
return Url::fromRoute('entity.user.edit_form', [
'user' => $this->uid,
]);
}
return Url::fromRoute('social_post_' . $this->provider . '.user.collection');
}
}