You are here

protected function ScheduledUpdateTypeBaseForm::createDefaultValueElements in Scheduled Updates 8

Create the default value elements for a field.

Parameters

$field_selected:

Return value

array

1 call to ScheduledUpdateTypeBaseForm::createDefaultValueElements()
ScheduledUpdateTypeBaseForm::createCloneFieldSelect in src/Form/ScheduledUpdateTypeBaseForm.php
Create an option to select one field to clone on type add form.

File

src/Form/ScheduledUpdateTypeBaseForm.php, line 637
Contains \Drupal\scheduled_updates\Form\ScheduledUpdateTypeBaseForm.

Class

ScheduledUpdateTypeBaseForm

Namespace

Drupal\scheduled_updates\Form

Code

protected function createDefaultValueElements($field_selected, FormStateInterface $form_state) {
  $elements = [];

  // Create an arbitrary entity object (used by the 'default value' widget).
  $ids = (object) array(
    'entity_type' => $this
      ->getCurrentEntityType($form_state),
    'bundle' => $this
      ->getDefaultBundle($field_selected, $form_state),
    'entity_id' => NULL,
  );
  $form['#entity'] = _field_create_entity_from_ids($ids);

  /** @var FieldItemListInterface $items */
  $items = $form['#entity']
    ->get($field_selected);
  $definition = $items
    ->getFieldDefinition();
  if ($this
    ->isDefaultCompatible($definition)) {
    $item = $items
      ->first() ?: $items
      ->appendItem();
    if ($widget_override = $this
      ->getWidgetOverride($definition)) {
      $form_state
        ->set('default_value_widget', $widget_override);
    }

    // Add handling for default value.
    if ($elements = $items
      ->defaultValuesForm($form, $form_state)) {
      $elements = array_merge($elements, array(
        '#type' => 'details',
        '#title' => $this
          ->t('Default value and Date Only Updates'),
        '#open' => TRUE,
        '#tree' => TRUE,
        '#description' => $this
          ->t('The default value for this field, used when creating an update.'),
      ));
      $elements['#title'] = $this
        ->t('Default value and Date Only Updates');
      $elements['_no_form_display'] = [
        '#type' => 'checkbox',
        '#title' => $this
          ->t('Hide this field for an <strong>date only</strong> update.'),
        '#description' => $this
          ->t('Hiding fields with a default value is very useful for creating updates where the user only has to enter an update date.' . ' ' . 'For example creating a "Publish Date" update type where user simply has to pick a date they would like the content published on.'),
      ];
    }
  }
  return $elements;
}