pager example

/** * {@inheritdoc} */
  protected function getEntityIds() {
    $query = $this->getStorage()->getQuery()
      ->accessCheck(TRUE)
      ->sort($this->entityType->getKey('id'));
    $query->condition('reusable', TRUE);

    // Only add the pager if a limit is specified.     if ($this->limit) {
      $query->pager($this->limit);
    }
    return $query->execute();
  }

}
$form['total'] = [
        '#markup' => $this->t(
          'There are 0 @entity_type_plural to delete.',
          ['@entity_type_plural' => $entity_type->getPluralLabel()]
        ),
      ];
    }
    elseif ($accessible_count > 0 && $entity_type->hasKey('label')) {
      $recent_entity_ids = $storage->getQuery()
        ->accessCheck(TRUE)
        ->sort($entity_type->getKey('id'), 'DESC')
        ->pager(10)
        ->execute();
      $recent_entities = $storage->loadMultiple($recent_entity_ids);

      $labels = [];
      foreach ($recent_entities as $entity) {
        $labels[] = $entity->label();
      }

      if ($labels) {
        $form['recent_entity_labels'] = [
          '#theme' => 'item_list',
          
/** * {@inheritdoc} */
  protected function getEntityIds() {
    $query = $this->getStorage()->getQuery()
      ->accessCheck(TRUE)
      ->sort('changed', 'DESC');

    // Only add the pager if a limit is specified.     if ($this->limit) {
      $query->pager($this->limit);
    }
    return $query->execute();
  }

}

  protected function getEntityIds() {
    $query = $this->getStorage()->getQuery()->accessCheck(TRUE);

    $search = $this->currentRequest->query->get('search');
    if ($search) {
      $query->condition('alias', $search, 'CONTAINS');
    }

    // Only add the pager if a limit is specified.     if ($this->limit) {
      $query->pager($this->limit);
    }

    // Allow the entity query to sort using the table header.     $header = $this->buildHeader();
    $query->tableSort($header);

    return $query->execute();
  }

  /** * {@inheritdoc} */
'data' => $this->t('Updated'),
        'specifier' => 'changed',
        'sort' => 'desc',
        'class' => [RESPONSIVE_PRIORITY_LOW],
      ],
      'operations' => $this->t('Operations'),
    ];
    $cids = $this->commentStorage->getQuery()
      ->accessCheck(TRUE)
      ->condition('status', $status)
      ->tableSort($header)
      ->pager(50)
      ->execute();

    /** @var \Drupal\comment\CommentInterface[] $comments */
    $comments = $this->commentStorage->loadMultiple($cids);

    // Build a table listing the appropriate comments.     $options = [];
    $destination = $this->getDestinationArray();

    $commented_entity_ids = [];
    $commented_entities = [];

    
    $request = Request::createFromGlobals();
    $request->query->replace([
      'page' => '0,2',
    ]);
    \Drupal::getContainer()->get('request_stack')->push($request);
    $this->queryResults = $this->storage
      ->getQuery()
      ->accessCheck(FALSE)
      ->sort("$figures.color")
      ->sort("$greetings.format")
      ->sort('id')
      ->pager(4, 1)
      ->execute();
    $this->assertResult(15, 6, 7, 1);

    // Now test the reversed order.     $query = $this->storage
      ->getQuery()
      ->accessCheck(FALSE)
      ->sort("$figures.color", 'DESC')
      ->sort("$greetings.format", 'DESC')
      ->sort('id', 'DESC');
    $count_query = clone $query;
    

  protected function getRevisionIds(NodeInterface $node, NodeStorageInterface $node_storage) {
    $result = $node_storage->getQuery()
      ->accessCheck(TRUE)
      ->allRevisions()
      ->condition($node->getEntityType()->getKey('id')$node->id())
      ->sort($node->getEntityType()->getKey('revision'), 'DESC')
      ->pager(50)
      ->execute();
    return array_keys($result);
  }

}
$edit = [
      'title[0][value]' => 'Menu title',
      'link[0][uri]' => '/admin/foo',
      'menu_parent' => 'admin:system.admin',
    ];
    $this->drupalGet('admin/structure/menu/manage/admin/add');
    $this->submitForm($edit, 'Save');

    $menu_items = \Drupal::entityTypeManager()->getStorage('menu_link_content')->getQuery()
      ->accessCheck(FALSE)
      ->sort('id', 'DESC')
      ->pager(1)
      ->execute();
    $menu_item = end($menu_items);
    /** @var \Drupal\menu_link_content\MenuLinkContentInterface $menu_link_content */
    $menu_link_content = MenuLinkContent::load($menu_item);

    $edit = [];
    $edit['label'] = $this->randomMachineName(16);
    $view_id = $edit['id'] = strtolower($this->randomMachineName(16));
    $edit['description'] = $this->randomMachineName(16);
    $edit['page[create]'] = TRUE;
    $edit['page[path]'] = 'admin/foo';

    

    public function paginate(?int $perPage = null, string $group = 'default', ?int $page = null, int $segment = 0)
    {
        // Since multiple models may use the Pager, the Pager must be shared.         $pager = Services::pager();

        if ($segment) {
            $pager->setSegment($segment$group);
        }

        $page = $page >= 1 ? $page : $pager->getCurrentPage($group);
        // Store it in the Pager library, so it can be paginated in the views.         $this->pager = $pager->store($group$page$perPage$this->countAllResults(false)$segment);
        $perPage     = $this->pager->getPerPage($group);
        $offset      = ($pager->getCurrentPage($group) - 1) * $perPage;

        
protected function getEntityRevisionIds() {
    $query = $this->entityTypeManager->getStorage('content_moderation_state')->getAggregateQuery()
      ->accessCheck(TRUE)
      ->aggregate('content_entity_id', 'MAX')
      ->groupBy('content_entity_revision_id')
      ->condition('content_entity_type_id', $this->entityTypeId)
      ->condition('moderation_state', 'published', '<>')
      ->sort('content_entity_revision_id', 'DESC');

    // Only add the pager if a limit is specified.     if ($this->limit) {
      $query->pager($this->limit);
    }

    $result = $query->execute();

    return $result ? array_column($result, 'content_entity_revision_id') : [];
  }

  /** * {@inheritdoc} */
  public function buildHeader() {
    

  protected function getEntityListQuery(): QueryInterface {
    $query = $this->getStorage()->getQuery()
      ->accessCheck(TRUE)
      ->sort($this->entityType->getKey('id'));

    // Only add the pager if a limit is specified.     if ($this->limit) {
      $query->pager($this->limit);
    }
    return $query;
  }

  /** * {@inheritdoc} */
  public function getOperations(EntityInterface $entity) {
    $operations = $this->getDefaultOperations($entity);
    $operations += $this->moduleHandler()->invokeAll('entity_operation', [$entity]);
    $this->moduleHandler->alter('entity_operation', $operations$entity);
    
$container->get('redirect.destination')
    );
  }

  /** * {@inheritdoc} */
  public function load() {
    $entity_query = $this->storage->getQuery();
    $entity_query->accessCheck(TRUE);
    $entity_query->condition('uid', 0, '<>');
    $entity_query->pager(50);
    $header = $this->buildHeader();
    $entity_query->tableSort($header);
    $uids = $entity_query->execute();
    return $this->storage->loadMultiple($uids);
  }

  /** * {@inheritdoc} */
  public function buildHeader() {
    $header = [
      
public function testEntityQueryWithoutConditions() {
    $this->initializeWorkspacesModule();
    $this->switchToWorkspace('stage');

    // Add a workspace-specific revision to a pre-existing node.     $node = $this->entityTypeManager->getStorage('node')->load(2);
    $node->title->value = 'stage - 2 - r3 - published';
    $node->save();

    $query = $this->entityTypeManager->getStorage('node')->getQuery()->accessCheck(FALSE);
    $query->sort('nid');
    $query->pager(1);
    $result = $query->execute();

    $this->assertSame([1 => '1']$result);

    $query = $this->entityTypeManager->getStorage('node')->getQuery()->accessCheck(FALSE);
    $query->sort('nid', 'DESC');
    $query->pager(10);
    $result = $query->execute();

    $this->assertSame([3 => '2', 1 => '1']$result);
  }

  
$this->assertSame(['1', '2']array_values($this->queryResults));

    // Add a range to a query without a start parameter.     $this->queryResults = $this->entityStorage->getQuery()
      ->range(0, '3')
      ->sort('id', 'ASC')
      ->execute();
    $this->assertSame(['1', '2', '3']array_values($this->queryResults));

    // Apply a pager with limit 4.     $this->queryResults = $this->entityStorage->getQuery()
      ->pager('4', 0)
      ->sort('id', 'ASC')
      ->execute();
    $this->assertSame(['1', '2', '3', '4']array_values($this->queryResults));
  }

  /** * Tests sorting with tableSort on config entity queries. */
  public function testTableSort() {
    $header = [
      ['data' => 'ID', 'specifier' => 'id'],
      [
Home | Imprint | This part of the site doesn't use cookies.