setContextMapping example

/** * {@inheritdoc} */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    // Call the plugin submit handler.     $subform_state = SubformState::createForSubform($form['layout_settings']$form$form_state);
    $this->getPluginForm($this->layout)->submitConfigurationForm($form['layout_settings']$subform_state);

    // If this layout is context-aware, set the context mapping.     if ($this->layout instanceof ContextAwarePluginInterface) {
      $this->layout->setContextMapping($subform_state->getValue('context_mapping', []));
    }

    $configuration = $this->layout->getConfiguration();

    $section = $this->getCurrentSection();
    $section->setLayoutSettings($configuration);
    if (!$this->isUpdate) {
      $this->sectionStorage->insertSection($this->delta, $section);
    }

    $this->layoutTempstoreRepository->set($this->sectionStorage);
    
/** * {@inheritdoc} */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    // Call the plugin submit handler.     $subform_state = SubformState::createForSubform($form['settings']$form$form_state);
    $this->getPluginForm($this->block)->submitConfigurationForm($form$subform_state);

    // If this block is context-aware, set the context mapping.     if ($this->block instanceof ContextAwarePluginInterface) {
      $this->block->setContextMapping($subform_state->getValue('context_mapping', []));
    }

    $configuration = $this->block->getConfiguration();

    $section = $this->sectionStorage->getSection($this->delta);
    $section->getComponent($this->uuid)->setConfiguration($configuration);

    $this->layoutTempstoreRepository->set($this->sectionStorage);
    $form_state->setRedirectUrl($this->sectionStorage->getLayoutBuilderUrl());
  }

  

  public function validateConfigurationForm(array &$form, FormStateInterface $form_state) {
  }

  /** * {@inheritdoc} */
  public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
    $this->configuration['negate'] = $form_state->getValue('negate');
    if ($form_state->hasValue('context_mapping')) {
      $this->setContextMapping($form_state->getValue('context_mapping'));
    }
  }

  /** * {@inheritdoc} */
  public function execute() {
    return $this->executableManager->execute($this);
  }

  /** * {@inheritdoc} */
$this->doTestDifferentUser();
  }

  /** * Tests with both contexts mapped to the same user. */
  protected function doTestIdenticalUser() {
    /** @var \Drupal\Core\Condition\ConditionPluginBase $condition */
    $condition = \Drupal::service('plugin.manager.condition')
      ->createInstance('condition_test_dual_user')
      // Map the anonymous user to both contexts.       ->setContextMapping([
        'user1' => 'anonymous',
        'user2' => 'anonymous',
      ]);
    $contexts['anonymous'] = EntityContext::fromEntity($this->anonymous);
    \Drupal::service('context.handler')->applyContextMapping($condition$contexts);
    $this->assertTrue($condition->execute());
  }

  /** * Tests with each context mapped to different users. */
  

  protected static $modules = ['system', 'user', 'condition_test', 'node'];

  /** * Tests with both contexts mapped to the same user. */
  public function testContextMissing() {
    /** @var \Drupal\Core\Condition\ConditionPluginBase $condition */
    $condition = \Drupal::service('plugin.manager.condition')
      ->createInstance('condition_test_optional_context')
      ->setContextMapping([
        'node' => 'node',
      ]);
    \Drupal::service('context.handler')->applyContextMapping($condition[]);
    $this->assertTrue($condition->execute());
  }

  /** * Tests with both contexts mapped to the same user. */
  public function testContextNoValue() {
    /** @var \Drupal\Core\Condition\ConditionPluginBase $condition */
    
$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);

    // Save the settings of the plugin.     $entity->save();

    $this->messenger()->addStatus($this->t('The block configuration has been saved.'));
    $form_state->setRedirect(
      'block.admin_display_theme',
      [
        
Home | Imprint | This part of the site doesn't use cookies.