defaultValuesFormSubmit example

->onlyMethods(['defaultValueWidget', 'getValue'])
      ->setConstructorArgs([$field_definition])
      ->getMock();
    $field_list->expects($this->any())
      ->method('defaultValueWidget')
      ->willReturn(NULL);
    $form = [];
    $form_state = new FormState();
    $field_list->expects($this->never())
      ->method('getValue');

    $this->assertSame([]$field_list->defaultValuesFormSubmit([]$form$form_state));
  }

}
/** * {@inheritdoc} */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    parent::submitForm($form$form_state);

    // Handle the default value.     $default_value = [];
    if (isset($form['default_value']) && (!isset($form['set_default_value']) || $form_state->getValue('set_default_value'))) {
      $items = $this->getTypedData($form['#entity']);
      $default_value = $items->defaultValuesFormSubmit($form['default_value']$form$form_state);
    }
    $this->entity->setDefaultValue($default_value);
  }

  /** * {@inheritdoc} */
  public function save(array $form, FormStateInterface $form_state) {
    $this->entity->save();

    $this->messenger()->addStatus($this->t('Saved %label configuration.', ['%label' => $this->entity->getLabel()]));

    
// Ensure we return consecutive deltas, in case we removed unknown UUIDs.       $default_value = array_values($default_value);
    }
    return $default_value;
  }

  /** * {@inheritdoc} */
  public function defaultValuesFormSubmit(array $element, array &$form, FormStateInterface $form_state) {
    $default_value = parent::defaultValuesFormSubmit($element$form$form_state);

    // Convert numeric IDs to UUIDs to ensure config deployability.     $ids = [];
    foreach ($default_value as $delta => $properties) {
      if (isset($properties['entity']) && $properties['entity']->isNew()) {
        // This may be a newly created term.         $properties['entity']->save();
        $default_value[$delta]['target_id'] = $properties['entity']->id();
        unset($default_value[$delta]['entity']);
      }
      $ids[] = $default_value[$delta]['target_id'];
    }
Home | Imprint | This part of the site doesn't use cookies.