e2abb6e6af84a9512595e78c0eafb926e97c04a6
[yaffs-website] / web / core / modules / block_content / block_content.module
1 <?php
2
3 /**
4  * @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  * Implements hook_help().
14  */
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', ['name' => 'field_ui']) : '#';
19       $output = '';
20       $output .= '<h3>' . t('About') . '</h3>';
21       $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>.', [':block-library' => \Drupal::url('entity.block_content.collection'), ':block-content' => \Drupal::url('entity.block_content.collection'), ':field-help' => \Drupal::url('help.page', ['name' => 'field']), ':blocks' => \Drupal::url('help.page', ['name' => 'block']), ':online-help' => 'https://www.drupal.org/documentation/modules/block_content']) . '</p>';
22       $output .= '<h3>' . t('Uses') . '</h3>';
23       $output .= '<dl>';
24       $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">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>.', [':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 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.', [':blocks' => \Drupal::url('block.admin_display'), ':block-library' => \Drupal::url('entity.block_content.collection'), ':block_help' => \Drupal::url('help.page', ['name' => 'block'])]) . '</dd>';
28       $output .= '</dl>';
29       return $output;
30
31     case 'entity.block_content.collection':
32       $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.', [':types' => \Drupal::url('entity.block_content_type.collection'), ':blocks' => \Drupal::url('block.admin_display')]) . '</p>';
33       return $output;
34
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">Blocks</a> page in the custom block library.', [':block-library' => \Drupal::url('entity.block_content.collection')]) . '</p>';
37       return $output;
38
39   }
40 }
41
42 /**
43  * Implements hook_theme().
44  */
45 function block_content_theme($existing, $type, $theme, $path) {
46   return [
47     'block_content_add_list' => [
48       'variables' => ['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     $translation = $entity_types['block_content']->get('translation');
62     $translation['block_content'] = TRUE;
63     $entity_types['block_content']->set('translation', $translation);
64   }
65 }
66
67 /**
68  * Adds the default body field to a custom block type.
69  *
70  * @param string $block_type_id
71  *   Id of the block type.
72  * @param string $label
73  *   (optional) The label for the body instance. Defaults to 'Body'
74  *
75  * @return \Drupal\field\Entity\FieldConfig
76  *   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   $field = FieldConfig::loadByName('block_content', $block_type_id, 'body');
81   if (empty($field)) {
82     $field = FieldConfig::create([
83       'field_storage' => FieldStorageConfig::loadByName('block_content', 'body'),
84       'bundle' => $block_type_id,
85       'label' => $label,
86       'settings' => ['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', [
93         'type' => 'text_textarea_with_summary',
94       ])
95       ->save();
96
97     // Assign display settings for 'default' view mode.
98     entity_get_display('block_content', $block_type_id, 'default')
99       ->setComponent('body', [
100         'label' => 'hidden',
101         'type' => 'text_default',
102       ])
103       ->save();
104   }
105
106   return $field;
107 }