You are here

protected function SocialMediaLinksFieldDefaultFormatter::getPlatformsWithValues in Social Media Links Block and Field 8.2

Get the platforms that have values.

Return value

array $platforms.

1 call to SocialMediaLinksFieldDefaultFormatter::getPlatformsWithValues()
SocialMediaLinksFieldDefaultFormatter::viewElements in modules/social_media_links_field/src/Plugin/Field/FieldFormatter/SocialMediaLinksFieldDefaultFormatter.php
Builds a renderable array for a field value.

File

modules/social_media_links_field/src/Plugin/Field/FieldFormatter/SocialMediaLinksFieldDefaultFormatter.php, line 170

Class

SocialMediaLinksFieldDefaultFormatter
Plugin implementation of the 'social_media_links_field_default' formatter.

Namespace

Drupal\social_media_links_field\Plugin\Field\FieldFormatter

Code

protected function getPlatformsWithValues(FieldItemListInterface $items) {
  $platform_settings = $items
    ->getSetting('platforms');
  $all_platforms_available = TRUE;
  foreach ($platform_settings as $platform_id => $platform) {
    if ($platform['enabled']) {
      $all_platforms_available = FALSE;
      break;
    }
  }
  $platforms = [];
  foreach ($items as $item) {

    // We have two possible structures where the platform values can be
    // stored.
    // * If the select widget was used the values are saved in two fields
    // (platform and value).
    // * If the default list widget was used the values are saved in a
    // multidimensional array structure (platform_values).
    if (empty($item->platform_values)) {

      // Select widget fields handling.
      if ($all_platforms_available || isset($platform_settings[$item->platform]['enabled']) && $platform_settings[$item->platform]['enabled']) {
        $platforms[$item->platform] = [
          'value' => $item->value,
          'weight' => $platform_settings[$item->platform]['weight'],
          'description' => $platform_settings[$item->platform]['description'],
        ];
      }
    }
    else {

      // Default list field handling.
      $platform_values = $item->platform_values;
      foreach ($platform_values as $platform_id => $platform_value) {
        if ($all_platforms_available || !empty($platform_value['value']) && isset($platform_settings[$platform_id]['enabled']) && $platform_settings[$platform_id]['enabled']) {
          $platforms[$platform_id] = [
            'value' => $platform_value['value'],
            'weight' => $platform_settings[$platform_id]['weight'],
            'description' => $platform_settings[$platform_id]['description'],
          ];
        }
      }
    }
  }
  return \Drupal::service('plugin.manager.social_media_links.platform')
    ->getPlatformsWithValue($platforms);
}