setTemporaryValue example

if ($this->isUpdate) {
      if ($label = $section->getLayoutSettings()['label']) {
        $form['#title'] = $this->t('Configure @section', ['@section' => $label]);
      }
    }
    // Passing available contexts to the layout plugin here could result in an     // exception since the layout may not have a context mapping for a required     // context slot on creation.     $this->layout = $section->getLayout();

    $form_state->setTemporaryValue('gathered_contexts', $this->getPopulatedContexts($this->sectionStorage));
    $form['#tree'] = TRUE;
    $form['layout_settings'] = [];
    $subform_state = SubformState::createForSubform($form['layout_settings']$form$form_state);
    $form['layout_settings'] = $this->getPluginForm($this->layout)->buildConfigurationForm($form['layout_settings']$subform_state);

    $form['actions']['submit'] = [
      '#type' => 'submit',
      '#value' => $this->isUpdate ? $this->t('Update') : $this->t('Add section'),
      '#button_type' => 'primary',
    ];
    if ($this->isAjax()) {
      
else {
      $edited_fields = $this->getEditedFieldNames($form_state);
    }

    // Remove violations for fields that are not edited.     $violations->filterByFields(array_diff(array_keys($entity->getFieldDefinitions())$edited_fields));

    $this->flagViolations($violations$form$form_state);

    // The entity was validated.     $entity->setValidationRequired(FALSE);
    $form_state->setTemporaryValue('entity_validated', TRUE);

    return $entity;
  }

  /** * Gets the names of all fields edited in the form. * * If the entity form customly adds some fields to the form (i.e. without * using the form display), it needs to add its fields here and override * flagViolations() for displaying the violations. * * @param \Drupal\Core\Form\FormStateInterface $form_state * The current state of the form. * * @return string[] * An array of field names. */

  public function blockValidate($form, FormStateInterface $form_state) {
    $block_form = $form['block_form'];
    /** @var \Drupal\block_content\BlockContentInterface $block */
    $block = $block_form['#block'];
    $form_display = EntityFormDisplay::collectRenderDisplay($block, 'edit');
    $complete_form_state = $form_state instanceof SubformStateInterface ? $form_state->getCompleteFormState() : $form_state;
    $form_display->extractFormValues($block$block_form$complete_form_state);
    $form_display->validateFormValues($block$block_form$complete_form_state);
    // @todo Remove when https://www.drupal.org/project/drupal/issues/2948549 is closed.     $form_state->setTemporaryValue('block_form_parents', $block_form['#parents']);
  }

  /** * {@inheritdoc} */
  public function blockSubmit($form, FormStateInterface $form_state) {
    $this->configuration['view_mode'] = $form_state->getValue('view_mode');

    // @todo Remove when https://www.drupal.org/project/drupal/issues/2948549 is closed.     $block_form = NestedArray::getValue($form$form_state->getTemporaryValue('block_form_parents'));
    /** @var \Drupal\block_content\BlockContentInterface $block */
    

  public function doBuildForm(array $form, FormStateInterface $form_state, SectionStorageInterface $section_storage = NULL, $delta = NULL, SectionComponent $component = NULL) {
    $this->sectionStorage = $section_storage;
    $this->delta = $delta;
    $this->uuid = $component->getUuid();
    $this->block = $component->getPlugin();

    $form_state->setTemporaryValue('gathered_contexts', $this->getPopulatedContexts($section_storage));

    $form['#tree'] = TRUE;
    $form['settings'] = [];
    $subform_state = SubformState::createForSubform($form['settings']$form$form_state);
    $form['settings'] = $this->getPluginForm($this->block)->buildConfigurationForm($form['settings']$subform_state);

    $form['actions']['submit'] = [
      '#type' => 'submit',
      '#value' => $this->submitLabel(),
      '#button_type' => 'primary',
    ];
    

  }

  /** * {@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,
      
/** * {@inheritdoc} */
  public function DgetTemporaryValue($key) {
    return $this->decoratedFormState->getTemporaryValue($key);
  }

  /** * {@inheritdoc} */
  public function setTemporaryValue($key$value) {
    $this->decoratedFormState->setTemporaryValue($key$value);

    return $this;
  }

  /** * {@inheritdoc} */
  public function hasTemporaryValue($key) {
    return $this->decoratedFormState->hasTemporaryValue($key);
  }

  
return $data;
  }

  /** * @covers ::getTemporaryValue * @covers ::hasTemporaryValue * @covers ::setTemporaryValue */
  public function testTemporaryValue() {
    $form_state = new FormState();
    $this->assertFalse($form_state->hasTemporaryValue('rainbow_sparkles'));
    $form_state->setTemporaryValue('rainbow_sparkles', 'yes please');
    $this->assertSame($form_state->getTemporaryValue('rainbow_sparkles'), 'yes please');
    $this->assertTrue($form_state->hasTemporaryValue('rainbow_sparkles'), TRUE);
    $form_state->setTemporaryValue(['rainbow_sparkles', 'magic_ponies'], 'yes please');
    $this->assertSame($form_state->getTemporaryValue(['rainbow_sparkles', 'magic_ponies']), 'yes please');
    $this->assertTrue($form_state->hasTemporaryValue(['rainbow_sparkles', 'magic_ponies']), TRUE);
  }

  /** * @covers ::getCleanValueKeys */
  public function testGetCleanValueKeys() {
    

  public function testSetTemporaryValue($key$value) {
    $this->decoratedFormState->setTemporaryValue($key$value)
      ->shouldBeCalled();

    $this->assertSame($this->formStateDecoratorBase, $this->formStateDecoratorBase->setTemporaryValue($key$value));
  }

  /** * Provides data to self::testSetTemporaryValue(). */
  public function providerSetTemporaryValue() {
    return [
      ['FOO', 'BAR'],
      [
Home | Imprint | This part of the site doesn't use cookies.