8.5.x ContextAwarePluginAssignmentTrait.php ContextAwarePluginAssignmentTrait::addContextAssignmentElement(ContextAwarePluginInterface $plugin, array $contexts)
8.0.x ContextAwarePluginAssignmentTrait.php ContextAwarePluginAssignmentTrait::addContextAssignmentElement(ContextAwarePluginInterface $plugin, array $contexts)
8.1.x ContextAwarePluginAssignmentTrait.php ContextAwarePluginAssignmentTrait::addContextAssignmentElement(ContextAwarePluginInterface $plugin, array $contexts)
8.2.x ContextAwarePluginAssignmentTrait.php ContextAwarePluginAssignmentTrait::addContextAssignmentElement(ContextAwarePluginInterface $plugin, array $contexts)
8.3.x ContextAwarePluginAssignmentTrait.php ContextAwarePluginAssignmentTrait::addContextAssignmentElement(ContextAwarePluginInterface $plugin, array $contexts)
8.4.x ContextAwarePluginAssignmentTrait.php ContextAwarePluginAssignmentTrait::addContextAssignmentElement(ContextAwarePluginInterface $plugin, array $contexts)
8.6.x ContextAwarePluginAssignmentTrait.php ContextAwarePluginAssignmentTrait::addContextAssignmentElement(ContextAwarePluginInterface $plugin, array $contexts)

Builds a form element for assigning a context to a given slot.

Parameters

\Drupal\Core\Plugin\ContextAwarePluginInterface $plugin: The context-aware plugin.

\Drupal\Component\Plugin\Context\ContextInterface[] $contexts: An array of contexts.

Return value

array A form element for assigning context.

2 calls to ContextAwarePluginAssignmentTrait::addContextAssignmentElement()
BlockBase::buildConfigurationForm in core/lib/Drupal/Core/Block/BlockBase.php
Creates a generic configuration form for all block types. Individual block plugins can add elements to this form by overriding BlockBase::blockForm(). Most block plugins should not override this method unless they need to alter the generic form elements.
ConditionPluginBase::buildConfigurationForm in core/lib/Drupal/Core/Condition/ConditionPluginBase.php
Form constructor.

File

core/lib/Drupal/Core/Plugin/ContextAwarePluginAssignmentTrait.php, line 37

Class

ContextAwarePluginAssignmentTrait
Handles context assignments for context-aware plugins.

Namespace

Drupal\Core\Plugin

Code

protected function addContextAssignmentElement(ContextAwarePluginInterface $plugin, array $contexts) {
  $element = [];
  foreach ($plugin
    ->getContextDefinitions() as $context_slot => $definition) {
    $valid_contexts = $this
      ->contextHandler()
      ->getMatchingContexts($contexts, $definition);
    $options = [];
    foreach ($valid_contexts as $context_id => $context) {
      $element['#tree'] = TRUE;
      $options[$context_id] = $context
        ->getContextDefinition()
        ->getLabel();
      $element[$context_slot] = [
        '#type' => 'value',
        '#value' => $context_id,
      ];
    }
    if (count($options) > 1 || !$definition
      ->isRequired()) {
      $assignments = $plugin
        ->getContextMapping();
      $element[$context_slot] = [
        '#title' => $definition
          ->getLabel() ?: $this
          ->t('Select a @context value:', [
          '@context' => $context_slot,
        ]),
        '#type' => 'select',
        '#options' => $options,
        '#required' => $definition
          ->isRequired(),
        '#default_value' => !empty($assignments[$context_slot]) ? $assignments[$context_slot] : '',
        '#description' => $definition
          ->getDescription(),
      ];
      if (!$definition
        ->isRequired()) {
        $element[$context_slot]['#empty_value'] = '';
      }
    }
  }
  return $element;
}