custom_block.admin.inc 3.01 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
<?php

/**
 * @file
 * Admin page callbacks for the custom block module.
 */

use Drupal\custom_block\Plugin\Core\Entity\CustomBlockType;

/**
 * Page callback: Lists custom block types.
 *
 * @return array
 *   A build array in the format expected by drupal_render().
 *
 * @see custom_block_menu()
 */
function custom_block_type_list() {
  return drupal_container()->get('plugin.manager.entity')->getListController('custom_block_type')->render();
}

/**
 * Page callback: Presents the custom block type creation form.
 *
 * @return array
 *   A form array as expected by drupal_render().
 *
 * @see custom_block_menu()
 */
function custom_block_type_add() {
  $block_type = entity_create('custom_block_type', array());
  return entity_get_form($block_type);
}

/**
 * Page callback: Presents the custom block type edit form.
 *
 * @param \Drupal\custom_block\Plugin\Core\Entity\CustomBlockType $block_type
 *   The custom block type to edit.
 *
 * @return array
 *   A form array as expected by drupal_render().
 *
 * @see custom_block_menu()
 */
function custom_block_type_edit(CustomBlockType $block_type) {
  return entity_get_form($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 = entity_query('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';
}