SocialMediaLinksPlatformManager.php in Social Media Links Block and Field 8.2
File
src/SocialMediaLinksPlatformManager.php
View source
<?php
namespace Drupal\social_media_links;
use Drupal\Core\Plugin\DefaultPluginManager;
use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
class SocialMediaLinksPlatformManager extends DefaultPluginManager {
public function __construct(\Traversable $namespaces, CacheBackendInterface $cache_backend, ModuleHandlerInterface $module_handler) {
parent::__construct('Plugin/SocialMediaLinks/Platform', $namespaces, $module_handler, 'Drupal\\social_media_links\\PlatformInterface', 'Drupal\\social_media_links\\Annotation\\Platform');
$this
->alterInfo('social_media_links_platforms_info');
$this
->setCacheBackend($cache_backend, 'social_media_links_platforms');
}
public function getPlatforms() {
$plugins = $this
->getDefinitions();
foreach ($plugins as $plugin_id => $plugin) {
$instance = $this
->createInstance($plugin_id);
if ($instance instanceof PlatformInterface) {
$plugins[$plugin_id]['instance'] = $instance;
}
}
return $plugins;
}
public function getPlatformsSortedByWeight(array $settings) {
$default_weight = -10;
$platforms = $this
->getPlatforms();
foreach ($platforms as $platform_id => $platform) {
$platforms[$platform_id]['weight'] = isset($settings['platforms'][$platform_id]['weight']) ? $settings['platforms'][$platform_id]['weight'] : $default_weight++;
}
uasort($platforms, [
'Drupal\\Component\\Utility\\SortArray',
'sortByWeightElement',
]);
return $platforms;
}
public function getPlatformsWithValue(array $platforms, $sort = TRUE) {
$usedPlatforms = [];
foreach ($this
->getPlatforms() as $platform_id => $platform) {
if (!empty($platforms[$platform_id]['value'])) {
$platform['instance']
->setValue($platforms[$platform_id]['value']);
if (!empty($platforms[$platform_id]['description'])) {
$platform['instance']
->setDescription($platforms[$platform_id]['description']);
}
$usedPlatforms[$platform_id] = $platform;
$usedPlatforms[$platform_id]['weight'] = $platforms[$platform_id]['weight'];
$usedPlatforms[$platform_id]['url'] = $platform['instance']
->generateUrl($platform['instance']
->getUrl());
}
}
if ($sort) {
uasort($usedPlatforms, [
'Drupal\\Component\\Utility\\SortArray',
'sortByWeightElement',
]);
}
return $usedPlatforms;
}
}