GeysirParagraphDeleteForm.php in Geysir 8
File
src/Form/GeysirParagraphDeleteForm.php
View source
<?php
namespace Drupal\geysir\Form;
use Drupal\Core\Entity\ContentEntityDeleteForm;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
class GeysirParagraphDeleteForm extends ContentEntityDeleteForm {
use GeysirFormEntityRevisionTrait;
public function getQuestion() {
$route_match = $this
->getRouteMatch();
$parent_entity_type = $route_match
->getParameter('parent_entity_type');
$parent_entity_revision = $route_match
->getParameter('parent_entity_revision');
$field_name = $route_match
->getParameter('field');
$delta = $route_match
->getParameter('delta');
$parent_entity_revision = $this
->getParentRevisionOrParent($parent_entity_type, $parent_entity_revision);
$field = $parent_entity_revision
->get($field_name);
$field_definition = $field
->getFieldDefinition();
$field_label = $field_definition
->getLabel();
return $this
->t('Are you sure you want to delete #@delta of @field of %label?', [
'@delta' => $delta,
'@field' => $field_label,
'%label' => $parent_entity_revision
->label(),
]);
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$route_match = $this
->getRouteMatch();
$parent_entity_type = $route_match
->getParameter('parent_entity_type');
$parent_entity_revision = $route_match
->getParameter('parent_entity_revision');
$field_name = $route_match
->getParameter('field');
$delta = $route_match
->getParameter('delta');
$parent_entity_revision = $this
->getParentRevisionOrParent($parent_entity_type, $parent_entity_revision);
$parent_entity_revision
->get($field_name)
->removeItem($delta);
$this
->saveParentEntityRevision($parent_entity_revision, $this->time, $this
->currentUser());
$parent_revision_id = $parent_entity_revision
->getRevisionId() ? $parent_entity_revision
->getRevisionId() : $parent_entity_revision
->id();
$form_state
->setTemporary([
'parent_entity_revision' => $parent_revision_id,
]);
$form_state
->setRedirectUrl($this
->getRedirectUrl());
}
public function getCancelUrl() {
return $this
->getRedirectUrl();
}
protected function getRedirectUrl() {
$referer = $this
->getRequest()->server
->get('HTTP_REFERER');
$path = parse_url($referer, PHP_URL_PATH);
return Url::fromUserInput($path);
}
protected function getParentRevisionOrParent($parent_entity_type, $parent_entity_revision) {
$entity_storage = $this->entityTypeManager
->getStorage($parent_entity_type);
if ($this->entityTypeManager
->getDefinition($parent_entity_type)
->isRevisionable()) {
return $entity_storage
->loadRevision($parent_entity_revision);
}
else {
return $entity_storage
->load($parent_entity_revision);
}
}
}