ConditionalFieldDeleteForm.php in Conditional Fields 4.x
File
src/Form/ConditionalFieldDeleteForm.php
View source
<?php
namespace Drupal\conditional_fields\Form;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Form\ConfirmFormBase;
use Drupal\Core\Url;
class ConditionalFieldDeleteForm extends ConfirmFormBase {
private $entityType;
private $bundle;
private $fieldName;
private $uuid;
public function getQuestion() {
return $this
->t('Are you sure you want to delete the %field_name condition?', [
'%field_name' => $this->fieldName,
]);
}
public function getCancelUrl() {
return Url::fromRoute('conditional_fields.conditions_list', [
'entity_type' => $this->entityType,
'bundle' => $this->bundle,
]);
}
public function getFormId() {
return 'conditional_field_delete_form';
}
public function submitForm(array &$form, FormStateInterface $form_state) {
if (empty($this->entityType) || empty($this->bundle) || empty($this->fieldName) || empty($this->uuid)) {
return;
}
$entity = \Drupal::entityTypeManager()
->getStorage('entity_form_display')
->load($this->entityType . '.' . $this->bundle . '.default');
if (!$entity) {
return;
}
$field = $entity
->getComponent($this->fieldName);
unset($field['third_party_settings']['conditional_fields'][$this->uuid]);
$entity
->setComponent($this->fieldName, $field);
$entity
->save();
$form_state
->setRedirectUrl($this
->getCancelUrl());
}
public function buildForm(array $form, FormStateInterface $form_state, $entity_type = NULL, $bundle = NULL, $field_name = NULL, $uuid = NULL) {
$this->entityType = $entity_type;
$this->bundle = $bundle;
$this->fieldName = $field_name;
$this->uuid = $uuid;
return parent::buildForm($form, $form_state);
}
}