BlockContentDeleteForm.php 1.63 KB
Newer Older
1 2 3 4
<?php

/**
 * @file
5
 * Contains \Drupal\block\Form\BlockContentDeleteForm.
6 7
 */

8
namespace Drupal\block_content\Form;
9

10
use Drupal\Core\Entity\ContentEntityConfirmFormBase;
11
use Drupal\Core\Form\FormStateInterface;
12
use Drupal\Core\Url;
13 14 15 16

/**
 * Provides a confirmation form for deleting a custom block entity.
 */
17
class BlockContentDeleteForm extends ContentEntityConfirmFormBase {
18 19 20 21 22 23 24 25 26 27 28

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

  /**
   * {@inheritdoc}
   */
29
  public function getCancelUrl() {
30
    return new Url('block.admin_display');
31 32 33 34 35 36 37 38 39 40 41 42
  }

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

  /**
   * {@inheritdoc}
   */
43
  public function buildForm(array $form, FormStateInterface $form_state) {
44 45 46
    $instances = $this->entity->getInstances();

    $form['message'] = array(
47
      '#markup' => $this->formatPlural(count($instances), 'This will also remove 1 placed block instance.', 'This will also remove @count placed block instances.'),
48 49 50
      '#access' => !empty($instances),
    );

51
    return parent::buildForm($form, $form_state);
52 53 54 55 56
  }

  /**
   * {@inheritdoc}
   */
57
  public function submitForm(array &$form, FormStateInterface $form_state) {
58 59
    $this->entity->delete();
    drupal_set_message($this->t('Custom block %label has been deleted.', array('%label' => $this->entity->label())));
60
    $this->logger('block_content')->notice('Custom block %label has been deleted.', array('%label' => $this->entity->label()));
61
    $form_state->setRedirect('block_content.list');
62 63 64
  }

}