class BlockBase

Same name and namespace in other branches
  1. 11.x core/lib/Drupal/Core/Block/BlockBase.php \Drupal\Core\Block\BlockBase
  2. 10 core/lib/Drupal/Core/Block/BlockBase.php \Drupal\Core\Block\BlockBase
  3. 9 core/lib/Drupal/Core/Block/BlockBase.php \Drupal\Core\Block\BlockBase
  4. 8.9.x core/lib/Drupal/Core/Block/BlockBase.php \Drupal\Core\Block\BlockBase

Defines a base block implementation that most blocks plugins will extend.

This abstract class provides the generic block configuration form, default block settings, and handling for general user-defined block visibility settings.

Hierarchy

Expanded class hierarchy of BlockBase

Related topics

57 files declare their use of BlockBase
AjaxFormBlock.php in core/modules/system/tests/modules/ajax_forms_test/src/Plugin/Block/AjaxFormBlock.php
AnnounceBlock.php in core/modules/announcements_feed/src/Plugin/Block/AnnounceBlock.php
AttachedRenderingBlock.php in core/modules/system/tests/modules/render_attached_test/src/Plugin/Block/AttachedRenderingBlock.php
AutowireBlock.php in core/modules/system/tests/modules/autowire_test/src/Plugin/Block/AutowireBlock.php
AutowireErrorBlock.php in core/modules/system/tests/modules/autowire_test/src/Plugin/Block/AutowireErrorBlock.php

... See full list

File

core/lib/Drupal/Core/Block/BlockBase.php, line 23

Namespace

Drupal\Core\Block
View source
abstract class BlockBase extends PluginBase implements BlockPluginInterface, PluginWithFormsInterface, PreviewAwarePluginInterface, PreviewFallbackInterface, ContextAwarePluginInterface {
  use BlockPluginTrait {
    buildConfigurationForm as traitBuildConfigurationForm;
    submitConfigurationForm as traitSubmitConfigurationForm;
  }
  use ContextAwarePluginTrait;
  use ContextAwarePluginAssignmentTrait;
  
  /**
   * {@inheritdoc}
   */
  public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
    $form = $this->traitBuildConfigurationForm($form, $form_state);
    // Add context mapping UI form elements.
    $contexts = $form_state->getTemporaryValue('gathered_contexts') ?: [];
    $form['context_mapping'] = $this->addContextAssignmentElement($this, $contexts);
    return $form;
  }
  
  /**
   * {@inheritdoc}
   */
  public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
    if (!$form_state->getErrors() && $form_state->getValue('context_mapping')) {
      $this->configuration['context_mapping'] = $form_state->getValue('context_mapping');
    }
    $this->traitSubmitConfigurationForm($form, $form_state);
  }

}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.