Skip to content
custom_block.module 7.61 KiB
Newer Older
<?php

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

use Drupal\custom_block\Entity\CustomBlockType;
use Drupal\custom_block\Entity\CustomBlock;
/**
 * Implements hook_help().
 */
function custom_block_help($path, $arg) {
  switch ($path) {
    case 'admin/help#custom_block':
      return t('Allows the creation of custom blocks through the user interface.');

    case 'admin/structure/block/custom-blocks':
      $output = '<p>' . t('This page lists user-created blocks. These blocks are derived from block types. A block type can consist of different fields and display settings. From the block types tab you can manage these fields as well as create new block types.') . '</p>';
      return $output;

    case 'admin/structure/block/custom-blocks/types':
      $output = '<p>' . t('This page lists block types. A block type can consist of different fields and display settings. From here you can manage these fields as well as create new block types.') . '</p>';
      return $output;

  }
}

function custom_block_menu_local_tasks(&$data, $route_name) {
  if ($route_name == 'custom_block.list') {
    // @todo Move to a LocalAction plugin when https://drupal.org/node/2045267
    //   allows local actions to work with query strings.
    $item = menu_get_item('block/add');
    if ($item['access']) {
      // Add a destination parameter.
      $item['localized_options']['query']['destination'] = 'admin/structure/block/custom-blocks';
      $data['actions']['block/add'] = array(
        '#theme' => 'menu_local_action',
        '#link' => $item,
      );
    }
  }
  $routes = array_map(function ($theme) {
    return "block.admin_display_$theme";
    // @todo Move to a LocalAction plugin when https://drupal.org/node/2045267
    //   allows local actions to work with query strings.
    $item = menu_get_item('block/add');
    if ($item['access']) {
      // Add a destination parameter.
      $item['localized_options']['query']['theme'] = \Drupal::request()->attributes->get('theme');
      $data['actions']['block/add'] = array(
        '#theme' => 'menu_local_action',
        '#link' => $item,
      );
/**
 * Implements hook_menu().
 */
function custom_block_menu() {
  $items['admin/structure/block/custom-blocks'] = array(
    'title' => 'Custom block library',
    'description' => 'Manage custom blocks.',
    'route_name' => 'custom_block.list',
    'type' => MENU_LOCAL_TASK | MENU_NORMAL_ITEM,
  $items['admin/structure/block/custom-blocks/list'] = array(
    'title' => 'Blocks',
    'type' => MENU_DEFAULT_LOCAL_TASK,
  );
  $items['admin/structure/block/custom-blocks/types'] = array(
    'route_name' => 'custom_block.type_list',
  $items['admin/structure/block/custom-blocks/types/add'] = array(
    'route_name' => 'custom_block.type_add',
    'type' => MENU_SIBLING_LOCAL_TASK,
  $items['admin/structure/block/custom-blocks/manage/%custom_block_type'] = array(
    'title' => 'Edit custom block type',
    'title callback' => 'entity_page_label',
    'route_name' => 'custom_block.type_edit',
  $items['admin/structure/block/custom-blocks/manage/%custom_block_type/edit'] = array(
    'title' => 'Edit',
    'type' => MENU_DEFAULT_LOCAL_TASK,
    'weight' => -10,
  );
  $items['block/add'] = array(
    'title' => 'Add custom block',
    'route_name' => 'custom_block.add_page',
  );
  $items['block/add/%custom_block_type'] = array(
    'route_name' => 'custom_block.add_form'
  );
  // There has to be a base-item in order for contextual links to work.
  $items['block/%custom_block'] = array(
    'title' => 'Edit',
    'route_name' => 'custom_block.edit',
  );
  $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',
    'weight' => 1,
    'type' => MENU_LOCAL_TASK,
    'context' => MENU_CONTEXT_INLINE,
    'route_name' => 'custom_block.delete',
  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\Entity\CustomBlockType|null
 *   A CustomBlockType object or NULL 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\Entity\CustomBlock|null
 *   A CustomBlock object or NULL 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 = \Drupal::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('custom_block', 'body');
  $instance = field_info_instance('custom_block', 'body', $block_type_id);
    $field = entity_create('field_entity', array(
      'entity_type' => 'custom_block',
    $instance = entity_create('field_instance', array(
      'entity_type' => 'custom_block',
      'bundle' => $block_type_id,
      'label' => $label,
      'settings' => array('display_summary' => FALSE),
    // Assign widget settings for the 'default' form mode.
    entity_get_form_display('custom_block', $block_type_id, 'default')
    // Assign display settings for 'default' view mode.
    entity_get_display('custom_block', $block_type_id, 'default')
        'label' => 'hidden',
        'type' => 'text_default',
      ))
      ->save();
  }

  return $instance;
}

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