You are here

link_badges.module in Link Badges 8

Same filename and directory in other branches
  1. 7 link_badges.module

Link Badges module file.

File

link_badges.module
View source
<?php

/**
 * @file
 * Link Badges module file.
 */

/**
 * Implements hook_link_alter()
 * @param $variables
 */
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';
    }
  }
}

/**
 * Implements hook_theme().
 */
function link_badges_theme($existing, $type, $theme, $path) {
  return array(
    'link_badges_link_text' => array(
      'template' => 'link-badges-link-text',
      'variables' => array(
        'link' => NULL,
        'badge_value' => NULL,
        'badge_class' => NULL,
        'badge_wrapper_class' => NULL,
        'badge_text_class' => NULL,
      ),
    ),
    'link_badges_views_view' => array(
      'variables' => array(
        'view_array' => array(),
        'view' => NULL,
      ),
      'template' => 'link-badges-views-view',
    ),
  );
}

/**
 * Preprocess the menu badge views display.
 */
function template_preprocess_link_badges_views_view(&$vars) {
  $view = $vars['view'];
  $vars['rows'] = !empty($view->result) || $view->style_plugin
    ->even_empty() ? $view->style_plugin
    ->render($view->result) : '';
  if (!empty($vars['rows'])) {
    $rendered = drupal_render($vars['rows']);
    $vars['badge_result'] = trim(strip_tags($rendered));
  }
  else {
    $vars['badge_result'] = NULL;
  }
}

Functions

Namesort descending Description
link_badges_link_alter Implements hook_link_alter()
link_badges_theme Implements hook_theme().
template_preprocess_link_badges_views_view Preprocess the menu badge views display.