You are here

protected function SocialLinkWidget::getFormValues in Social Link Field 8

Provide default form values.

Parameters

bool $enable_social: Necessity to show or hide social select.

\Drupal\Core\Field\FieldItemListInterface $items: Array of default values for this field.

int $delta: The order of this item in the array of sub-elements.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Return value

string[] Returns default item values.

1 call to SocialLinkWidget::getFormValues()
SocialLinkWidget::formElement in src/Plugin/Field/FieldWidget/SocialLinkWidget.php
Returns the form for a single field widget.

File

src/Plugin/Field/FieldWidget/SocialLinkWidget.php, line 371

Class

SocialLinkWidget
Plugin implementation of the 'open_hours' widget.

Namespace

Drupal\social_link_field\Plugin\Field\FieldWidget

Code

protected function getFormValues($enable_social, FieldItemListInterface $items, $delta, FormStateInterface $form_state) {
  $entity_values = $items[$delta];
  $default_values = $this->fieldDefinition
    ->getDefaultValueLiteral();
  $is_ajax_callback = $form_state
    ->isRebuilding();
  $apply_items_value = !$is_ajax_callback && $entity_values->social;
  $apply_default_value = !$is_ajax_callback && $items
    ->isEmpty() && !empty($default_values) && isset($default_values[$delta]);

  // Values must be obtained from form state when is rebuilding.
  $form_state_raw = $form_state
    ->getValue($this->fieldDefinition
    ->getName());
  $form_state_default = !empty($form_state_raw) && !empty($form_state_raw[$delta]) ? $form_state_raw[$delta] : NULL;
  $apply_state_value = $is_ajax_callback && !empty($form_state_default);

  // From form config.
  if ($apply_default_value) {
    $social = $default_values[$delta]['social'];
    $link = $default_values[$delta]['link'];
  }
  else {
    if ($apply_items_value) {
      $social = $entity_values->social;
      $link = $entity_values->link;
    }
    else {
      if ($apply_state_value) {
        $social = $form_state_default['social'];
        $link = $form_state_default['link'];
      }
      else {
        $social = '';
        $link = '';
      }
    }
  }
  return [
    'social' => $social,
    'link' => $link,
  ];
}