FieldGroupDeleteForm.php in Field Group 8.3
File
src/Form/FieldGroupDeleteForm.php
View source
<?php
namespace Drupal\field_group\Form;
use Drupal\Core\Form\ConfirmFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Messenger\MessengerInterface;
use Drupal\field_group\FieldgroupUi;
use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class FieldGroupDeleteForm extends ConfirmFormBase {
protected $fieldGroup;
protected $messenger;
protected $entityTypeBundleInfo;
public function __construct(MessengerInterface $messenger, EntityTypeBundleInfoInterface $entity_type_bundle_info) {
$this->messenger = $messenger;
$this->entityTypeBundleInfo = $entity_type_bundle_info;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('messenger'), $container
->get('entity_type.bundle.info'));
}
public function getFormId() {
return 'field_group_delete_form';
}
public function buildForm(array $form, FormStateInterface $form_state, $field_group_name = NULL, $entity_type_id = NULL, $bundle = NULL, $context = NULL) {
if ($context == 'form') {
$mode = $this
->getRequest()->attributes
->get('form_mode_name');
}
else {
$mode = $this
->getRequest()->attributes
->get('view_mode_name');
}
if (empty($mode)) {
$mode = 'default';
}
$this->fieldGroup = field_group_load_field_group($field_group_name, $entity_type_id, $bundle, $context, $mode);
return parent::buildForm($form, $form_state);
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$bundles = $this->entityTypeBundleInfo
->getAllBundleInfo();
$bundle_label = $bundles[$this->fieldGroup->entity_type][$this->fieldGroup->bundle]['label'];
field_group_delete_field_group($this->fieldGroup);
$this->messenger
->addMessage($this
->t('The group %group has been deleted from the %type content type.', [
'%group' => $this->fieldGroup->label,
'%type' => $bundle_label,
]));
$form_state
->setRedirectUrl($this
->getCancelUrl());
}
public function getQuestion() {
return $this
->t('Are you sure you want to delete the group %group?', [
'%group' => $this->fieldGroup->label,
]);
}
public function getConfirmText() {
return $this
->t('Delete');
}
public function getCancelUrl() {
return FieldgroupUi::getFieldUiRoute($this->fieldGroup);
}
}