You are here

function template_preprocess_flippy in Flippy 8

Same name and namespace in other branches
  1. 7 flippy.module \template_preprocess_flippy()

Implements template_preprocess_hook().

File

./flippy.module, line 426
Allows administrators to add previous/next pagers to any node type.

Code

function template_preprocess_flippy(&$vars) {

  // For getting node type.
  if ($node = \Drupal::request()->attributes
    ->get('node')) {
    $vars['node'] = $node;
  }
  $flippy_settings = \Drupal::config('flippy.settings');

  // Get all the labels.
  $first_label = t($flippy_settings
    ->get('flippy_first_label_' . $vars['node']
    ->getType()));
  $last_label = t($flippy_settings
    ->get('flippy_last_label_' . $vars['node']
    ->getType()));
  $random_label = t($flippy_settings
    ->get('flippy_random_label_' . $vars['node']
    ->getType()));
  $prev_label = t($flippy_settings
    ->get('flippy_prev_label_' . $vars['node']
    ->getType()));
  $next_label = t($flippy_settings
    ->get('flippy_next_label_' . $vars['node']
    ->getType()));
  $show_empty = $flippy_settings
    ->get('flippy_show_empty_' . $vars['node']
    ->getType());

  // See if we need to truncate labels.
  if ($truncate = $flippy_settings
    ->get('flippy_truncate_' . $vars['node']
    ->getType())) {
    if (is_numeric($truncate)) {
      $first_label = strlen($first_label) > $truncate ? $first_label : substr($first_label, 0, $truncate) . $flippy_settings
        ->get('flippy_ellipse_' . $vars['node']
        ->getType());
      $last_label = strlen($last_label) > $truncate ? $last_label : substr($last_label, 0, $truncate) . $flippy_settings
        ->get('flippy_ellipse_' . $vars['node']
        ->getType());
      $random_label = strlen($random_label) > $truncate ? $random_label : substr($random_label, 0, $truncate) . $flippy_settings
        ->get('flippy_ellipse_' . $vars['node']
        ->getType());
      $prev_label = strlen($prev_label) > $truncate ? $prev_label : substr($prev_label, 0, $truncate) . $flippy_settings
        ->get('flippy_ellipse_' . $vars['node']
        ->getType());
      $next_label = strlen($next_label) > $truncate ? $next_label : substr($next_label, 0, $truncate) . $flippy_settings
        ->get('flippy_ellipse_' . $vars['node']
        ->getType());
    }
  }

  // See if we need to create the loop.
  if ($flippy_settings
    ->get('flippy_loop_' . $vars['node']
    ->getType())) {
    if (!isset($vars['list']['next']['nid']) && isset($vars['list']['first']['nid'])) {
      $vars['list']['next']['nid'] = $vars['list']['first']['nid'];
    }
    if (!isset($vars['list']['prev']['nid']) && isset($vars['list']['last']['nid'])) {
      $vars['list']['prev']['nid'] = $vars['list']['last']['nid'];
    }
  }

  // Build the variables for twig.
  if ($nav = $vars['list']) {
    if ($flippy_settings
      ->get('flippy_first_last_' . $vars['node']
      ->getType()) != 0) {
      if (isset($nav['first']) && $nav['first']['nid'] != FALSE) {
        $vars['first'] = \Drupal::service('flippy.pager')
          ->flippy_generate_link($nav['first']['nid'], $first_label);
      }
      elseif ($show_empty) {
        $vars['first'] = [
          '#type' => 'html_tag',
          '#tag' => 'span',
          '#attributes' => [
            'class' => [
              'empty',
            ],
          ],
          '#value' => $first_label,
        ];
      }
      if (isset($nav['last']) && $nav['last']['nid'] != FALSE) {
        $vars['last'] = \Drupal::service('flippy.pager')
          ->flippy_generate_link($nav['last']['nid'], $last_label);
      }
      elseif ($show_empty) {
        $vars['last'] = [
          '#type' => 'html_tag',
          '#tag' => 'span',
          '#attributes' => [
            'class' => [
              'empty',
            ],
          ],
          '#value' => $last_label,
        ];
      }
    }
    if ($flippy_settings
      ->get('flippy_random_' . $vars['node']
      ->getType())) {
      $vars['random'] = \Drupal::service('flippy.pager')
        ->flippy_generate_link($nav['random']['nid'], $random_label);
    }
    if (isset($nav['prev']) && $nav['prev']['nid'] != FALSE) {
      $vars['prev'] = \Drupal::service('flippy.pager')
        ->flippy_generate_link($nav['prev']['nid'], $prev_label);
    }
    elseif ($show_empty) {
      $vars['prev'] = [
        '#type' => 'html_tag',
        '#tag' => 'span',
        '#attributes' => [
          'class' => [
            'empty',
          ],
        ],
        '#value' => $prev_label,
      ];
    }
    if (isset($nav['next']) && $nav['next']['nid'] != FALSE) {
      $vars['next'] = \Drupal::service('flippy.pager')
        ->flippy_generate_link($nav['next']['nid'], $next_label);
    }
    elseif ($show_empty) {
      $vars['next'] = [
        '#type' => 'html_tag',
        '#tag' => 'span',
        '#attributes' => [
          'class' => [
            'empty',
          ],
        ],
        '#value' => $next_label,
      ];
    }
  }
  unset($vars['list']);
}