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

/**
 * @file
5
 * Allows the creation of custom blocks through the user interface.
6 7
 */

8 9
use Drupal\custom_block\Entity\CustomBlockType;
use Drupal\custom_block\Entity\CustomBlock;
10

11 12 13 14 15 16
/**
 * Implements hook_help().
 */
function custom_block_help($path, $arg) {
  switch ($path) {
    case 'admin/help#custom_block':
17 18 19 20 21 22 23 24 25 26 27
      $output = '';
      $output .= '<h3>' . t('About') . '</h3>';
      $output .= '<p>' . t('The Custom Block module allows you to create blocks of content, which can be placed in regions throughout the website. Custom blocks can have fields; see the <a href="!field-help">Field module help</a> for more information. Once created, custom blocks can be placed like blocks provided by other modules; see the <a href="!blocks">Block module help page</a> for details. For more information, see <a href="!online-help">the online documentation for the Custom Block module</a>.', array('!custom-blocks' => \Drupal::url('custom_block.list'), '!field-help' => \Drupal::url('help.page', array('name' => 'field')), '!blocks' => \Drupal::url('help.page', array('name' => 'block')), '!online-help' => 'https://drupal.org/documentation/modules/custom_block')) . '</p>';
      $output .= '<h3>' . t('Uses') . '</h3>';
      $output .= '<dl>';
      $output .= '<dt>' . t('Creating and managing custom block types') . '</dt>';
      $output .= '<dd>' . t('Users with the <em>Administer blocks</em> permission can create different custom block types, each with different fields and display settings, from the <a href="!types">Custom block types</a> page. The Custom block types page lists all of your created custom block types, and allows you to edit and manage them. For more information about managing fields and display settings, see the <a href="!field-ui">Field UI module help</a>.', array('!types' => \Drupal::url('custom_block.type_list'), '!field-ui' => \Drupal::url('help.page', array('name' => 'field_ui')))) . '</dd>';
      $output .= '<dt>' . t('Creating custom blocks') . '</dt>';
      $output .= '<dd>' . t('Users with the <em>Administer blocks</em> permission can <a href="!block-add">add custom blocks</a> of each of their defined custom block types. Created custom blocks are then listed on the <a href="!blocks">Blocks administration page</a>.', array('!blocks' => \Drupal::url('block.admin_display'), '!block-add' => \Drupal::url('custom_block.add_page'))) . '</dd>';
      $output .= '</dl>';
      return $output;
28

29
    case 'admin/structure/block/custom-blocks':
30 31 32
      $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;

33
    case 'admin/structure/block/custom-blocks/types':
34 35 36 37 38 39
      $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;

  }
}

40 41 42 43 44 45 46 47 48
/**
 * Implements hook_menu_link_defaults().
 */
function custom_block_menu_link_defaults() {
  $links['custom_block.add_page'] = array(
    'link_title' => 'Add custom block',
    'description' => 'Add custom block',
    'route_name' => 'custom_block.add_page',
  );
49 50 51 52 53
  $items['custom_block.list'] = array(
    'link_title' => 'Custom block library',
    'parent' => 'block.admin.structure',
    'description' => 'Manage custom blocks.',
  );
54 55 56 57

  return $links;
}

58 59 60 61 62
/**
 * Implements hook_theme().
 */
function custom_block_theme($existing, $type, $theme, $path) {
  return array(
63 64 65
    'custom_block_add_list' => array(
      'variables' => array('content' => NULL),
      'file' => 'custom_block.pages.inc',
66
      'template' => 'custom-block-add-list',
67
    ),
68 69 70
  );
}

71 72 73 74 75 76
/**
 * Loads a custom block type.
 *
 * @param int $id
 *   The ID of the custom block type to load.
 *
77
 * @return \Drupal\custom_block\Entity\CustomBlockType|null
78
 *   A CustomBlockType object or NULL if the requested $id does not exist.
79 80 81 82 83 84 85 86 87 88 89
 */
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.
 *
90
 * @return \Drupal\custom_block\Entity\CustomBlock|null
91
 *   A CustomBlock object or NULL if the requested $id does not exist.
92 93 94 95 96 97
 */
function custom_block_load($id) {
  return entity_load('custom_block', $id);
}

/**
98
 * Implements hook_entity_type_alter().
99
 */
100 101
function custom_block_entity_type_alter(array &$entity_types) {
  /** @var $entity_types \Drupal\Core\Entity\EntityTypeInterface[] */
102
  // Add a translation handler for fields if the language module is enabled.
103
  if (\Drupal::moduleHandler()->moduleExists('language')) {
104
    $translation = $entity_types['custom_block']->get('translation');
105
    $translation['custom_block'] = TRUE;
106
    $entity_types['custom_block']->set('translation', $translation);
107 108 109 110 111 112 113 114
  }
}

/**
 * Implements hook_entity_bundle_info().
 */
function custom_block_entity_bundle_info() {
  $bundles = array();
115
  foreach (\Drupal::configFactory()->listAll('custom_block.type.') as $config_name) {
116
    $config = \Drupal::config($config_name);
117
    $bundles['custom_block'][$config->get('id')]['label'] = $config->get('label');
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
  }
  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.
135 136
  $field = field_info_field('custom_block', 'body');
  $instance = field_info_instance('custom_block', 'body', $block_type_id);
137
  if (empty($field)) {
138
    $field = entity_create('field_config', array(
139
      'name' => 'body',
140
      'entity_type' => 'custom_block',
141
      'type' => 'text_with_summary',
142 143
    ));
    $field->save();
144 145
  }
  if (empty($instance)) {
146
    $instance = entity_create('field_instance_config', array(
147
      'field_name' => 'body',
148 149 150 151
      'entity_type' => 'custom_block',
      'bundle' => $block_type_id,
      'label' => $label,
      'settings' => array('display_summary' => FALSE),
152 153
    ));
    $instance->save();
154

155 156
    // Assign widget settings for the 'default' form mode.
    entity_get_form_display('custom_block', $block_type_id, 'default')
157
      ->setComponent('body', array(
158 159 160 161
        'type' => 'text_textarea_with_summary',
      ))
      ->save();

162 163
    // Assign display settings for 'default' view mode.
    entity_get_display('custom_block', $block_type_id, 'default')
164
      ->setComponent('body', array(
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
        '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,
181
    'block/*' => TRUE,
182
    'block/*/delete' => TRUE,
183
    'admin/structure/block/custom-blocks/*' => TRUE,
184 185 186
  );
  return $paths;
}