block_content.module
Same filename and directory in other branches
File
-
core/
modules/ block_content/ block_content.module
View source
<?php
/**
* @file
*/
use Drupal\Core\Link;
use Drupal\Core\Url;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
/**
* Adds the default body field to a 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 \Drupal\field\Entity\FieldConfig
* A Body field object.
*
* @deprecated in drupal:11.3.0 and is removed from drupal:12.0.0. There is no replacement.
* @see https://www.drupal.org/node/3535528
*/
function block_content_add_body_field($block_type_id, $label = 'Body') {
@trigger_error(__FUNCTION__ . '() is deprecated in drupal:11.3.0 and is removed from drupal:12.0.0. There is no replacement. See https://www.drupal.org/node/3535528', E_USER_DEPRECATED);
// Add or remove the body field, as needed.
$field = FieldConfig::loadByName('block_content', $block_type_id, 'body');
if (empty($field)) {
$field = FieldConfig::create([
'field_storage' => FieldStorageConfig::loadByName('block_content', 'body'),
'bundle' => $block_type_id,
'label' => $label,
'settings' => [
'display_summary' => FALSE,
'allowed_formats' => [],
],
]);
$field->save();
/** @var \Drupal\Core\Entity\EntityDisplayRepositoryInterface $display_repository */
$display_repository = \Drupal::service('entity_display.repository');
// Assign widget settings for the default form mode.
$display_repository->getFormDisplay('block_content', $block_type_id)
->setComponent('body', [
'type' => 'text_textarea_with_summary',
])
->save();
// Assign display settings for default view mode.
$display_repository->getViewDisplay('block_content', $block_type_id)
->setComponent('body', [
'label' => 'hidden',
'type' => 'text_default',
])
->save();
}
return $field;
}
/**
* Prepares variables for a block type creation list templates.
*
* Default template: block-content-add-list.html.twig.
*
* @param array $variables
* An associative array containing:
* - content: An array of block types.
*
* @see block_content_add_page()
*/
function template_preprocess_block_content_add_list(&$variables) : void {
@trigger_error(__FUNCTION__ . '() is deprecated in drupal:11.3.0 and is removed from drupal:12.0.0. Use entity_add_list instead. See https://www.drupal.org/node/3530643', E_USER_DEPRECATED);
$variables['types'] = [];
$query = \Drupal::request()->query
->all();
foreach ($variables['content'] as $type) {
$variables['types'][$type->id()] = [
'link' => Link::fromTextAndUrl($type->label(), Url::fromRoute('block_content.add_form', [
'block_content_type' => $type->id(),
], [
'query' => $query,
]))
->toString(),
'description' => [
'#markup' => $type->getDescription(),
],
'title' => $type->label(),
'localized_options' => [
'query' => $query,
],
];
}
}
Functions
| Title | Deprecated | Summary |
|---|---|---|
| block_content_add_body_field | in drupal:11.3.0 and is removed from drupal:12.0.0. There is no replacement. |
Adds the default body field to a block type. |
| template_preprocess_block_content_add_list | Prepares variables for a block type creation list templates. |
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.