You are here

SimpleMegaMenuTypeDeleteForm.php in Simple Mega Menu 2.0.x

Same filename and directory in other branches
  1. 8 src/Form/SimpleMegaMenuTypeDeleteForm.php

File

src/Form/SimpleMegaMenuTypeDeleteForm.php
View source
<?php

namespace Drupal\simple_megamenu\Form;

use Drupal\Core\Entity\EntityDeleteForm;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;

/**
 * Builds the form to delete Simple mega menu type entities.
 */
class SimpleMegaMenuTypeDeleteForm extends EntityDeleteForm {

  /**
   * {@inheritdoc}
   */
  public function getQuestion() {
    return $this
      ->t('Are you sure you want to delete %name?', [
      '%name' => $this->entity
        ->label(),
    ]);
  }

  /**
   * {@inheritdoc}
   */
  public function getCancelUrl() {
    return new Url('entity.simple_mega_menu_type.collection');
  }

  /**
   * {@inheritdoc}
   */
  public function getConfirmText() {
    return $this
      ->t('Delete');
  }

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
    $entities = $this->entityTypeManager
      ->getStorage('simple_mega_menu')
      ->getQuery()
      ->condition('type', $this->entity
      ->id())
      ->count()
      ->execute();
    if ($entities) {
      $caption = '<p>' . $this
        ->formatPlural($entities, '%type is used by 1 piece of content on your site. You can not remove this content type until you have removed all of the %type content.', '%type is used by @count pieces of content on your site. You may not remove %type until you have removed all of the %type content.', [
        '%type' => $this->entity
          ->label(),
      ]) . '</p>';
      $form['#title'] = $this
        ->getQuestion();
      $form['description'] = [
        '#markup' => $caption,
      ];
      return $form;
    }
    return parent::buildForm($form, $form_state);
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $this->entity
      ->delete();
    $this
      ->messenger()
      ->addStatus($this
      ->t('content @type: deleted @label.', [
      '@type' => $this->entity
        ->bundle(),
      '@label' => $this->entity
        ->label(),
    ]));
    $form_state
      ->setRedirectUrl($this
      ->getCancelUrl());
  }

}

Classes

Namesort descending Description
SimpleMegaMenuTypeDeleteForm Builds the form to delete Simple mega menu type entities.