ParagraphDeleteForm.php in Paragraphs table 8
File
src/Form/ParagraphDeleteForm.php
View source
<?php
namespace Drupal\paragraphs_table\Form;
use Drupal\Core\Entity\ContentEntityDeleteForm;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\RevisionableEntityBundleInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\entity_reference_revisions\EntityReferenceRevisionsFieldItemList;
use Drupal\paragraphs\ParagraphInterface;
class ParagraphDeleteForm extends ContentEntityDeleteForm {
private function getInfoItem() {
$field_name = $this->entity
->get('parent_field_name')->value;
$host = $this->entity
->getParentEntity();
$entity_type = $host
->getEntityTypeId();
$bundle = $host
->bundle();
$entityFieldManager = \Drupal::service('entity_field.manager')
->getFieldDefinitions($entity_type, $bundle);
return [
'%type' => $entityFieldManager[$field_name]
->getLabel(),
'%id' => $this->entity
->id(),
];
}
public function getQuestion() {
return $this
->t('Are you sure you want to delete %type item %id?', $this
->getInfoItem());
}
protected function getDeletionMessage() {
return $this
->t('Paragraph %type item %id has been deleted.', $this
->getInfoItem());
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$parent = $this->entity
->getParentEntity();
$parent
->get($this->entity
->get('parent_field_name')->value);
$parent_field = $this
->getParentField($this->entity);
$parent_field_item = $this
->findParentFieldItem($this->entity, $parent_field);
$parent_field
->removeItem($parent_field_item
->getName());
if ($this
->shouldCreateNewRevision($parent)) {
$this
->saveNewRevision($parent);
}
else {
$parent
->save();
}
}
public function getParentField(ParagraphInterface $paragraph) {
$parent = $paragraph
->getParentEntity();
if (!$parent) {
return NULL;
}
return $parent
->get($paragraph
->get('parent_field_name')->value);
}
protected function findParentFieldItem(ParagraphInterface $paragraph, EntityReferenceRevisionsFieldItemList $field) {
$paragraph_id = $paragraph
->id();
$paragraph_revision_id = $paragraph
->getRevisionId();
foreach ($field as $item) {
if ($item->target_id == $paragraph_id && $item->target_revision_id == $paragraph_revision_id) {
return $item;
}
}
return NULL;
}
protected function shouldCreateNewRevision(EntityInterface $entity) {
$new_revision_default = FALSE;
if ($bundle_entity_type = $entity
->getEntityType()
->getBundleEntityType()) {
$bundle_entity = $this->entityTypeManager
->getStorage($bundle_entity_type)
->load($entity
->bundle());
if ($bundle_entity instanceof RevisionableEntityBundleInterface) {
$new_revision_default = $bundle_entity
->shouldCreateNewRevision();
}
}
return $new_revision_default;
}
public function saveNewRevision(ContentEntityInterface $entity) {
$result = $this
->doSaveNewRevision($entity);
while ($entity instanceof ParagraphInterface) {
$parent = $entity
->getParentEntity();
if (!$parent) {
break;
}
$this
->doSaveNewRevision($parent);
$entity = $parent;
}
return $result;
}
protected function doSaveNewRevision(ContentEntityInterface $entity) {
if ($entity instanceof ParagraphInterface) {
$parent_field_item = $this
->getParentFieldItem($entity);
}
try {
$entity
->setNewRevision();
} catch (\LogicException $e) {
}
$status = $entity
->save();
if (isset($parent_field_item)) {
$parent_field_item
->set('target_revision_id', $entity
->getRevisionId());
}
return $status;
}
}