You are here

public function PathAlias::buildLink in Freelinking 4.0.x

Same name and namespace in other branches
  1. 8.3 src/Plugin/freelinking/PathAlias.php \Drupal\freelinking\Plugin\freelinking\PathAlias::buildLink()

Build a link with the plugin.

Parameters

array $target: The target array with including the following keys:

  • text: The text to display in the URL.
  • indicator: The indicator string.
  • dest: The destination string for the plugin to turn into a URI.
  • tooltip: An optional tooltip.
  • language: A language object.

Return value

array Link array.

Overrides FreelinkingPluginInterface::buildLink

File

src/Plugin/freelinking/PathAlias.php, line 109

Class

PathAlias
Freelinking path plugin.

Namespace

Drupal\freelinking\Plugin\freelinking

Code

public function buildLink(array $target) {
  $failover = $this
    ->getConfiguration()['settings']['failover'];

  // All aliases must use a preceding slash.
  $alias = strpos('/', $target['dest']) === 0 ? $target['dest'] : '/' . $target['dest'];
  $langcode = isset($target['language']) ? $target['language']
    ->getId() : NULL;
  $path = $this->aliasManager
    ->getPathByAlias($alias, $langcode);

  // A path  was found.
  if ($path !== $alias) {
    $link = [
      '#type' => 'link',
      '#title' => $target['text'],
      '#url' => Url::fromUserInput($path, [
        'language' => $target['language'],
      ]),
      '#attributes' => [
        'title' => isset($target['tooltip']) ? $target['tooltip'] : $this
          ->getTip(),
      ],
    ];
  }
  elseif ($failover === 'search' && $this->moduleHandler
    ->moduleExists('search')) {
    $link = [
      '#type' => 'link',
      '#title' => $target['text'],
      '#url' => Url::fromUserInput('/search', [
        'query' => [
          'keys' => $path,
        ],
        'language' => $target['language'],
      ]),
      '#attributes' => [
        'title' => $this
          ->getTip(),
      ],
    ];
  }
  else {
    $link = [
      '#theme' => 'freelink_error',
      '#plugin' => 'path_alias',
      '#message' => $this
        ->t('path “%path” not found', [
        '%path' => $path,
      ]),
    ];
  }
  return $link;
}