You are here

protected function SubscriptionWidget::getSelectedOptions in Simplenews 8

Same name and namespace in other branches
  1. 8.2 src/Plugin/Field/FieldWidget/SubscriptionWidget.php \Drupal\simplenews\Plugin\Field\FieldWidget\SubscriptionWidget::getSelectedOptions()
  2. 3.x src/Plugin/Field/FieldWidget/SubscriptionWidget.php \Drupal\simplenews\Plugin\Field\FieldWidget\SubscriptionWidget::getSelectedOptions()

Determines selected options from the incoming field values.

Parameters

\Drupal\Core\Field\FieldItemListInterface $items: The field values.

Return value

array The array of corresponding selected options.

Overrides OptionsWidgetBase::getSelectedOptions

File

src/Plugin/Field/FieldWidget/SubscriptionWidget.php, line 73

Class

SubscriptionWidget
Plugin implementation of the 'simplenews_subscription_select' widget.

Namespace

Drupal\simplenews\Plugin\Field\FieldWidget

Code

protected function getSelectedOptions(FieldItemListInterface $items, $delta = 0) {

  // Copy parent behavior but also check the status property.
  $flat_options = OptGroup::flattenOptions($this
    ->getOptions($items
    ->getEntity()));
  $selected_options = array();
  foreach ($items as $item) {
    $value = $item->{$this->column};

    // Keep the value if it actually is in the list of options (needs to be
    // checked against the flat list).
    if ($item->status == SIMPLENEWS_SUBSCRIPTION_STATUS_SUBSCRIBED && isset($flat_options[$value])) {
      $selected_options[] = $value;
    }
  }
  return $selected_options;
}