1. 8.3.x core/modules/block_content/block_content.module
  2. 8.0.x core/modules/block_content/block_content.module
  3. 8.1.x core/modules/block_content/block_content.module
  4. 8.2.x core/modules/block_content/block_content.module
  5. 8.4.x core/modules/block_content/block_content.module

Allows the creation of custom blocks through the user interface.

File

core/modules/block_content/block_content.module
View source
  1. <?php
  2. /**
  3. * @file
  4. * Allows the creation of custom blocks through the user interface.
  5. */
  6. use Drupal\Core\Routing\RouteMatchInterface;
  7. use Drupal\field\Entity\FieldConfig;
  8. use Drupal\field\Entity\FieldStorageConfig;
  9. /**
  10. * Implements hook_help().
  11. */
  12. function block_content_help($route_name, RouteMatchInterface $route_match) {
  13. switch ($route_name) {
  14. case 'help.page.block_content':
  15. $field_ui = \Drupal::moduleHandler()->moduleExists('field_ui') ? \Drupal::url('help.page', array('name' => 'field_ui')) : '#';
  16. $output = '';
  17. $output .= '<h3>' . t('About') . '</h3>';
  18. $output .= '<p>' . t('The Custom Block module allows you to create and manage custom <em>block types</em> and <em>content-containing blocks</em> from the <a href = ":block-library" >Custom block library<a/> page. 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</a> page 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>';
  19. $output .= '<h3>' . t('Uses') . '</h3>';
  20. $output .= '<dl>';
  21. $output .= '<dt>' . t('Creating and managing custom block types') . '</dt>';
  22. $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">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>';
  23. $output .= '<dt>' . t('Creating custom blocks') . '</dt>';
  24. $output .= '<dd>' . t('Users with the <em>Administer blocks</em> permission can create, edit, and delete custom blocks of each defined custom block type, from the <a href=":block-library">Blocks</a> page in the Custom block library. After creating a block, place it in a region from the <a href=":blocks">Block layout</a> page; 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>';
  25. $output .= '</dl>';
  26. return $output;
  27. case 'entity.block_content.collection':
  28. $output = '<p>' . t('Blocks in the block library belong to <a href=":types">Custom 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</a> page.', array(':types' => \Drupal::url('entity.block_content_type.collection'), ':blocks' => \Drupal::url('block.admin_display'))) . '</p>';
  29. return $output;
  30. case 'entity.block_content_type.collection':
  31. $output = '<p>' . t('Each block type has its own fields and display settings. Create blocks of each type on the <a href=":block-library">Blocks</a> page in the custom block library.', array(':block-library' => \Drupal::url('entity.block_content.collection'))) . '</p>';
  32. return $output;
  33. }
  34. }
  35. /**
  36. * Implements hook_theme().
  37. */
  38. function block_content_theme($existing, $type, $theme, $path) {
  39. return array(
  40. 'block_content_add_list' => array(
  41. 'variables' => array('content' => NULL),
  42. 'file' => 'block_content.pages.inc',
  43. ),
  44. );
  45. }
  46. /**
  47. * Implements hook_entity_type_alter().
  48. */
  49. function block_content_entity_type_alter(array &$entity_types) {
  50. /** @var $entity_types \Drupal\Core\Entity\EntityTypeInterface[] */
  51. // Add a translation handler for fields if the language module is enabled.
  52. if (\Drupal::moduleHandler()->moduleExists('language')) {
  53. $translation = $entity_types['block_content']->get('translation');
  54. $translation['block_content'] = TRUE;
  55. $entity_types['block_content']->set('translation', $translation);
  56. }
  57. }
  58. /**
  59. * Adds the default body field to a custom block type.
  60. *
  61. * @param string $block_type_id
  62. * Id of the block type.
  63. * @param string $label
  64. * (optional) The label for the body instance. Defaults to 'Body'
  65. *
  66. * @return \Drupal\field\Entity\FieldConfig
  67. * A Body field object.
  68. */
  69. function block_content_add_body_field($block_type_id, $label = 'Body') {
  70. // Add or remove the body field, as needed.
  71. $field = FieldConfig::loadByName('block_content', $block_type_id, 'body');
  72. if (empty($field)) {
  73. $field = FieldConfig::create([
  74. 'field_storage' => FieldStorageConfig::loadByName('block_content', 'body'),
  75. 'bundle' => $block_type_id,
  76. 'label' => $label,
  77. 'settings' => array('display_summary' => FALSE),
  78. ]);
  79. $field->save();
  80. // Assign widget settings for the 'default' form mode.
  81. entity_get_form_display('block_content', $block_type_id, 'default')
  82. ->setComponent('body', array(
  83. 'type' => 'text_textarea_with_summary',
  84. ))
  85. ->save();
  86. // Assign display settings for 'default' view mode.
  87. entity_get_display('block_content', $block_type_id, 'default')
  88. ->setComponent('body', array(
  89. 'label' => 'hidden',
  90. 'type' => 'text_default',
  91. ))
  92. ->save();
  93. }
  94. return $field;
  95. }

Functions

Namesort descending Description
block_content_add_body_field Adds the default body field to a custom block type.
block_content_entity_type_alter Implements hook_entity_type_alter().
block_content_help Implements hook_help().
block_content_theme Implements hook_theme().