You are here

public function SharethisManager::renderSpans in ShareThis 8.2

Custom html block.

Parameters

array $array: Settings array.

string $title: Title string.

string $string: Sharethis path.

Return value

array Array with options, title and path.

Overrides SharethisManagerInterface::renderSpans

2 calls to SharethisManager::renderSpans()
SharethisManager::blockContents in src/SharethisManager.php
Custom html block.
SharethisManager::widgetContents in src/SharethisManager.php
Custom html markup for widget.

File

src/SharethisManager.php, line 216

Class

SharethisManager
Defines an SharethisManager service.

Namespace

Drupal\sharethis

Code

public function renderSpans(array $data_options, $mtitle, $mpath) {
  foreach ($data_options['option_extras'] as $service) {
    $data_options['services'] .= ',"' . $service . '"';
  }

  // Share buttons are simply spans of the form class='st_SERVICE_BUTTONTYPE'
  // where -- "st" stands for ShareThis.
  $type = mb_substr($data_options['buttons'], 4);
  $type = $type == '_' ? '' : Html::escape($type);
  $service_array = explode(',', $data_options['services']);
  $st_spans = [];
  foreach ($service_array as $service_full) {

    // Strip the quotes from element in array (They are there for javascript).
    $service = explode(':', $service_full);

    // Service names are expected to be parsed by Name:machine_name. If only
    // one element in the array is given, it's an invalid service.
    if (count($service) < 2) {
      continue;
    }

    // Find the service code name.
    $service_code_name = mb_substr($service[1], 0, -1);

    // Switch the title on a per-service basis if required.
    // $mtitle = $mtitle;.
    switch ($service_code_name) {
      case 'twitter':
        $mtitle = empty($data_options['twitter_suffix']) ? Html::escape($mtitle) : Html::escape($mtitle) . ' ' . Html::escape($data_options['twitter_suffix']);
        break;
    }

    // Sanitize the service code for display.
    $display = Html::escape($service_code_name);

    // Put together the span attributes.
    $attributes = [
      'st_url' => $mpath,
      'st_title' => $mtitle,
      'class' => 'st_' . $display . $type,
    ];
    if ($service_code_name == 'twitter') {
      if (!empty($data_options['twitter_handle'])) {
        $attributes['st_via'] = $data_options['twitter_handle'];
        $attributes['st_username'] = $data_options['twitter_recommends'];
      }
    }

    // Only show the display text if the type is set.
    if (!empty($type)) {
      $attributes['displayText'] = Html::escape($display);
    }
    $span_element = [
      '#type' => 'html_tag',
      '#tag' => 'span',
      '#attributes' => $attributes,
      // It's an empty span tag.
      '#value' => '',
    ];

    // Render the span tag.
    $st_spans[] = $span_element;
  }
  return [
    'data_options' => $data_options,
    'm_path' => $mpath,
    'm_title' => $mtitle,
    'st_spans' => $st_spans,
  ];
}