extractFormValues example

usort($field_state['items'][SortArray::class, 'sortByWeightElement']);
      $items->setValue($field_state['items']);
    }

    return parent::form($items$form$form_state$get_delta);
  }

  /** * {@inheritdoc} */
  public function extractFormValues(FieldItemListInterface $items, array $form, FormStateInterface $form_state) {
    parent::extractFormValues($items$form$form_state);

    // Update reference to 'items' stored during add or remove to take into     // account changes to values like 'weight' etc.     // @see Drupal\media_library\Plugin\Field\FieldWidget\MediaLibraryWidget::addItems     // @see Drupal\media_library\Plugin\Field\FieldWidget\MediaLibraryWidget::removeItem     $field_name = $this->fieldDefinition->getName();
    $field_state = static::getWidgetState($form['#parents']$field_name$form_state);
    $field_state['items'] = $items->getValue();
    static::setWidgetState($form['#parents']$field_name$form_state$field_state);
  }

  
$new_values[] = $new_value;
      }
    }

    return $new_values;
  }

  /** * {@inheritdoc} */
  public function extractFormValues(FieldItemListInterface $items, array $form, FormStateInterface $form_state) {
    parent::extractFormValues($items$form$form_state);

    // Update reference to 'items' stored during upload to take into account     // changes to values like 'alt' etc.     // @see \Drupal\file\Plugin\Field\FieldWidget\FileWidget::submit()     $field_name = $this->fieldDefinition->getName();
    $field_state = static::getWidgetState($form['#parents']$field_name$form_state);
    $field_state['items'] = $items->getValue();
    static::setWidgetState($form['#parents']$field_name$form_state$field_state);
  }

  /** * Form API callback. Retrieves the value for the file_generic field element. * * This method is assigned as a #value_callback in formElement() method. */
return ['#markup' => $this->t('No widget available for: %type.', ['%type' => $this->getFieldDefinition()->getType()])];
      }
    }
  }

  /** * {@inheritdoc} */
  public function defaultValuesFormValidate(array $element, array &$form, FormStateInterface $form_state) {
    // Extract the submitted value, and validate it.     if ($widget = $this->defaultValueWidget($form_state)) {
      $widget->extractFormValues($this$element$form_state);
      // Force a non-required field definition.       // @see self::defaultValueWidget().       $this->getFieldDefinition()->setRequired(FALSE);
      $violations = $this->validate();

      // Assign reported errors to the correct form element.       if (count($violations)) {
        $widget->flagErrors($this$violations$element$form_state);
      }
    }
  }

  
$menu_link = new MenuLinkDefault([], 'my_plugin_id', []$static_override->reveal());
    $menu_link_form->setMenuLinkInstance($menu_link);

    $form_state = new FormState();
    $form_state->setValue('id', 'my_plugin_id');
    $form_state->setValue('enabled', FALSE);
    $form_state->setValue('weight', 5);
    $form_state->setValue('expanded', TRUE);
    $form_state->setValue('menu_parent', 'foo:bar');

    $form = [];
    $result = $menu_link_form->extractFormValues($form$form_state);

    $this->assertEquals([
      'id' => 'my_plugin_id',
      'enabled' => 0,
      'weight' => 5,
      'expanded' => 1,
      'parent' => 'bar',
      'menu_name' => 'foo',
    ]$result);
  }

}
/** * {@inheritdoc} */
  public function elementValidate($element, FormStateInterface $form_state$form) {
    $field_name = $element['#field_name'];
    $entity = $form_state->getFormObject()->getEntity();
    $input = $form_state->getUserInput();
    if (!empty($input['_triggering_element_name']) && str_contains($input['_triggering_element_name'], 'media-library-update')) {
      // This will validate a required field before an upload is completed.       $display = EntityFormDisplay::collectRenderDisplay($entity, 'edit');
      $display->extractFormValues($entity$form$form_state);
      $display->validateFormValues($entity$form$form_state);
    }
    $form_value = $form_state->getValue($field_name);
    if (!empty($form_value['media_library_selection'])) {
      $entity->set($field_name$form_value['media_library_selection']);
    }
  }

}

    return $element;
  }

  /** * {@inheritdoc} */
  public function extractFormValues(FieldableEntityInterface $entity, array &$form, FormStateInterface $form_state) {
    $extracted = [];
    foreach ($entity as $name => $items) {
      if ($widget = $this->getRenderer($name)) {
        $widget->extractFormValues($items$form$form_state);
        $extracted[$name] = $name;
      }
    }
    return $extracted;
  }

  /** * {@inheritdoc} */
  public function validateFormValues(FieldableEntityInterface $entity, array &$form, FormStateInterface $form_state) {
    $violations = $entity->validate();
    
else {
      $message = $this->t('This link is provided by the Views module from view %label.', ['%label' => $label]);
    }
    $form['info']['#title'] = $message;
    return $form;
  }

  /** * {@inheritdoc} */
  public function extractFormValues(array &$form, FormStateInterface $form_state) {
    $definition = parent::extractFormValues($form$form_state);
    $definition['title'] = $form_state->getValue('title');
    $definition['description'] = $form_state->getValue('description');

    return $definition;
  }

}

  protected function validateMediaEntity(MediaInterface $media, array $form, FormStateInterface $form_state$delta) {
    $form_display = EntityFormDisplay::collectRenderDisplay($media, 'media_library');
    $form_display->extractFormValues($media$form['media'][$delta]['fields']$form_state);
    $form_display->validateFormValues($media$form['media'][$delta]['fields']$form_state);
  }

  /** * {@inheritdoc} */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    foreach ($this->getAddedMediaItems($form_state) as $delta => $media) {
      EntityFormDisplay::collectRenderDisplay($media, 'media_library')
        ->extractFormValues($media$form['media'][$delta]['fields']$form_state);
      $this->prepareMediaEntityForSave($media);
      
    $values_2[1]['value'] = 0;

    // Pretend the form has been built.     $form_state->setFormObject(\Drupal::entityTypeManager()->getFormObject($entity_type, 'default'));
    \Drupal::formBuilder()->prepareForm('field_test_entity_form', $form$form_state);
    \Drupal::formBuilder()->processForm('field_test_entity_form', $form$form_state);
    $form_state->setValue($this->fieldTestData->field_name, $values);
    $form_state->setValue($this->fieldTestData->field_name_2, $values_2);

    // Extract values for all fields.     $entity = clone($entity_init);
    $display->extractFormValues($entity$form$form_state);

    asort($weights);
    asort($weights_2);
    $expected_values = [];
    $expected_values_2 = [];
    foreach ($weights as $key => $value) {
      if ($key != 1) {
        $expected_values[] = ['value' => $values[$key]['value']];
      }
    }
    $this->assertSame($expected_values$entity->{$this->fieldTestData->field_name}->getValue(), 'Submit filters empty values');
    

  public function isDefaultFormLangcode(FormStateInterface $form_state) {
    $this->initFormLangcodes($form_state);
    return $form_state->get('langcode') == $form_state->get('entity_default_langcode');
  }

  /** * {@inheritdoc} */
  protected function copyFormValuesToEntity(EntityInterface $entity, array $form, FormStateInterface $form_state) {
    // First, extract values from widgets.     $extracted = $this->getFormDisplay($form_state)->extractFormValues($entity$form$form_state);

    // Then extract the values of fields that are not rendered through widgets,     // by simply copying from top-level form values. This leaves the fields     // that are not being edited within this form untouched.     foreach ($form_state->getValues() as $name => $values) {
      if ($entity->hasField($name) && !isset($extracted[$name])) {
        $entity->set($name$values);
      }
    }
  }

  


  /** * {@inheritdoc} */
  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');

    
return $form;
  }

  /** * {@inheritdoc} */
  public function validateForm(array &$form, FormStateInterface $form_state) {
    $entity_1 = $form_state->get('entity_1');
    /** @var \Drupal\Core\Entity\Display\EntityFormDisplayInterface $form_display_1 */
    $form_display_1 = $form_state->get('form_display_1');
    $form_display_1->extractFormValues($entity_1$form$form_state);
    $form_display_1->validateFormValues($entity_1$form$form_state);

    $entity_2 = $form_state->get('entity_2');
    /** @var \Drupal\Core\Entity\Display\EntityFormDisplayInterface $form_display_2 */
    $form_display_2 = $form_state->get('form_display_2');
    $extracted = $form_display_2->extractFormValues($entity_2$form['entity_2']$form_state);
    // Extract the values of fields that are not rendered through widgets, by     // simply copying from top-level form values. This leaves the fields that     // are not being edited within this form untouched.     // @see \Drupal\Tests\field\Functional\NestedFormTest::testNestedEntityFormEntityLevelValidation()     foreach ($form_state->getValues()['entity_2'] as $name => $values) {
      
/** * {@inheritdoc} */
  public function validateConfigurationForm(array &$form, FormStateInterface $form_state) {
  }

  /** * {@inheritdoc} */
  public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
    $new_definition = $this->extractFormValues($form$form_state);

    return $this->menuLinkManager->updateDefinition($this->menuLink->getPluginId()$new_definition);
  }

}
'targetEntityType' => 'node',
      'bundle' => 'unmoderated',
      'mode' => 'default',
      'status' => TRUE,
    ]);
    $form = [];
    $form_state = new FormState();
    $entity_form_display->buildForm($entity$form$form_state);

    // The moderation_state field should have no values for an entity that isn't     // being moderated.     $entity_form_display->extractFormValues($entity$form$form_state);
    $this->assertEquals(0, $entity->moderation_state->count());
  }

  /** * @covers ::isApplicable */
  public function testIsApplicable() {
    // The moderation_state field definition should be applicable to our widget.     $fields = $this->container->get('entity_field.manager')->getFieldDefinitions('node', 'test_type');
    $this->assertTrue(ModerationStateWidget::isApplicable($fields['moderation_state']));
    $this->assertFalse(ModerationStateWidget::isApplicable($fields['status']));
    
Home | Imprint | This part of the site doesn't use cookies.