You are here

public function FreelinkingManager::parseTarget in Freelinking 4.0.x

Same name and namespace in other branches
  1. 8.3 src/FreelinkingManager.php \Drupal\freelinking\FreelinkingManager::parseTarget()

Parse link arguments from the target string.

Parameters

string $target: The target string to parse arguments for the link.

string $langcode: The language code i.e. "en".

Return value

array Target arguments.

Overrides FreelinkingManagerInterface::parseTarget

1 call to FreelinkingManager::parseTarget()
FreelinkingManager::createFreelinkElement in src/FreelinkingManager.php
Create the render array for the respective Freelinking plugin.

File

src/FreelinkingManager.php, line 108

Class

FreelinkingManager
Freelinking plugin manager.

Namespace

Drupal\freelinking

Code

public function parseTarget($target, $langcode) {
  $args = [];
  $args['target'] = $target;
  $args['other'] = [];
  $items = explode('|', $target);

  /*
   * Each argument in the target string has three possibilities:
   *
   * - Unnamed: the first three argument sare dest, text and tooltip.
   * - INI: arguments can be delimited by key=value pairs.
   * - Other: Anything else is appended to an indexed array.
   */
  $index = 0;
  foreach ($items as $key => $item) {
    if ($index < 3) {
      switch ($index) {
        case 0:
          $args['dest'] = $item;
          break;
        case 1:
          $args['text'] = $item;
          break;
        case 2:
          $args['tooltip'] = $item;
          break;
      }
      $index++;
    }
    elseif (strpos($item, '=')) {
      [
        $name,
        $value,
      ] = explode('=', $item);
      $args[$name] = $value;
    }
    else {
      $args['other'][] = $item;
    }
  }

  // Convert URL-encoded text into something readable for link text & tooltip.
  $args['text'] = isset($args['text']) ? urldecode($args['text']) : NULL;
  $args['tooltip'] = isset($args['tooltip']) ? urldecode($args['tooltip']) : NULL;
  $args['language'] = $this->languageManager
    ->getLanguage($langcode);
  return $args;
}