defaultValuesForm example


class DateRangeFieldItemList extends DateTimeFieldItemList {

  /** * {@inheritdoc} */
  public function defaultValuesForm(array &$form, FormStateInterface $form_state) {
    if (empty($this->getFieldDefinition()->getDefaultValueCallback())) {
      $default_value = $this->getFieldDefinition()->getDefaultValueLiteral();

      $element = parent::defaultValuesForm($form$form_state);

      $element['default_date_type']['#title'] = $this->t('Default start date');
      $element['default_date_type']['#description'] = $this->t('Set a default value for the start date.');

      $element['default_end_date_type'] = [
        '#type' => 'select',
        '#title' => $this->t('Default end date'),
        '#description' => $this->t('Set a default value for the end date.'),
        '#default_value' => $default_value[0]['default_end_date_type'] ?? '',
        '#options' => [
          static::DEFAULT_VALUE_NOW => $this->t('Current date'),
          
$form['settings'] += $item->fieldSettingsForm($form$form_state);
    $form['third_party_settings'] = [
      '#tree' => TRUE,
      '#weight' => 11,
    ];

    // Create a new instance of typed data for the field to ensure that default     // value widget is always rendered from a clean state.     $items = $this->getTypedData($form['#entity']);

    // Add handling for default value.     if ($element = $items->defaultValuesForm($form$form_state)) {
      $has_required = $this->hasAnyRequired($element);

      $element = array_merge($element[
        '#type' => 'details',
        '#title' => $this->t('Default value'),
        '#open' => TRUE,
        '#tree' => TRUE,
        '#description' => $this->t('The default value for this field, used when creating new content.'),
        '#weight' => 12,
      ]);

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

    $this->assertEquals('No widget available for: <em class="placeholder">field_type</em>.', $field_list->defaultValuesForm($form$form_state)['#markup']);
  }

  /** * @covers ::defaultValuesFormValidate */
  public function testDefaultValuesFormValidate() {
    $field_definition = $this->createMock(FieldDefinitionInterface::class);
    /** @var \Drupal\Core\Field\FieldItemList|\PHPUnit\Framework\MockObject\MockObject $field_list */
    $field_list = $this->getMockBuilder(FieldItemList::class)
      ->onlyMethods(['defaultValueWidget', 'validate'])
      ->setConstructorArgs([$field_definition])
      
Home | Imprint | This part of the site doesn't use cookies.