You are here

private function ForwardLinkBuilder::buildLink in Forward 8

Same name and namespace in other branches
  1. 8.3 src/ForwardLinkBuilder.php \Drupal\forward\ForwardLinkBuilder::buildLink()
  2. 8.2 src/ForwardLinkBuilder.php \Drupal\forward\ForwardLinkBuilder::buildLink()

Build a link

1 call to ForwardLinkBuilder::buildLink()
ForwardLinkBuilder::buildForwardEntityLink in src/ForwardLinkBuilder.php
Builds a Forward link for a given entity.

File

src/ForwardLinkBuilder.php, line 68

Class

ForwardLinkBuilder
Defines a class for building markup for a Forward link on an entity.

Namespace

Drupal\forward

Code

private function buildLink(EntityInterface $entity, array $settings) {
  $langcode = $entity
    ->language()
    ->getId();
  $token = [
    'forward' => [
      'entity' => $entity,
    ],
  ];
  $title = $this->tokenService
    ->replace($settings['forward_interface_title'], $token, array(
    'langcode' => $langcode,
  ));
  $title_text = $title;
  $html = FALSE;

  // Output the correct style of link
  $default_icon = drupal_get_path('module', 'forward') . '/images/forward.gif';
  $custom_icon = $settings['forward_link_icon'];
  $link_style = $settings['forward_link_style'];
  switch ($link_style) {

    // text only is a "noop" since the title text is already setup above
    // image only
    case 1:
      $img = $custom_icon ? $custom_icon : $default_icon;
      $render_array = array(
        '#theme' => 'image',
        '#uri' => $img,
        '#alt' => $title,
        '#attributes' => array(
          'class' => array(
            'forward-icon',
          ),
        ),
      );
      $title = $this->renderer
        ->render($render_array);
      $html = TRUE;
      break;

    // image and text
    case 2:
      $img = $custom_icon ? $custom_icon : $default_icon;
      $render_array = array(
        'image' => array(
          '#theme' => 'image',
          '#uri' => $img,
          '#alt' => $title,
          '#attributes' => array(
            'class' => array(
              'forward-icon',
              'forward-icon-margin',
            ),
          ),
        ),
        'text' => array(
          '#markup' => $title_text,
        ),
      );
      $title = $this->renderer
        ->render($render_array);
      $html = TRUE;
      break;
  }
  $attributes = array(
    'title' => $this->tokenService
      ->replace($settings['forward_link_title'], $token, array(
      'langcode' => $langcode,
    )),
    'class' => array(
      'forward-page',
    ),
  );
  if ($settings['forward_link_nofollow']) {
    $attributes['rel'] = 'nofollow';
  }
  $entity_id = $entity
    ->id();
  $entity_type = $entity
    ->getEntityTypeId();
  $url = Url::fromUri("internal:/forward/{$entity_type}/{$entity_id}");
  $url
    ->setOptions(array(
    'html' => $html,
    'attributes' => $attributes,
  ));
  return array(
    'title' => $title,
    'url' => $url,
    'attributes' => $attributes,
  );
}