Commit d91dc913 authored by webchick's avatar webchick

Issue #2022415 by bdone: Convert custom_block_type_delete_form() to a Controller.

parent e2949882
......@@ -34,53 +34,3 @@ function custom_block_type_add() {
function custom_block_type_edit(CustomBlockType $block_type) {
return Drupal::entityManager()->getForm($block_type);
}
/**
* Page callback: Form constructor for the custom block type deletion form.
*
* @param \Drupal\custom_block\Plugin\Core\Entity\CustomBlockType $block_type
* The custom block type to be deleted.
*
* @see custom_block_menu()
* @see custom_block_type_delete_form_submit()
*
* @ingroup forms
*/
function custom_block_type_delete_form($form, &$form_state, CustomBlockType $block_type) {
$form_state['custom_block_type'] = $block_type;
$form['id'] = array(
'#type' => 'value',
'#value' => $block_type->id(),
);
$message = t('Are you sure you want to delete %label?', array('%label' => $block_type->label()));
$blocks = Drupal::entityQuery('custom_block')->condition('type', $block_type->id())->execute();
if (!empty($blocks)) {
drupal_set_title($message, PASS_THROUGH);
$caption = '<p>' . format_plural(count($blocks), '%label is used by 1 custom block on your site. You can not remove this block type until you have removed all of the %label blocks.', '%label is used by @count custom blocks on your site. You may not remove %label until you have removed all of the %label custom blocks.', array('%label' => $block_type->label())) . '</p>';
$form['description'] = array('#markup' => $caption);
return $form;
}
return confirm_form(
$form,
$message,
'admin/structure/custom-blocks',
t('This action cannot be undone.'),
t('Delete')
);
}
/**
* Form submission handler for custom_block_type_delete_form().
*/
function custom_block_type_delete_form_submit($form, &$form_state) {
$block_type = $form_state['custom_block_type'];
$block_type->delete();
drupal_set_message(t('Custom block type %label has been deleted.', array('%label' => $block_type->label())));
watchdog('custom_block', 'Custom block type %label has been deleted.', array('%label' => $block_type->label()), WATCHDOG_NOTICE);
$form_state['redirect'] = 'admin/structure/custom-blocks';
}
......@@ -60,14 +60,10 @@ function custom_block_menu() {
);
$items['admin/structure/custom-blocks/manage/%custom_block_type/delete'] = array(
'title' => 'Delete',
'page callback' => 'drupal_get_form',
'page arguments' => array('custom_block_type_delete_form', 4),
'access arguments' => array('administer blocks'),
'type' => MENU_LOCAL_TASK,
'weight' => 10,
'file' => 'custom_block.admin.inc',
'route_name' => 'custom_block_type_delete',
);
$items['block/add'] = array(
'title' => 'Add custom block',
'route_name' => 'custom_block_add_page',
......
......@@ -18,3 +18,10 @@ custom_block_add_form:
_content: 'Drupal\custom_block\Controller\CustomBlockController::addForm'
requirements:
_permission: 'administer blocks'
custom_block_type_delete:
pattern: '/admin/structure/custom-blocks/manage/{custom_block_type}/delete'
defaults:
_entity_form: 'custom_block_type.delete'
requirements:
_permission: 'administer blocks'
<?php
/**
* @file
* Contains \Drupal\custom_block\Form\CustomBlockTypeDeleteForm.
*/
namespace Drupal\custom_block\Form;
use Drupal\Core\Entity\EntityConfirmFormBase;
use Drupal\Core\Entity\EntityControllerInterface;
use Drupal\Core\Entity\Query\QueryFactory;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\Request;
/**
* Provides a confirmation form for deleting a custom block type entity.
*/
class CustomBlockTypeDeleteForm extends EntityConfirmFormBase implements EntityControllerInterface {
/**
* The query factory to create entity queries.
*
* @var \Drupal\Core\Entity\Query\QueryFactory
*/
public $queryFactory;
/**
* Constructs a query factory object.
*
* @param \Drupal\Core\Entity\Query\QueryFactory $query_factory
* The entity query object.
*/
public function __construct(QueryFactory $query_factory) {
$this->queryFactory = $query_factory;
}
/**
* {@inheritdoc}
*/
public static function createInstance(ContainerInterface $container, $entity_type, array $entity_info) {
return new static(
$container->get('entity.query')
);
}
/**
* {@inheritdoc}
*/
public function getQuestion() {
return t('Are you sure you want to delete %label?', array('%label' => $this->entity->label()));
}
/**
* {@inheritdoc}
*/
public function getCancelPath() {
return 'admin/structure/custom-blocks';
}
/**
* {@inheritdoc}
*/
public function getConfirmText() {
return t('Delete');
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, array &$form_state, Request $request = NULL) {
$blocks = $this->queryFactory->get('custom_block')->condition('type', $this->entity->id())->execute();
if (!empty($blocks)) {
$caption = '<p>' . format_plural(count($blocks), '%label is used by 1 custom block on your site. You can not remove this block type until you have removed all of the %label blocks.', '%label is used by @count custom blocks on your site. You may not remove %label until you have removed all of the %label custom blocks.', array('%label' => $this->entity->label())) . '</p>';
$form['description'] = array('#markup' => $caption);
return $form;
}
else {
return parent::buildForm($form, $form_state, $request);
}
}
/**
* {@inheritdoc}
*/
public function submit(array $form, array &$form_state) {
$this->entity->delete();
$form_state['redirect'] = 'admin/structure/custom-blocks';
drupal_set_message(t('Custom block type %label has been deleted.', array('%label' => $this->entity->label())));
watchdog('custom_block', 'Custom block type %label has been deleted.', array('%label' => $this->entity->label()), WATCHDOG_NOTICE);
}
}
......@@ -23,7 +23,8 @@
* controllers = {
* "storage" = "Drupal\Core\Config\Entity\ConfigStorageController",
* "form" = {
* "default" = "Drupal\custom_block\CustomBlockTypeFormController"
* "default" = "Drupal\custom_block\CustomBlockTypeFormController",
* "delete" = "Drupal\custom_block\Form\CustomBlockTypeDeleteForm"
* },
* "list" = "Drupal\custom_block\CustomBlockTypeListController"
* },
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment