You are here

public function FlippyPager::flippy_build_list in DXPR Theme Helper 8

Helper function: Query to get the list of flippy pagers.

@parameter current node object

Return value

array a list of flippy pagers

File

src/FlippyPager.php, line 96

Class

FlippyPager
Defines the flippy pager service.

Namespace

Drupal\glazed_helper

Code

public function flippy_build_list($node) {

  // Get all the properties from the current node.
  $master_list =& drupal_static(__FUNCTION__);
  if (!isset($master_list)) {
    $master_list = [];
  }
  if (!isset($master_list[$node
    ->id()])) {
    $sort = 'created';

    // Depending on order, decide what before and after means.
    $before = '<';
    $after = '>';

    // Also decide what up and down means
    $up = 'ASC';
    $down = 'DESC';

    // Create a starting-point EntityQuery object.
    $query = $this->nodeQuery;
    $query
      ->condition('type', $node
      ->getType())
      ->condition('status', 1)
      ->condition('nid', $node
      ->id(), '!=')
      ->addTag('node_access');

    // Create the individual queries
    $prev = clone $query;
    $next = clone $query;

    // Otherwise we assume the variable is a column in the base table
    // (a property). Like above, set the conditions.
    $sort_value = $node
      ->get($sort);
    $sort_value = $sort_value
      ->getValue();

    // Previous query to find out the previous item based on the field,
    // using node id if the other criteria is the same.
    $field_default_condition = $prev
      ->andConditionGroup()
      ->condition($sort, $sort_value[0]['value'])
      ->condition('nid', $node
      ->id(), $before);
    $field_sorting_group = $prev
      ->orConditionGroup()
      ->condition($sort, $sort_value[0]['value'], $before)
      ->condition($field_default_condition);
    $prev
      ->condition($field_sorting_group);

    // Next query to find out the next item based on the field, using
    // node id if the other criteria is the same.
    $field_default_condition = $next
      ->andConditionGroup()
      ->condition($sort, $sort_value[0]['value'])
      ->condition('nid', $node
      ->id(), $after);
    $field_sorting_group = $next
      ->orConditionGroup()
      ->condition($sort, $sort_value[0]['value'], $after)
      ->condition($field_default_condition);
    $next
      ->condition($field_sorting_group);

    // Set the ordering.
    $prev
      ->sort($sort, $down);
    $next
      ->sort($sort, $up);

    // Event dispatcher.
    $queries = [
      'prev' => $prev,
      'next' => $next,
    ];

    // Execute the queries.
    $results = [];
    $results['prev'] = $queries['prev']
      ->range(0, 1)
      ->execute();
    $results['prev'] = !empty($results['prev']) ? array_values($results['prev'])[0] : NULL;
    $results['next'] = $queries['next']
      ->range(0, 1)
      ->execute();
    $results['next'] = !empty($results['next']) ? array_values($results['next'])[0] : NULL;
    $node_ids = [];
    foreach ($results as $key => $result) {

      // If the query returned no results, it means we're already
      // at the beginning/end of the pager, so ignore those.
      if (is_numeric($result)) {

        // Otherwise we save the node ID.
        $node_ids[$key] = (int) $result;
        continue;
      }
      if (is_array($result) && count($result) > 0) {

        // Otherwise we save the node ID.
        $node_ids[$key] = $results[$key];
      }
    }

    // Make our final array of node IDs and titles.
    $list = [];

    // but only if we actually found some matches
    if (count($node_ids) > 0) {
      foreach ($node_ids as $key => $nid) {
        $list[$key] = [
          'nid' => $nid,
        ];
      }
    }

    // Finally set the current info for themers to use.
    $list['current'] = [
      'nid' => $node
        ->id(),
    ];
    $master_list[$node
      ->id()] = $list;
  }
  return $master_list[$node
    ->id()];
}