You are here

protected function SocialLinkWidget::ensureItemsOrder in Social Link Field 8

Ensure items order to prevent delta issues.

Parameters

\Drupal\Core\Field\FieldItemListInterface $items: Original items.

1 call to SocialLinkWidget::ensureItemsOrder()
SocialLinkWidget::formMultipleElements in src/Plugin/Field/FieldWidget/SocialLinkWidget.php
Special handling to create form elements for multiple values.

File

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

Class

SocialLinkWidget
Plugin implementation of the 'open_hours' widget.

Namespace

Drupal\social_link_field\Plugin\Field\FieldWidget

Code

protected function ensureItemsOrder(FieldItemListInterface $items) {
  $default_values = $this->fieldDefinition
    ->getDefaultValueLiteral();
  $items_array = $items
    ->getValue();
  $new_items = [];
  $map = [];

  // Try to locate delta from default value.
  // Working even with multiple same rrss.
  foreach ($default_values as $default_delta => $default) {
    $set = FALSE;
    foreach ($items_array as $saved_delta => $item) {

      // Already located.
      if ($item['social'] == $default['social'] && !in_array($saved_delta, $map)) {
        $new_items[$default_delta] = $items_array[$saved_delta];
        $map[$default_delta] = $saved_delta;
        $set = TRUE;
        continue 2;
      }
    }
    if (!$set) {
      $new_items[$default_delta] = $default;
    }
  }
  $items
    ->setValue($new_items);
}