You are here

public function ShareEverywhereService::build in Share Everywhere 2.x

Same name and namespace in other branches
  1. 8 src/ShareEverywhereService.php \Drupal\share_everywhere\ShareEverywhereService::build()

Builds a renderable array of Social buttons.

Parameters

string $url: Node url.

string $id: Node entity type plus node id.

Return value

array Renderable build array.

Overrides ShareEverywhereServiceInterface::build

File

src/ShareEverywhereService.php, line 46

Class

ShareEverywhereService
Defines a ShareEverywhereService service.

Namespace

Drupal\share_everywhere

Code

public function build($url, $id) {
  global $base_url;
  $config = $this->configFactory
    ->get('share_everywhere.settings');
  $module_path = drupal_get_path('module', 'share_everywhere');
  $build = [
    '#theme' => 'share_everywhere',
  ];
  $buttons = [];
  $library = [];
  switch ($config
    ->get('alignment')) {
    case 'left':
      $build['#attributes']['class'] = [
        'se-align-left',
      ];
      break;
    case 'right':
      $build['#attributes']['class'] = [
        'se-align-right',
      ];
      break;
  }
  $share_buttons = $config
    ->get('buttons');
  uasort($share_buttons, 'Drupal\\Component\\Utility\\SortArray::sortByWeightElement');
  foreach ($share_buttons as $key => $button) {
    if ($key == 'facebook_like' && $button['enabled']) {
      $build['#facebook_like'] = [
        '#theme' => 'se_facebook_like',
        '#url' => $url,
      ];
      array_push($build['#attributes']['class'], 'se-has-like');
    }
    elseif ($button['enabled']) {
      $buttons[$key] = [
        '#theme' => 'se_' . $key,
        '#url' => $url,
      ];
      if ($key != 'facebook_like' && $config
        ->get('style') == 'share_everywhere') {
        $buttons[$key]['#content'] = [
          '#type' => 'html_tag',
          '#tag' => 'img',
          '#attributes' => [
            'src' => $base_url . '/' . $module_path . '/img/' . $button['image'],
            'title' => $this
              ->t($button['title']),
            'alt' => $this
              ->t($button['title']),
          ],
        ];
      }
      elseif ($config
        ->get('style') == 'custom') {
        $buttons[$key]['#content'] = $this
          ->t($button['name']);
      }
    }
  }
  $build['#buttons'] = $buttons;
  $build['#se_links_id'] = 'se-links-' . $id;
  if ($config
    ->get('display_title')) {
    $build['#title'] = $this
      ->t($config
      ->get('title'));
  }
  $build['#share_icon'] = [
    'id' => 'se-trigger-' . $id,
    'src' => $base_url . '/' . $module_path . '/img/' . $config
      ->get('share_icon.image'),
    'alt' => $this
      ->t($config
      ->get('share_icon.alt')),
  ];
  if (!$config
    ->get('collapsible')) {
    $build['#share_icon']['class'] = 'se-disabled';
  }
  if ($config
    ->get('style') == 'share_everywhere') {
    if ($config
      ->get('collapsible')) {
      $library = [
        'share_everywhere/share_everywhere.css',
        'share_everywhere/share_everywhere.js',
      ];
    }
    else {
      $library = [
        'share_everywhere/share_everywhere.css',
      ];
    }
  }
  elseif ($config
    ->get('style') == 'custom') {
    if ($config
      ->get('include_css')) {
      $library = [
        'share_everywhere/share_everywhere.css',
      ];
    }
    if ($config
      ->get('include_js') && $config
      ->get('collapsible')) {
      array_push($library, 'share_everywhere/share_everywhere.js');
    }
  }
  if (!$config
    ->get('collapsible') || $config
    ->get('style') == 'custom' && !$config
    ->get('include_js')) {
    $build['#is_active'] = 'se-active';
  }
  elseif ($config
    ->get('collapsible')) {
    $build['#is_active'] = 'se-inactive';
  }
  if (!empty($library)) {
    $build['#attached'] = [
      'library' => $library,
    ];
  }
  return $build;
}