custom_block.module 8.13 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
 * Implements hook_menu_local_tasks().
42
 */
43
44
function custom_block_menu_local_tasks(&$data, $route_name) {
  if ($route_name == 'custom_block.list') {
45
46
47
48
49
    // @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.
50
      $item['localized_options']['query']['destination'] = 'admin/structure/block/custom-blocks';
51
52
53
54
55
56
      $data['actions']['block/add'] = array(
        '#theme' => 'menu_local_action',
        '#link' => $item,
      );
    }
  }
57
58
}

59
60
61
62
/**
 * Implements hook_menu().
 */
function custom_block_menu() {
63
64
  $items['admin/structure/block/custom-blocks'] = array(
    'title' => 'Custom block library',
65
    'description' => 'Manage custom blocks.',
66
    'route_name' => 'custom_block.list',
67
    'type' => MENU_NORMAL_ITEM,
68
  );
69

70
  $items['admin/structure/block/custom-blocks/manage/%custom_block_type'] = array(
71
72
    'title' => 'Edit custom block type',
    'title callback' => 'entity_page_label',
73
    'title arguments' => array(5),
74
    'route_name' => 'custom_block.type_edit',
75
  );
76

77
  $items['block/add'] = array(
78
    'title' => 'Add custom block',
79
    'description' => 'Add custom block',
80
    'route_name' => 'custom_block.add_page',
81
  );
82

83
84
85
  return $items;
}

86
87
88
89
90
91
92
93
94
95
96
97
98
/**
 * 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',
  );

  return $links;
}

99
100
101
102
103
/**
 * Implements hook_theme().
 */
function custom_block_theme($existing, $type, $theme, $path) {
  return array(
104
105
106
    'custom_block_add_list' => array(
      'variables' => array('content' => NULL),
      'file' => 'custom_block.pages.inc',
107
      'template' => 'custom-block-add-list',
108
    ),
109
110
111
  );
}

112
113
114
115
116
117
/**
 * Loads a custom block type.
 *
 * @param int $id
 *   The ID of the custom block type to load.
 *
118
 * @return \Drupal\custom_block\Entity\CustomBlockType|null
119
 *   A CustomBlockType object or NULL if the requested $id does not exist.
120
121
122
123
124
125
126
127
128
129
130
 */
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.
 *
131
 * @return \Drupal\custom_block\Entity\CustomBlock|null
132
 *   A CustomBlock object or NULL if the requested $id does not exist.
133
134
135
136
137
138
139
140
 */
function custom_block_load($id) {
  return entity_load('custom_block', $id);
}

/**
 * Implements hook_entity_info_alter().
 */
141
142
function custom_block_entity_info_alter(&$entity_info) {
  /** @var $entity_info \Drupal\Core\Entity\EntityTypeInterface[] */
143
  // Add a translation handler for fields if the language module is enabled.
144
  if (\Drupal::moduleHandler()->moduleExists('language')) {
145
146
147
    $translation = $entity_info['custom_block']->get('translation');
    $translation['custom_block'] = TRUE;
    $entity_info['custom_block']->set('translation', $translation);
148
149
150
151
152
153
154
155
156
  }
}

/**
 * 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) {
157
    $config = \Drupal::config($config_name);
158
    $bundles['custom_block'][$config->get('id')]['label'] = $config->get('label');
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
  }
  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.
176
177
  $field = field_info_field('custom_block', 'body');
  $instance = field_info_instance('custom_block', 'body', $block_type_id);
178
  if (empty($field)) {
179
    $field = entity_create('field_entity', array(
180
      'name' => 'body',
181
      'entity_type' => 'custom_block',
182
      'type' => 'text_with_summary',
183
184
    ));
    $field->save();
185
186
  }
  if (empty($instance)) {
187
    $instance = entity_create('field_instance', array(
188
      'field_name' => 'body',
189
190
191
192
      'entity_type' => 'custom_block',
      'bundle' => $block_type_id,
      'label' => $label,
      'settings' => array('display_summary' => FALSE),
193
194
    ));
    $instance->save();
195

196
197
    // Assign widget settings for the 'default' form mode.
    entity_get_form_display('custom_block', $block_type_id, 'default')
198
      ->setComponent('body', array(
199
200
201
202
        'type' => 'text_textarea_with_summary',
      ))
      ->save();

203
204
    // Assign display settings for 'default' view mode.
    entity_get_display('custom_block', $block_type_id, 'default')
205
      ->setComponent('body', array(
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
        '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,
222
    'block/*' => TRUE,
223
    'block/*/delete' => TRUE,
224
    'admin/structure/block/custom-blocks/*' => TRUE,
225
226
227
  );
  return $paths;
}