BlockBase.php 1.36 KB
Newer Older
1 2
<?php

3
namespace Drupal\Core\Block;
4

5
use Drupal\Core\Form\FormStateInterface;
6
use Drupal\Core\Plugin\ContextAwarePluginAssignmentTrait;
7 8 9
use Drupal\Core\Plugin\ContextAwarePluginInterface;
use Drupal\Core\Plugin\ContextAwarePluginTrait;
use Drupal\Core\Plugin\PluginBase;
10
use Drupal\Core\Plugin\PluginWithFormsInterface;
11
use Drupal\Core\Render\PreviewFallbackInterface;
12 13 14 15 16 17 18

/**
 * 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.
19 20
 *
 * @ingroup block_api
21
 */
22
abstract class BlockBase extends PluginBase implements BlockPluginInterface, PluginWithFormsInterface, PreviewFallbackInterface, ContextAwarePluginInterface {
23

24 25 26
  use BlockPluginTrait {
    buildConfigurationForm as traitBuildConfigurationForm;
  }
27
  use ContextAwarePluginTrait;
28
  use ContextAwarePluginAssignmentTrait;
29

30 31 32 33 34 35 36 37 38 39 40 41 42
  /**
   * {@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;
  }

43
}