You are here

public function NodeTitle::buildLink in Freelinking 4.0.x

Same name and namespace in other branches
  1. 8.3 src/Plugin/freelinking/NodeTitle.php \Drupal\freelinking\Plugin\freelinking\NodeTitle::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/NodeTitle.php, line 145

Class

NodeTitle
Node Title freelinking plugin.

Namespace

Drupal\freelinking\Plugin\freelinking

Code

public function buildLink(array $target) {
  $link = '';
  $failover_option = $this
    ->getConfiguration()['settings']['failover'];
  $result = $this
    ->getQuery($target);
  if ($result && !empty($result)) {
    $nid = array_shift($result);
    $link = [
      '#type' => 'link',
      '#title' => isset($target['text']) ? $target['text'] : $target['dest'],
      '#url' => Url::fromRoute('entity.node.canonical', [
        'node' => $nid,
      ], [
        'language' => $target['language'],
      ]),
      '#attributes' => [
        'title' => isset($target['tooltip']) ? $target['tooltip'] : $this
          ->getTip(),
      ],
    ];
  }
  elseif ($failover_option !== 'error' && $failover_option !== '_none') {
    return [
      'error' => $failover_option,
    ];
  }
  elseif ($failover_option === 'error') {
    $link = [
      '#theme' => 'freelink_error',
      '#plugin' => 'nodetitle',
      '#message' => $this
        ->t('Node title %target does not exist', [
        '%target' => $target['dest'],
      ]),
    ];
  }
  else {
    $link = [
      '#markup' => '[[nodetitle:' . $target['target'] . ']]',
    ];
  }
  return $link;
}