Same name and namespace in other branches
- 8.9.x core/modules/block/src/BlockForm.php \Drupal\block\BlockForm
- 9 core/modules/block/src/BlockForm.php \Drupal\block\BlockForm
Provides form for block instance forms.
@internal
Hierarchy
- class \Drupal\Core\Form\FormBase implements ContainerInjectionInterface, FormInterface uses DependencySerializationTrait, LoggerChannelTrait, MessengerTrait, RedirectDestinationTrait, StringTranslationTrait
- class \Drupal\Core\Entity\EntityForm implements EntityFormInterface
- class \Drupal\block\BlockForm
- class \Drupal\Core\Entity\EntityForm implements EntityFormInterface
Expanded class hierarchy of BlockForm
2 files declare their use of BlockForm
- BlockEntitySettingTrayForm.php in core/
modules/ settings_tray/ src/ Block/ BlockEntitySettingTrayForm.php - BlockFormTest.php in core/
modules/ block/ tests/ src/ Unit/ BlockFormTest.php
File
- core/
modules/ block/ src/ BlockForm.php, line 25
Namespace
Drupal\blockView source
class BlockForm extends EntityForm {
/**
* The block entity.
*
* @var \Drupal\block\BlockInterface
*/
protected $entity;
/**
* The block storage.
*
* @var \Drupal\Core\Config\Entity\ConfigEntityStorageInterface
*/
protected $storage;
/**
* The condition plugin manager.
*
* @var \Drupal\Core\Condition\ConditionManager
*/
protected $manager;
/**
* The language manager service.
*
* @var \Drupal\Core\Language\LanguageManagerInterface
*/
protected $language;
/**
* The theme handler.
*
* @var \Drupal\Core\Extension\ThemeHandler
*/
protected $themeHandler;
/**
* The context repository service.
*
* @var \Drupal\Core\Plugin\Context\ContextRepositoryInterface
*/
protected $contextRepository;
/**
* The plugin form manager.
*
* @var \Drupal\Core\Plugin\PluginFormFactoryInterface
*/
protected $pluginFormFactory;
/**
* The block repository service.
*
* @var \Drupal\block\BlockRepositoryInterface
*/
protected $blockRepository;
/**
* Constructs a BlockForm object.
*
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* The entity type manager.
* @param \Drupal\Core\Executable\ExecutableManagerInterface $manager
* The ConditionManager for building the visibility UI.
* @param \Drupal\Core\Plugin\Context\ContextRepositoryInterface $context_repository
* The lazy context repository service.
* @param \Drupal\Core\Language\LanguageManagerInterface $language
* The language manager.
* @param \Drupal\Core\Extension\ThemeHandlerInterface $theme_handler
* The theme handler.
* @param \Drupal\Core\Plugin\PluginFormFactoryInterface $plugin_form_manager
* The plugin form manager.
* @param \Drupal\block\BlockRepositoryInterface|null $block_repository
* The block repository service.
*/
public function __construct(EntityTypeManagerInterface $entity_type_manager, ExecutableManagerInterface $manager, ContextRepositoryInterface $context_repository, LanguageManagerInterface $language, ThemeHandlerInterface $theme_handler, PluginFormFactoryInterface $plugin_form_manager, ?BlockRepositoryInterface $block_repository = NULL) {
if ($block_repository === NULL) {
@trigger_error('Calling ' . __METHOD__ . ' without the $block_repository argument is deprecated in drupal:10.2.0 and will be required in drupal:11.0.0. See https://www.drupal.org/node/3333575', E_USER_DEPRECATED);
$block_repository = \Drupal::service('block.repository');
}
$this->storage = $entity_type_manager
->getStorage('block');
$this->manager = $manager;
$this->contextRepository = $context_repository;
$this->language = $language;
$this->themeHandler = $theme_handler;
$this->pluginFormFactory = $plugin_form_manager;
$this->blockRepository = $block_repository;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static($container
->get('entity_type.manager'), $container
->get('plugin.manager.condition'), $container
->get('context.repository'), $container
->get('language_manager'), $container
->get('theme_handler'), $container
->get('plugin_form.factory'), $container
->get('block.repository'));
}
/**
* {@inheritdoc}
*/
public function form(array $form, FormStateInterface $form_state) {
$entity = $this->entity;
// Store the gathered contexts in the form state for other objects to use
// during form building.
$form_state
->setTemporaryValue('gathered_contexts', $this->contextRepository
->getAvailableContexts());
$form['#tree'] = TRUE;
$form['settings'] = [];
$subform_state = SubformState::createForSubform($form['settings'], $form, $form_state);
$form['settings'] = $this
->getPluginForm($entity
->getPlugin())
->buildConfigurationForm($form['settings'], $subform_state);
$form['visibility'] = $this
->buildVisibilityInterface([], $form_state);
// If creating a new block, calculate a safe default machine name.
$form['id'] = [
'#type' => 'machine_name',
'#maxlength' => 64,
'#description' => $this
->t('A unique name for this block instance. Must be alpha-numeric and underscore separated.'),
'#default_value' => !$entity
->isNew() ? $entity
->id() : $this
->getUniqueMachineName($entity),
'#machine_name' => [
'exists' => '\\Drupal\\block\\Entity\\Block::load',
'replace_pattern' => '[^a-z0-9_.]+',
'source' => [
'settings',
'label',
],
],
'#required' => TRUE,
'#disabled' => !$entity
->isNew(),
];
// Theme settings.
if ($theme = $entity
->getTheme()) {
$form['theme'] = [
'#type' => 'value',
'#value' => $theme,
];
}
else {
$theme = $this
->config('system.theme')
->get('default');
$theme_options = [];
foreach ($this->themeHandler
->listInfo() as $theme_name => $theme_info) {
if (!empty($theme_info->status)) {
$theme_options[$theme_name] = $theme_info->info['name'];
}
}
$form['theme'] = [
'#type' => 'select',
'#options' => $theme_options,
'#title' => $this
->t('Theme'),
'#default_value' => $theme,
'#ajax' => [
'callback' => '::themeSwitch',
'wrapper' => 'edit-block-region-wrapper',
],
];
}
// Hidden weight setting.
$weight = $entity
->isNew() ? $this
->getRequest()->query
->get('weight', 0) : $entity
->getWeight();
$form['weight'] = [
'#type' => 'hidden',
'#default_value' => $weight,
];
// Region settings.
$entity_region = $entity
->getRegion();
$region = $entity
->isNew() ? $this
->getRequest()->query
->get('region', $entity_region) : $entity_region;
$form['region'] = [
'#type' => 'select',
'#title' => $this
->t('Region'),
'#description' => $this
->t('Select the region where this block should be displayed.'),
'#default_value' => $region,
'#required' => TRUE,
'#options' => system_region_list($form_state
->getValue('theme', $theme), REGIONS_VISIBLE),
'#prefix' => '<div id="edit-block-region-wrapper">',
'#suffix' => '</div>',
];
$form['#attached']['library'][] = 'block/drupal.block.admin';
return $form;
}
/**
* Handles switching the available regions based on the selected theme.
*/
public function themeSwitch($form, FormStateInterface $form_state) {
return $form['region'];
}
/**
* Helper function for building the visibility UI form.
*
* @param array $form
* An associative array containing the structure of the form.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The current state of the form.
*
* @return array
* The form array with the visibility UI added in.
*/
protected function buildVisibilityInterface(array $form, FormStateInterface $form_state) {
$form['visibility_tabs'] = [
'#type' => 'vertical_tabs',
'#title' => $this
->t('Visibility'),
'#parents' => [
'visibility_tabs',
],
'#attached' => [
'library' => [
'block/drupal.block',
],
],
];
// @todo Allow list of conditions to be configured in
// https://www.drupal.org/node/2284687.
$visibility = $this->entity
->getVisibility();
$definitions = $this->manager
->getFilteredDefinitions('block_ui', $form_state
->getTemporaryValue('gathered_contexts'), [
'block' => $this->entity,
]);
foreach ($definitions as $condition_id => $definition) {
// Don't display the current theme condition.
if ($condition_id == 'current_theme') {
continue;
}
// Don't display the language condition until we have multiple languages.
if ($condition_id == 'language' && !$this->language
->isMultilingual()) {
continue;
}
/** @var \Drupal\Core\Condition\ConditionInterface $condition */
$condition = $this->manager
->createInstance($condition_id, $visibility[$condition_id] ?? []);
$form_state
->set([
'conditions',
$condition_id,
], $condition);
$condition_form = $condition
->buildConfigurationForm([], $form_state);
$condition_form['#type'] = 'details';
$condition_form['#title'] = $condition
->getPluginDefinition()['label'];
$condition_form['#group'] = 'visibility_tabs';
$form[$condition_id] = $condition_form;
}
// Disable negation for specific conditions.
$disable_negation = [
'entity_bundle:node',
'language',
'response_status',
'user_role',
];
foreach ($disable_negation as $condition) {
if (isset($form[$condition])) {
$form[$condition]['negate']['#type'] = 'value';
$form[$condition]['negate']['#value'] = $form[$condition]['negate']['#default_value'];
}
}
if (isset($form['user_role'])) {
$form['user_role']['#title'] = $this
->t('Roles');
unset($form['user_role']['roles']['#description']);
}
if (isset($form['request_path'])) {
$form['request_path']['#title'] = $this
->t('Pages');
$form['request_path']['negate']['#type'] = 'radios';
$form['request_path']['negate']['#default_value'] = (int) $form['request_path']['negate']['#default_value'];
$form['request_path']['negate']['#title_display'] = 'invisible';
$form['request_path']['negate']['#options'] = [
$this
->t('Show for the listed pages'),
$this
->t('Hide for the listed pages'),
];
}
return $form;
}
/**
* {@inheritdoc}
*/
protected function actions(array $form, FormStateInterface $form_state) {
$actions = parent::actions($form, $form_state);
$actions['submit']['#value'] = $this
->t('Save block');
$actions['delete']['#title'] = $this
->t('Remove block');
return $actions;
}
/**
* {@inheritdoc}
*/
public function validateForm(array &$form, FormStateInterface $form_state) {
parent::validateForm($form, $form_state);
$form_state
->setValue('weight', (int) $form_state
->getValue('weight'));
// The Block Entity form puts all block plugin form elements in the
// settings form element, so just pass that to the block for validation.
$this
->getPluginForm($this->entity
->getPlugin())
->validateConfigurationForm($form['settings'], SubformState::createForSubform($form['settings'], $form, $form_state));
$this
->validateVisibility($form, $form_state);
}
/**
* Helper function to independently validate the visibility UI.
*
* @param array $form
* A nested array form elements comprising the form.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The current state of the form.
*/
protected function validateVisibility(array $form, FormStateInterface $form_state) {
// Validate visibility condition settings.
foreach ($form_state
->getValue('visibility') as $condition_id => $values) {
// Allow the condition to validate the form.
$condition = $form_state
->get([
'conditions',
$condition_id,
]);
$condition
->validateConfigurationForm($form['visibility'][$condition_id], SubformState::createForSubform($form['visibility'][$condition_id], $form, $form_state));
}
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
parent::submitForm($form, $form_state);
$entity = $this->entity;
// The Block Entity form puts all block plugin form elements in the
// settings form element, so just pass that to the block for submission.
$sub_form_state = SubformState::createForSubform($form['settings'], $form, $form_state);
// Call the plugin submit handler.
$block = $entity
->getPlugin();
$this
->getPluginForm($block)
->submitConfigurationForm($form, $sub_form_state);
// If this block is context-aware, set the context mapping.
if ($block instanceof ContextAwarePluginInterface && $block
->getContextDefinitions()) {
$context_mapping = $sub_form_state
->getValue('context_mapping', []);
$block
->setContextMapping($context_mapping);
}
$this
->submitVisibility($form, $form_state);
}
/**
* {@inheritdoc}
*/
public function save(array $form, FormStateInterface $form_state) {
$value = parent::save($form, $form_state);
$this
->messenger()
->addStatus($this
->t('The block configuration has been saved.'));
$form_state
->setRedirect('block.admin_display_theme', [
'theme' => $form_state
->getValue('theme'),
], [
'query' => [
'block-placement' => Html::getClass($this->entity
->id()),
],
]);
return $value;
}
/**
* Helper function to independently submit the visibility UI.
*
* @param array $form
* A nested array form elements comprising the form.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The current state of the form.
*/
protected function submitVisibility(array $form, FormStateInterface $form_state) {
foreach ($form_state
->getValue('visibility') as $condition_id => $values) {
// Allow the condition to submit the form.
$condition = $form_state
->get([
'conditions',
$condition_id,
]);
$condition
->submitConfigurationForm($form['visibility'][$condition_id], SubformState::createForSubform($form['visibility'][$condition_id], $form, $form_state));
$condition_configuration = $condition
->getConfiguration();
// Update the visibility conditions on the block.
$this->entity
->getVisibilityConditions()
->addInstanceId($condition_id, $condition_configuration);
}
}
/**
* Generates a unique machine name for a block based on a suggested string.
*
* @param \Drupal\block\BlockInterface $block
* The block entity.
*
* @return string
* Returns the unique name.
*/
public function getUniqueMachineName(BlockInterface $block) {
$suggestion = $block
->getPlugin()
->getMachineNameSuggestion();
return $this->blockRepository
->getUniqueMachineName($suggestion, $block
->getTheme());
}
/**
* Retrieves the plugin form for a given block and operation.
*
* @param \Drupal\Core\Block\BlockPluginInterface $block
* The block plugin.
*
* @return \Drupal\Core\Plugin\PluginFormInterface
* The plugin form for the block.
*/
protected function getPluginForm(BlockPluginInterface $block) {
if ($block instanceof PluginWithFormsInterface) {
return $this->pluginFormFactory
->createInstance($block, 'configure');
}
return $block;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
DependencySerializationTrait:: |
public | function | 2 | |
DependencySerializationTrait:: |
public | function | 2 | |
DependencySerializationTrait:: |
protected | property | ||
DependencySerializationTrait:: |
protected | property | ||
EntityForm:: |
public | function |
Builds an updated entity object based upon the submitted form values. Overrides EntityFormInterface:: |
4 |
BlockForm:: |
public | function | Constructs a BlockForm object. | |
EntityForm:: |
protected | function | Copies top-level form values to entity properties. | 9 |
EntityForm:: |
public | function |
Determines which entity will be used by this form from a RouteMatch object. Overrides EntityFormInterface:: |
1 |
EntityForm:: |
public | function |
Form constructor. Overrides FormInterface:: |
10 |
EntityForm:: |
public | function | Form element #after_build callback: Updates the entity with submitted data. | |
BlockForm:: |
public | function |
Form submission handler for the 'save' action. Overrides EntityForm:: |
|
BlockForm:: |
public | function |
Form validation handler. Overrides FormBase:: |
|
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
BlockForm:: |
public | function | Generates a unique machine name for a block based on a suggested string. | |
BlockForm:: |
public | function |
Gets the actual form array to be built. Overrides EntityForm:: |
1 |
FormBase:: |
protected | function | Gets the config factory for this form. | 2 |
FormBase:: |
protected | function | Gets the current user. | |
EntityForm:: |
public | function |
Gets the form entity. Overrides EntityFormInterface:: |
|
FormBase:: |
protected | function | Gets the logger for a specific channel. | |
LoggerChannelTrait:: |
protected | function | Gets the logger for a specific channel. | |
MessengerTrait:: |
public | function | Gets the messenger. | 8 |
EntityForm:: |
public | function |
Gets the operation identifying the form. Overrides EntityFormInterface:: |
|
FormBase:: |
protected | function | Gets the request object. | |
FormBase:: |
protected | function | Gets the route match. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
BlockForm:: |
public | function | Handles switching the available regions based on the selected theme. | |
BlockForm:: |
protected | function | Helper function for building the visibility UI form. | 1 |
BlockForm:: |
protected | function | Helper function to independently submit the visibility UI. | 1 |
BlockForm:: |
protected | function | Helper function to independently validate the visibility UI. | 1 |
EntityForm:: |
protected | function | Initialize the form state and the entity before the first form build. | 2 |
LoggerChannelTrait:: |
public | function | Injects the logger channel factory. | |
BlockForm:: |
public static | function |
Instantiates a new instance of this class. Overrides FormBase:: |
|
EntityForm:: |
protected | function | Invokes the specified prepare hook variant. | |
RedirectDestinationTrait:: |
protected | function | Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url. | |
EntityForm:: |
protected | function | Prepares the entity object before the form is built first. | 2 |
EntityForm:: |
public | function | Process callback: assigns weights and hides extra fields. | |
FormBase:: |
public | function | Resets the configuration factory. | |
FormBase:: |
protected | function | Retrieves a configuration object. | |
BlockForm:: |
protected | function | Retrieves the plugin form for a given block and operation. | 1 |
FormBase:: |
protected | function | Returns a redirect response object for the specified route. | |
EntityForm:: |
public | function |
Returns a string identifying the base form. Overrides BaseFormIdInterface:: |
4 |
EntityForm:: |
public | function |
Returns a unique string identifying the form. Overrides FormInterface:: |
12 |
BlockForm:: |
protected | function |
Returns an array of supported actions for the current entity form. Overrides EntityForm:: |
1 |
EntityForm:: |
protected | function | Returns the action form element for the current entity form. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
RedirectDestinationTrait:: |
protected | function | Returns the redirect destination service. | |
FormBase:: |
private | function | Returns the service container. | |
FormBase:: |
public | function | Sets the config factory for this form. | |
EntityForm:: |
public | function |
Sets the entity type manager for this form. Overrides EntityFormInterface:: |
|
EntityForm:: |
public | function |
Sets the form entity. Overrides EntityFormInterface:: |
|
MessengerTrait:: |
public | function | Sets the messenger. | |
EntityForm:: |
public | function |
Sets the module handler for this form. Overrides EntityFormInterface:: |
|
EntityForm:: |
public | function |
Sets the operation for this form. Overrides EntityFormInterface:: |
|
RedirectDestinationTrait:: |
public | function | Sets the redirect destination service. | |
FormBase:: |
public | function | Sets the request stack object to use. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 1 |
BlockForm:: |
protected | property |
The block entity. Overrides EntityForm:: |
|
BlockForm:: |
protected | property | The block repository service. | |
BlockForm:: |
protected | property | The block storage. | |
BlockForm:: |
protected | property | The condition plugin manager. | |
FormBase:: |
protected | property | The config factory. | 2 |
BlockForm:: |
protected | property | The context repository service. | |
EntityForm:: |
protected | property | The entity type manager. | 3 |
BlockForm:: |
protected | property | The language manager service. | |
LoggerChannelTrait:: |
protected | property | The logger channel factory service. | |
MessengerTrait:: |
protected | property | The messenger. | 8 |
EntityForm:: |
protected | property | The module handler service. | 2 |
EntityForm:: |
protected | property | The name of the current operation. | |
BlockForm:: |
protected | property | The plugin form manager. | |
RedirectDestinationTrait:: |
protected | property | The redirect destination service. | 1 |
FormBase:: |
protected | property | The request stack. | 1 |
FormBase:: |
protected | property | The route match. | |
StringTranslationTrait:: |
protected | property | The string translation service. | 3 |
BlockForm:: |
protected | property | The theme handler. | |
BlockForm:: |
public | function |
This is the default entity object builder function. It is called before any
other submit handler to build the new entity object to be used by the
following submit handlers. At this point of the form workflow the entity is
validated and the form state… Overrides EntityForm:: |
|
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. |