You are here

function addtoany_create_data in AddToAny Share Buttons 8

Generate data for AddToAny buttons.

Parameters

string $url: If present this will be used as the URL.

string $title: If present this will be used as the title. Use an empty string for no title or NULL to use the current page title.

object $config: If present this will be used as custom config data. Use NULL for default config data.

Return value

array The array with url, title, additional_html that will be send to Twig.

2 calls to addtoany_create_data()
AddToAnyBlock::build in src/Plugin/Block/AddToAnyBlock.php
Builds and returns the renderable array for this block plugin.
addtoany_create_entity_data in ./addtoany.module
Generate data for AddToAny buttons for a node.

File

./addtoany.module, line 219
Handle AddToAny integration.

Code

function addtoany_create_data($url = NULL, $title = NULL, $config = NULL) {
  if (is_null($config)) {
    $config = \Drupal::config('addtoany.settings');
  }
  $additional_html = rtrim($config
    ->get('additional_html'));
  $universal_button_placement = $config
    ->get('universal_button_placement');
  $is_front = \Drupal::service('path.matcher')
    ->isFrontPage();

  // Default to <front> or the current path.
  if (!isset($url) || $is_front) {

    // Use <front> for the front page to avoid '/node' as the final URL,
    // otherwise use current path.
    $url = $is_front ? Url::fromRoute('<front>')
      ->setAbsolute()
      ->toString() : Url::fromRoute('<current>')
      ->setAbsolute()
      ->toString();
  }
  else {

    // Sanitize and encode URL for HTML output.
    $url = UrlHelper::stripDangerousProtocols($url);
  }

  // Default to the current title if available, otherwise use the site name.
  if (!isset($title)) {
    $site_name = \Drupal::config('system.site')
      ->get('name');
    if ($is_front) {
      $title = $site_name;
    }
    else {
      $request = \Drupal::request();
      $route_match = \Drupal::routeMatch();
      $route_object = $route_match
        ->getRouteObject();
      if ($route_object !== NULL) {
        $title = \Drupal::service('title_resolver')
          ->getTitle($request, $route_object);
      }

      // Expecting array|string|null from getTitle.
      if (is_array($title)) {
        $title['#allowed_tags'] = [];
        $title = \Drupal::service('renderer')
          ->renderPlain($title);
      }
    }
    $title = empty($title) ? $site_name : $title;
  }
  $buttons_size = $config
    ->get('buttons_size');

  // Must be a 3 digit positive integer.
  $buttons_size = $buttons_size !== '32' && strlen($buttons_size) <= 3 && $buttons_size !== '' && is_numeric($buttons_size) && intval($buttons_size) == $buttons_size && $buttons_size > 0 ? $buttons_size : '32';
  $button_setting = $config
    ->get('universal_button');
  if ($button_setting == 'custom') {
    $button_image = UrlHelper::stripDangerousProtocols($config
      ->get('custom_universal_button'));
  }
  $info = [
    'link_url' => $url,
    'link_title' => $title,
    'buttons_size' => $buttons_size,
    'button_setting' => $button_setting,
    'button_image' => isset($button_image) ? $button_image : '',
    'universal_button_placement' => $universal_button_placement,
    'addtoany_html' => $additional_html,
  ];
  return $info;
}