custom_block.module 8.19 KB
Newer Older
1 2 3 4 5 6 7
<?php

/**
 * @file
 * Allows the creaation of custom blocks through the user interface.
 */

8 9 10 11 12 13
use Drupal\custom_block\Plugin\Core\Entity\CustomBlockType;
use Drupal\custom_block\Plugin\Core\Entity\CustomBlock;
use Drupal\custom_block\Plugin\block\block\CustomBlockBlock;
use Drupal\block\Plugin\Core\Entity\Block;

/**
14
 * Implements hook_menu_local_tasks().
15
 */
16 17 18 19 20 21 22 23 24 25 26 27
function custom_block_menu_local_tasks(&$data, $router_item, $root_path) {
  // Add the "Add custom block" action link to the theme-specific block library
  // listing page.
  // @todo This should just be $root_path == 'admin/structure/block/list/%/add'
  //   but block_menu() registers static router paths instead of dynamic ones.
  if (preg_match('@^admin/structure/block/list/(.*)/add$@', $root_path)) {
    $item = menu_get_item('block/add');
    if ($item['access']) {
      $data['actions']['block/add'] = array(
        '#theme' => 'menu_local_action',
        '#link' => $item,
      );
28 29 30 31
    }
  }
}

32 33 34 35
/**
 * Implements hook_menu().
 */
function custom_block_menu() {
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
  $items['admin/structure/custom-blocks'] = array(
    'title' => 'Custom block types',
    'description' => 'Manage custom block types.',
    'page callback' => 'custom_block_type_list',
    'access arguments' => array('administer blocks'),
    'file' => 'custom_block.admin.inc',
  );
  $items['admin/structure/custom-blocks/add'] = array(
    'title' => 'Add custom block type',
    'page callback' => 'custom_block_type_add',
    'access arguments' => array('administer blocks'),
    'type' => MENU_LOCAL_ACTION,
    'weight' => 1,
    'file' => 'custom_block.admin.inc',
  );
  $items['admin/structure/custom-blocks/manage/%custom_block_type'] = array(
    'title' => 'Edit custom block type',
    'title callback' => 'entity_page_label',
    'title arguments' => array(4),
    'page callback' => 'custom_block_type_edit',
    'page arguments' => array(4),
    'access arguments' => array('administer blocks'),
    'file' => 'custom_block.admin.inc',
  );
  $items['admin/structure/custom-blocks/manage/%custom_block_type/edit'] = array(
    'title' => 'Edit',
    'type' => MENU_DEFAULT_LOCAL_TASK,
    'weight' => -10,
  );
  $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',
  );

  $items['block/add'] = array(
    'title' => 'Add custom block',
77
    'route_name' => 'custom_block_add_page',
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
  );
  $items['block/add/%custom_block_type'] = array(
    'title callback' => 'entity_page_label',
    'title arguments' => array(2),
    'page callback' => 'custom_block_add',
    'page arguments' => array(2),
    'access arguments' => array('administer blocks'),
    'description' => 'Add custom block',
    'file' => 'custom_block.pages.inc',
  );
  // There has to be a base-item in order for contextual links to work.
  $items['block/%custom_block'] = array(
    'title' => 'Edit',
    'page callback' => 'custom_block_edit',
    'page arguments' => array(1),
    'access callback' => 'entity_page_access',
    'access arguments' => array(1, 'update'),
    'file' => 'custom_block.pages.inc',
  );
  $items['block/%custom_block/edit'] = array(
    'title' => 'Edit',
    'weight' => 0,
    'type' => MENU_DEFAULT_LOCAL_TASK,
    'context' => MENU_CONTEXT_PAGE | MENU_CONTEXT_INLINE,
  );
  $items['block/%custom_block/delete'] = array(
    'title' => 'Delete',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('custom_block_delete_form', 1),
    'access callback' => 'entity_page_access',
    'access arguments' => array(1, 'delete'),
    'weight' => 1,
    'type' => MENU_LOCAL_TASK,
    'context' => MENU_CONTEXT_INLINE,
    'file' => 'custom_block.pages.inc',
  );
114 115 116 117 118 119 120 121 122 123 124
  return $items;
}

/**
 * Implements hook_theme().
 */
function custom_block_theme($existing, $type, $theme, $path) {
  return array(
    'custom_block_block' => array(
      'variables' => array('body' => NULL, 'format' => NULL),
    ),
125 126 127 128
    'custom_block_add_list' => array(
      'variables' => array('content' => NULL),
      'file' => 'custom_block.pages.inc',
    ),
129 130 131 132 133 134 135 136 137 138 139 140 141 142
  );
}

/**
 * Returns HTML for a custom block.
 *
 * @ingroup themeable
 */
function theme_custom_block_block($variables) {
  $body = $variables['body'];
  $format = $variables['format'];

  return check_markup($body, $format);
}
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186

/**
 * Loads a custom block type.
 *
 * @param int $id
 *   The ID of the custom block type to load.
 *
 * @return Drupal\custom_block\Plugin\Core\Entity\CustomBlockType|false
 *   A CustomBlockType object or FALSE if the requested $id does not exist.
 */
function custom_block_type_load($id) {
  return entity_load('custom_block_type', $id);
}

/**
 * Loads a custom block.
 *
 * @param int $id
 *   The id of the custom block.
 *
 * @return Drupal\custom_block\Plugin\Core\Entity\CustomBlock|false
 *   A CustomBlock object or FALSE if the requested $id does not exist.
 */
function custom_block_load($id) {
  return entity_load('custom_block', $id);
}

/**
 * Implements hook_entity_info_alter().
 */
function custom_block_entity_info_alter(&$types) {
  // Add a translation handler for fields if the language module is enabled.
  if (module_exists('language')) {
    $types['custom_block']['translation']['custom_block'] = TRUE;
  }
}

/**
 * Implements hook_entity_bundle_info().
 */
function custom_block_entity_bundle_info() {
  $bundles = array();
  foreach (config_get_storage_names_with_prefix('custom_block.type.') as $config_name) {
    $config = config($config_name);
187
    $bundles['custom_block'][$config->get('id')]['label'] = $config->get('label');
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281
  }
  return $bundles;
}

/**
 * Implements hook_entity_view_mode_info().
 */
function custom_block_entity_view_mode_info() {
  $view_modes['custom_block']['full'] = array(
    'label' => t('Full'),
  );
  return $view_modes;
}

/**
 * Adds the default body field to a custom block type.
 *
 * @param string $block_type_id
 *   Id of the block type.
 * @param string $label
 *   (optional) The label for the body instance. Defaults to 'Block body'
 *
 * @return array()
 *   Body field instance.
 */
function custom_block_add_body_field($block_type_id, $label = 'Block body') {
  // Add or remove the body field, as needed.
  $field = field_info_field('block_body');
  $instance = field_info_instance('custom_block', 'block_body', $block_type_id);
  if (empty($field)) {
    $field = array(
      'field_name' => 'block_body',
      'type' => 'text_with_summary',
      'entity_types' => array('custom_block'),
    );
    $field = field_create_field($field);
  }
  if (empty($instance)) {
    $instance = array(
      'field_name' => 'block_body',
      'entity_type' => 'custom_block',
      'bundle' => $block_type_id,
      'label' => $label,
      'widget' => array('type' => 'text_textarea_with_summary'),
      'settings' => array('display_summary' => FALSE),
    );
    $instance = field_create_instance($instance);

    // Assign display settings for 'default' view mode.
    entity_get_display('custom_block', $block_type_id, 'default')
      ->setComponent('block_body', array(
        'label' => 'hidden',
        'type' => 'text_default',
      ))
      ->save();
  }

  return $instance;
}

/**
 * Implements hook_form_FORM_ID_alter() for block_plugin_ui().
 */
function custom_block_form_block_plugin_ui_alter(&$form, $form_state) {
  foreach ($form['left']['plugin_library']['#rows'] as $plugin_id => &$row) {
    // @todo Clean up when http://drupal.org/node/1874498 lands.
    if (strpos($plugin_id, ':') === FALSE) {
      continue;
    }
    list($base, $derivative) = explode(':', $plugin_id);
    if ($base !== 'custom_block') {
      continue;
    }
    $custom_block = entity_load_by_uuid('custom_block', $derivative);
    $row['1']['data']['#links']['edit'] = array(
      'title' => t('Edit'),
      'href' => 'block/' . $custom_block->id() . '/edit'
    );
  }
}

/**
 * Implements hook_admin_paths().
 */
function custom_block_admin_paths() {
  $paths = array(
    'block/add' => TRUE,
    'block/add/*' => TRUE,
    'block/*/edit' => TRUE,
    'block/*/delete' => TRUE,
    'admin/structure/custom-blocks/*' => TRUE,
  );
  return $paths;
}