You are here

public function Node::buildLink in Freelinking 4.0.x

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

Class

Node
Node ID freelinking plugin.

Namespace

Drupal\freelinking\Plugin\freelinking

Code

public function buildLink(array $target) {

  // Failover.
  $link = [
    '#theme' => 'freelink_error',
    '#plugin' => 'nid',
  ];

  // Attempt to load the node by the node ID provided by target destination.
  $node = $this->entityTypeManager
    ->getStorage('node')
    ->load($target['dest']);
  if (NULL !== $node && $node instanceof NodeInterface) {

    // Get target.
    if ($target['language']
      ->getId() !== $node
      ->language()
      ->getId()) {
      $node = $node
        ->getTranslation($target['language']
        ->getId());
    }
    $link = [
      '#type' => 'link',
      '#title' => isset($target['text']) ? $target['text'] : $node
        ->label(),
      '#url' => Url::fromRoute('entity.node.canonical', [
        'node' => $node
          ->id(),
      ], [
        'language' => $target['language'],
      ]),
      '#attributes' => [
        'title' => isset($target['tooltip']) ? $target['tooltip'] : $this
          ->getTip(),
      ],
    ];
  }
  else {

    // Save some processing by generating the translation link for failover later.
    $link['#message'] = $this
      ->t('Invalid node ID @nid', [
      '@nid' => $target['dest'],
    ]);
  }
  return $link;
}