You are here

public function LinkShortcode::process in Shortcode 2.0.x

Same name and namespace in other branches
  1. 8 shortcode_basic_tags/src/Plugin/Shortcode/LinkShortcode.php \Drupal\shortcode_basic_tags\Plugin\Shortcode\LinkShortcode::process()

Performs the shortcode processing.

Parameters

array $attributes: Array of attributes.

string $text: The text string to be processed.

string $langcode: The language code of the text to be filtered. Defaults to LANGCODE_NOT_SPECIFIED.

Return value

string The processed text.

Overrides ShortcodeInterface::process

File

shortcode_basic_tags/src/Plugin/Shortcode/LinkShortcode.php, line 22

Class

LinkShortcode
Insert div or span around the text with some css classes.

Namespace

Drupal\shortcode_basic_tags\Plugin\Shortcode

Code

public function process(array $attributes, $text, $langcode = Language::LANGCODE_NOT_SPECIFIED) {

  // Merge with default attributes.
  $attributes = $this
    ->getAttributes([
    'path' => '<front>',
    'url' => '',
    'title' => '',
    'class' => '',
    'id' => '',
    'style' => '',
    'media_file_url' => FALSE,
  ], $attributes);
  $url = $attributes['url'];
  if (empty($url)) {
    $url = $this
      ->getUrlFromPath($attributes['path'], $attributes['media_file_url']);
  }
  if ($text) {
    $title = $this
      ->getTitleFromAttributes($attributes['title'], $text);

    // Build element attributes to be used in twig.
    $element_attributes = [
      'href' => $url,
      'class' => $attributes['class'],
      'id' => $attributes['id'],
      'style' => $attributes['style'],
      'title' => $title,
    ];

    // Filter away empty attributes.
    $element_attributes = array_filter($element_attributes);
    $output = [
      '#theme' => 'shortcode_link',
      // Not required for rendering, just for extra context.
      '#url' => $url,
      '#attributes' => $element_attributes,
      '#text' => $text,
    ];
    return $this
      ->render($output);
  }
  return $url;
}