FieldCollectionItemDeleteForm.php in Field collection 8.3
File
src/Form/FieldCollectionItemDeleteForm.php
View source
<?php
namespace Drupal\field_collection\Form;
use Drupal\Core\Entity\ContentEntityConfirmFormBase;
use Drupal\Core\Form\FormStateInterface;
class FieldCollectionItemDeleteForm extends ContentEntityConfirmFormBase {
public function getQuestion() {
return t('Are you sure you want to delete this %title?', [
'%title' => $this->entity
->label(),
]);
}
public function getCancelUrl() {
return $this->entity
->getHost()
->toUrl();
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$host = $this->entity
->getHost();
foreach ($host->{$this->entity
->bundle()} as $key => $value) {
if ($value->value == $this->entity
->id()) {
unset($host->{$this->entity
->bundle()}[$key]);
}
}
$host
->save();
$this->entity
->delete();
$this
->logger('content')
->notice('@type: deleted %id.', [
'@type' => $this->entity
->bundle(),
'%id' => $this->entity
->id(),
]);
$node_type_storage = $this->entityTypeManager
->getStorage('field_collection');
$node_type = $node_type_storage
->load($this->entity
->bundle())
->label();
drupal_set_message(t('@type %id has been deleted.', [
'@type' => $node_type,
'%id' => $this->entity
->id(),
]));
$form_state
->setRedirectUrl($host
->toUrl());
}
}