block_content.module 5.66 KB
Newer Older
1 2 3 4
<?php

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

8
use Drupal\Core\Routing\RouteMatchInterface;
9
use Drupal\field\Entity\FieldConfig;
10
use Drupal\field\Entity\FieldStorageConfig;
11

12 13 14
/**
 * Implements hook_help().
 */
15
function block_content_help($route_name, RouteMatchInterface $route_match) {
16
  switch ($route_name) {
17
    case 'help.page.block_content':
18
      $field_ui = \Drupal::moduleHandler()->moduleExists('field_ui') ? \Drupal::url('help.page', array('name' => 'field_ui')) : '#';
19 20
      $output = '';
      $output .= '<h3>' . t('About') . '</h3>';
21
      $output .= '<p>' . t('The Custom Block module allows you to create custom <em>block types</em> and <em>content-containing blocks</em>, and provides a <a href=":block-library">Custom block library</a> listing all of them. Custom block types have fields; see the <a href=":field-help">Field module help</a> for more information. Once created, custom blocks can be placed in regions just like blocks provided by other modules; see the <a href=":blocks">Block module help page</a> for details. For more information, see the <a href=":online-help">online documentation for the Custom Block module</a>.', array(':block-library' => \Drupal::url('entity.block_content.collection'), ':block-content' => \Drupal::url('entity.block_content.collection'), ':field-help' => \Drupal::url('help.page', array('name' => 'field')), ':blocks' => \Drupal::url('help.page', array('name' => 'block')), ':online-help' => 'https://www.drupal.org/documentation/modules/block_content')) . '</p>';
22 23 24
      $output .= '<h3>' . t('Uses') . '</h3>';
      $output .= '<dl>';
      $output .= '<dt>' . t('Creating and managing custom block types') . '</dt>';
25
      $output .= '<dd>' . t('Users with the <em>Administer blocks</em> permission can create and edit custom block types with fields and display settings, from the <a href=":types">Custom block types</a> page in the Custom block library. For more information about managing fields and display settings, see the <a href=":field-ui">Field UI module help</a>.', array(':types' => \Drupal::url('entity.block_content_type.collection'), ':field-ui' => $field_ui)) . '</dd>';
26
      $output .= '<dt>' . t('Creating custom blocks') . '</dt>';
27
      $output .= '<dd>' . t('Users with the <em>Administer blocks</em> permission can create, edit, and delete custom blocks of each custom block type you have defined, from the <a href=":block-library">Custom block library</a> page. Custom blocks are shown in the <em>Place blocks</em> list on the <a href=":blocks">Block layout page</a>; see the <a href=":block_help">Block module help</a> for more information about placing blocks.', array(':blocks' => \Drupal::url('block.admin_display'), ':block-library' => \Drupal::url('entity.block_content.collection'), ':block_help' => \Drupal::url('help.page', array('name' => 'block')))) . '</dd>';
28 29
      $output .= '</dl>';
      return $output;
30

31
    case 'entity.block_content.collection':
32
      $output = '<p>' . t('Blocks in the block library belong to <a href=":types">block types</a>, each with its own fields and display settings. After creating a block, place it in a region from the <a href=":blocks">Block layout page</a>.', array(':types' => \Drupal::url('entity.block_content_type.collection'), ':blocks' => \Drupal::url('block.admin_display'))) . '</p>';
33 34
      return $output;

35
    case 'entity.block_content_type.collection':
36
      $output = '<p>' . t('Each block type has its own fields and display settings. Create blocks of each type on the <a href=":block-library">Block library page</a>.', array(':block-library' => \Drupal::url('entity.block_content.collection'))) . '</p>';
37 38 39 40 41
      return $output;

  }
}

42 43 44
/**
 * Implements hook_theme().
 */
45
function block_content_theme($existing, $type, $theme, $path) {
46
  return array(
47
    'block_content_add_list' => array(
48
      'variables' => array('content' => NULL),
49
      'file' => 'block_content.pages.inc',
50
    ),
51 52 53
  );
}

54
/**
55
 * Implements hook_entity_type_alter().
56
 */
57
function block_content_entity_type_alter(array &$entity_types) {
58
  /** @var $entity_types \Drupal\Core\Entity\EntityTypeInterface[] */
59
  // Add a translation handler for fields if the language module is enabled.
60
  if (\Drupal::moduleHandler()->moduleExists('language')) {
61 62 63
    $translation = $entity_types['block_content']->get('translation');
    $translation['block_content'] = TRUE;
    $entity_types['block_content']->set('translation', $translation);
64 65 66 67 68 69 70 71 72
  }
}

/**
 * Adds the default body field to a custom block type.
 *
 * @param string $block_type_id
 *   Id of the block type.
 * @param string $label
73
 *   (optional) The label for the body instance. Defaults to 'Body'
74
 *
75 76
 * @return \Drupal\field\Entity\FieldConfig
 *   A Body field object.
77
 */
78
function block_content_add_body_field($block_type_id, $label = 'Body') {
79
  // Add or remove the body field, as needed.
80 81 82
  $field = FieldConfig::loadByName('block_content', $block_type_id, 'body');
  if (empty($field)) {
    $field = entity_create('field_config', array(
83
      'field_storage' =>  FieldStorageConfig::loadByName('block_content', 'body'),
84 85 86
      'bundle' => $block_type_id,
      'label' => $label,
      'settings' => array('display_summary' => FALSE),
87
    ));
88
    $field->save();
89

90
    // Assign widget settings for the 'default' form mode.
91
    entity_get_form_display('block_content', $block_type_id, 'default')
92
      ->setComponent('body', array(
93 94 95 96
        'type' => 'text_textarea_with_summary',
      ))
      ->save();

97
    // Assign display settings for 'default' view mode.
98
    entity_get_display('block_content', $block_type_id, 'default')
99
      ->setComponent('body', array(
100 101 102 103 104 105
        'label' => 'hidden',
        'type' => 'text_default',
      ))
      ->save();
  }

106
  return $field;
107
}