summaryrefslogtreecommitdiffstats
path: root/core/modules/block/custom_block/custom_block.module
blob: 00caf8e5a1d4edf3bdbf5d24d05a1d3e9928aa7d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
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
77
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
114
115
116
117
<?php

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

use Symfony\Component\HttpFoundation\Request;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldInstanceConfig;

/**
 * Implements hook_help().
 */
function custom_block_help($route_name, Request $request) {
  switch ($route_name) {
    case 'help.page.custom_block':
      $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;

    case 'custom_block.list':
      $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 'custom_block.type_list':
      $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;

  }
}

/**
 * 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',
    ),
  );
}

/**
 * Implements hook_entity_type_alter().
 */
function custom_block_entity_type_alter(array &$entity_types) {
  /** @var $entity_types \Drupal\Core\Entity\EntityTypeInterface[] */
  // Add a translation handler for fields if the language module is enabled.
  if (\Drupal::moduleHandler()->moduleExists('language')) {
    $translation = $entity_types['custom_block']->get('translation');
    $translation['custom_block'] = TRUE;
    $entity_types['custom_block']->set('translation', $translation);
  }
}

/**
 * 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 'Body'
 *
 * @return array()
 *   Body field instance.
 */
function custom_block_add_body_field($block_type_id, $label = 'Body') {
  // Add or remove the body field, as needed.
  $field = FieldConfig::loadByName('custom_block', 'body');
  $instance = FieldInstanceConfig::loadByName('custom_block', $block_type_id, 'body');
  if (empty($field)) {
    $field = entity_create('field_config', array(
      'name' => 'body',
      'entity_type' => 'custom_block',
      'type' => 'text_with_summary',
    ));
    $field->save();
  }
  if (empty($instance)) {
    $instance = entity_create('field_instance_config', array(
      'field_name' => 'body',
      'entity_type' => 'custom_block',
      'bundle' => $block_type_id,
      'label' => $label,
      'settings' => array('display_summary' => FALSE),
    ));
    $instance->save();

    // Assign widget settings for the 'default' form mode.
    entity_get_form_display('custom_block', $block_type_id, 'default')
      ->setComponent('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('body', array(
        'label' => 'hidden',
        'type' => 'text_default',
      ))
      ->save();
  }

  return $instance;
}