You are here

function link_badges_link_alter in Link Badges 8

Implements hook_link_alter()

Parameters

$variables:

File

./link_badges.module, line 12
Link Badges module file.

Code

function link_badges_link_alter(&$variables) {
  if (!empty($variables['options']['link_badge'])) {
    $badge_value = NULL;
    if (!empty($variables['options']['link_badge']['id'])) {
      if (!empty($variables['options']['link_badge']['id']) && $variables['options']['link_badge']['id'] == 'views_badge') {

        // Set derived id from properties
        if (!empty($variables['options']['link_badge']['properties']['name'])) {
          $variables['options']['link_badge']['id'] .= ':' . $variables['options']['link_badge']['properties']['name'];
        }
        if (!empty($variables['options']['link_badge']['properties']['display_id'])) {
          $variables['options']['link_badge']['id'] .= ':' . $variables['options']['link_badge']['properties']['display_id'];
        }
      }
      $manager = \Drupal::service('plugin.manager.link_badge');
      $definitions = $manager
        ->getDefinitions();
      if (!empty($definitions[$variables['options']['link_badge']['id']])) {
        $badge = new $definitions[$variables['options']['link_badge']['id']]['class']();
        if (!empty($definitions[$variables['options']['link_badge']['id']]['properties'])) {
          foreach ($definitions[$variables['options']['link_badge']['id']]['properties'] as $name => $value) {
            $badge
              ->set($name, $value);
          }
        }
        $badge_value = $badge
          ->getBadgeValue();
      }
    }
    if (!is_null($badge_value)) {
      if (empty($variables['options']['html'])) {
        $variables['text'] = \Drupal\Component\Utility\SafeMarkup::checkPlain($variables['text']);
      }
      $variables['options']['html'] = TRUE;
      $badge_classes = !empty($variables['options']['link_badge']['attributes']['badge_class']) ? $variables['options']['link_badge']['attributes']['badge_class'] : array();
      $badge_wrapper_classes = !empty($variables['options']['link_badge']['attributes']['wrapper_class']) ? $variables['options']['link_badge']['attributes']['wrapper_class'] : array();
      $badge_text_classes = !empty($variables['options']['link_badge']['attributes']['text_class']) ? $variables['options']['link_badge']['attributes']['text_class'] : array();
      $badge_text = array(
        '#theme' => 'link_badges_link_text',
        '#link' => $variables,
        '#attached' => array(
          'library' => array(
            'link_badges/link-badge',
          ),
        ),
        '#badge_class' => $badge_classes,
        '#badge_wrapper_class' => $badge_wrapper_classes,
        '#badge_text_class' => $badge_text_classes,
        '#badge_value' => $badge_value,
      );
      $variables['text'] = drupal_render($badge_text);

      //$variables['options']['attributes']['class'][] = 'link-badge-wrapper';
    }
  }
}