Allows the creaation of custom blocks through the user interface.
<?php
/**
* @file
* Allows the creaation of custom blocks through the user interface.
*/
use Drupal\custom_block\Plugin\Core\Entity\CustomBlockType;
use Drupal\custom_block\Plugin\Core\Entity\CustomBlock;
/**
* Implements hook_menu_local_tasks().
*/
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,
);
}
}
}
/**
* Implements hook_menu().
*/
function custom_block_menu() {
$items['admin/structure/custom-blocks'] = array(
'title' => 'Custom block types',
'description' => 'Manage custom block types.',
'route_name' => 'custom_block_type_list',
);
$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',
'route_name' => 'custom_block_add_page',
);
$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',
);
return $items;
}
/**
* Implements hook_theme().
*/
function custom_block_theme($existing, $type, $theme, $path) {
return array(
'custom_block_add_list' => array(
'variables' => array(
'content' => NULL,
),
'file' => 'custom_block.pages.inc',
'template' => 'custom-block-add-list',
),
);
}
/**
* 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);
$bundles['custom_block'][$config
->get('id')]['label'] = $config
->get('label');
}
return $bundles;
}
/**
* 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,
'settings' => array(
'display_summary' => FALSE,
),
);
$instance = field_create_instance($instance);
// Assign widget settings for the 'default' form mode.
entity_get_form_display('custom_block', $block_type_id, 'default')
->setComponent('block_body', array(
'type' => 'text_textarea_with_summary',
))
->save();
// 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;
}
Name | Description |
---|---|
custom_block_type_load | Loads a custom block type. |
custom_block_theme | Implements hook_theme(). |
custom_block_menu_local_tasks | Implements hook_menu_local_tasks(). |
custom_block_menu | Implements hook_menu(). |
custom_block_load | Loads a custom block. |
custom_block_form_block_plugin_ui_alter | Implements hook_form_FORM_ID_alter() for block_plugin_ui(). |
custom_block_entity_info_alter | Implements hook_entity_info_alter(). |
custom_block_entity_bundle_info | Implements hook_entity_bundle_info(). |
custom_block_admin_paths | Implements hook_admin_paths(). |
custom_block_add_body_field | Adds the default body field to a custom block type. |