You are here

public function AppService::reportLink in Forena Reports 8

Parameters

$text:

$fields:

Return value

mixed

1 method overrides AppService::reportLink()
TestingAppService::reportLink in tests/src/Unit/Mock/TestingAppService.php

File

src/AppService.php, line 305

Class

AppService

Namespace

Drupal\forena

Code

public function reportLink($text, $fields) {

  //@TODO:  Map the fields from teh report call to the options for URL.
  $link = '';
  $target = '';
  $class = '';
  $rel = '';
  $add_query = '';

  // Extract the above variables from the field definition
  extract($fields);
  $attributes = [];

  // Get data attributes from the field definition
  foreach ($fields as $k => $v) {
    if (strpos($k, 'data-') === 0) {
      $attributes[$k] = $v;
    }
  }

  // use the target attribute to open links in new tabs or as popups.
  if (@strpos(strtolower($target), 'popup') === 0) {
    $options = "status=1";
    $attributes = array(
      'onclick' => 'window.open(this.href, \'' . $target . '\', "' . $options . '"); return false;',
    );
  }
  else {
    if ($target) {
      $attributes['target'] = $target;
    }
  }

  // Rel
  if ($rel) {
    $attributes['rel'] = $rel;
  }

  // Class
  if ($class) {
    $attributes['class'] = explode(' ', $class);
  }

  // @TODO: Add libararies for modals.
  @(list($path, $query) = explode('?', $link));
  @(list($query, $queryFrag) = explode('#', $query));
  @(list($path, $fragment) = explode('#', $path));
  $fragment = $fragment . $queryFrag;
  $data = array();
  parse_str($query, $data);

  // Add items from query string if specified.
  if ($add_query) {
    $parms = $_GET;
    $data = array_merge($parms, $data);
  }
  if (trim($path)) {

    // Work with internal links.
    if (!strpos($path, '://')) {
      $path = "/{$path}";
      $url = Url::fromUserInput($path, [
        'fragment' => $fragment,
        'query' => $data,
        'attributes' => $attributes,
        'absolute' => TRUE,
      ]);
    }
    else {
      $url = Url::fromUri($path, [
        'fragment' => $fragment,
        'query' => $data,
        'attributes' => $attributes,
        'absolute' => TRUE,
      ]);
    }
    $link = Link::fromTextAndUrl($text, $url)
      ->toRenderable();
    $link_html = \Drupal::service('renderer')
      ->render($link);
  }
  return $link_html;
}