annotate core/modules/block_content/block_content.module @ 0:c75dbcec494b

Initial commit from drush-created site
author Chris Cannam
date Thu, 05 Jul 2018 14:24:15 +0000
parents
children a9cd425dd02b
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 /**
Chris@0 4 * @file
Chris@0 5 * Allows the creation of custom blocks through the user interface.
Chris@0 6 */
Chris@0 7
Chris@0 8 use Drupal\Core\Routing\RouteMatchInterface;
Chris@0 9 use Drupal\field\Entity\FieldConfig;
Chris@0 10 use Drupal\field\Entity\FieldStorageConfig;
Chris@0 11
Chris@0 12 /**
Chris@0 13 * Implements hook_help().
Chris@0 14 */
Chris@0 15 function block_content_help($route_name, RouteMatchInterface $route_match) {
Chris@0 16 switch ($route_name) {
Chris@0 17 case 'help.page.block_content':
Chris@0 18 $field_ui = \Drupal::moduleHandler()->moduleExists('field_ui') ? \Drupal::url('help.page', ['name' => 'field_ui']) : '#';
Chris@0 19 $output = '';
Chris@0 20 $output .= '<h3>' . t('About') . '</h3>';
Chris@0 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>';
Chris@0 22 $output .= '<h3>' . t('Uses') . '</h3>';
Chris@0 23 $output .= '<dl>';
Chris@0 24 $output .= '<dt>' . t('Creating and managing custom block types') . '</dt>';
Chris@0 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>';
Chris@0 26 $output .= '<dt>' . t('Creating custom blocks') . '</dt>';
Chris@0 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>';
Chris@0 28 $output .= '</dl>';
Chris@0 29 return $output;
Chris@0 30
Chris@0 31 case 'entity.block_content.collection':
Chris@0 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>';
Chris@0 33 return $output;
Chris@0 34
Chris@0 35 case 'entity.block_content_type.collection':
Chris@0 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>';
Chris@0 37 return $output;
Chris@0 38
Chris@0 39 }
Chris@0 40 }
Chris@0 41
Chris@0 42 /**
Chris@0 43 * Implements hook_theme().
Chris@0 44 */
Chris@0 45 function block_content_theme($existing, $type, $theme, $path) {
Chris@0 46 return [
Chris@0 47 'block_content_add_list' => [
Chris@0 48 'variables' => ['content' => NULL],
Chris@0 49 'file' => 'block_content.pages.inc',
Chris@0 50 ],
Chris@0 51 ];
Chris@0 52 }
Chris@0 53
Chris@0 54 /**
Chris@0 55 * Implements hook_entity_type_alter().
Chris@0 56 */
Chris@0 57 function block_content_entity_type_alter(array &$entity_types) {
Chris@0 58 /** @var $entity_types \Drupal\Core\Entity\EntityTypeInterface[] */
Chris@0 59 // Add a translation handler for fields if the language module is enabled.
Chris@0 60 if (\Drupal::moduleHandler()->moduleExists('language')) {
Chris@0 61 $translation = $entity_types['block_content']->get('translation');
Chris@0 62 $translation['block_content'] = TRUE;
Chris@0 63 $entity_types['block_content']->set('translation', $translation);
Chris@0 64 }
Chris@0 65 }
Chris@0 66
Chris@0 67 /**
Chris@0 68 * Adds the default body field to a custom block type.
Chris@0 69 *
Chris@0 70 * @param string $block_type_id
Chris@0 71 * Id of the block type.
Chris@0 72 * @param string $label
Chris@0 73 * (optional) The label for the body instance. Defaults to 'Body'
Chris@0 74 *
Chris@0 75 * @return \Drupal\field\Entity\FieldConfig
Chris@0 76 * A Body field object.
Chris@0 77 */
Chris@0 78 function block_content_add_body_field($block_type_id, $label = 'Body') {
Chris@0 79 // Add or remove the body field, as needed.
Chris@0 80 $field = FieldConfig::loadByName('block_content', $block_type_id, 'body');
Chris@0 81 if (empty($field)) {
Chris@0 82 $field = FieldConfig::create([
Chris@0 83 'field_storage' => FieldStorageConfig::loadByName('block_content', 'body'),
Chris@0 84 'bundle' => $block_type_id,
Chris@0 85 'label' => $label,
Chris@0 86 'settings' => ['display_summary' => FALSE],
Chris@0 87 ]);
Chris@0 88 $field->save();
Chris@0 89
Chris@0 90 // Assign widget settings for the 'default' form mode.
Chris@0 91 entity_get_form_display('block_content', $block_type_id, 'default')
Chris@0 92 ->setComponent('body', [
Chris@0 93 'type' => 'text_textarea_with_summary',
Chris@0 94 ])
Chris@0 95 ->save();
Chris@0 96
Chris@0 97 // Assign display settings for 'default' view mode.
Chris@0 98 entity_get_display('block_content', $block_type_id, 'default')
Chris@0 99 ->setComponent('body', [
Chris@0 100 'label' => 'hidden',
Chris@0 101 'type' => 'text_default',
Chris@0 102 ])
Chris@0 103 ->save();
Chris@0 104 }
Chris@0 105
Chris@0 106 return $field;
Chris@0 107 }