You are here

public function Acquiadam::getPager in Media: Acquia DAM 8

Create a custom pager.

1 call to Acquiadam::getPager()
Acquiadam::getForm in src/Plugin/EntityBrowser/Widget/Acquiadam.php

File

src/Plugin/EntityBrowser/Widget/Acquiadam.php, line 614

Class

Acquiadam
Uses a view to provide entity listing in a browser's widget.

Namespace

Drupal\media_acquiadam\Plugin\EntityBrowser\Widget

Code

public function getPager(Folder $current_folder, $page, $num_per_page) {

  // Add container for pager.
  $form['pager-container'] = [
    '#type' => 'container',
    // Store page number in container so it can be retrieved from form state.
    '#page' => $page,
    '#attributes' => [
      'class' => [
        'acquiadam-asset-browser-pager',
      ],
    ],
  ];

  // If not on the first page.
  if ($page > 0) {

    // Add a button to go to the first page.
    $form['pager-container']['first'] = [
      '#type' => 'button',
      '#value' => '<<',
      '#name' => 'acquiadam_pager',
      '#acquiadam_page' => 0,
      '#attributes' => [
        'class' => [
          'page-button',
          'page-first',
        ],
      ],
    ];

    // Add a button to go to the previous page.
    $form['pager-container']['previous'] = [
      '#type' => 'button',
      '#value' => '<',
      '#name' => 'acquiadam_pager',
      '#acquiadam_page' => $page - 1,
      '#attributes' => [
        'class' => [
          'page-button',
          'page-previous',
        ],
      ],
    ];
  }

  // Last available page based on number of assets in folder
  // divided by number of assets to show per page.
  $last_page = floor(($current_folder->numassets - 1) / $num_per_page);

  // First page to show in the pager.
  // Try to put the button for the current page in the middle by starting at
  // the current page number minus 4.
  $start_page = max(0, $page - 4);

  // Last page to show in the pager.  Don't go beyond the last available page.
  $end_page = min($start_page + 9, $last_page);

  // Create buttons for pages from start to end.
  for ($i = $start_page; $i <= $end_page; $i++) {
    $form['pager-container']['page_' . $i] = [
      '#type' => 'button',
      '#value' => $i + 1,
      '#name' => 'acquiadam_pager',
      '#acquiadam_page' => $i,
      '#attributes' => [
        'class' => [
          $i == $page ? 'page-current' : '',
          'page-button',
        ],
      ],
    ];
  }

  // If not on the last page.
  if ($end_page > $page) {

    // Add a button to go to the next page.
    $form['pager-container']['next'] = [
      '#type' => 'button',
      '#value' => '>',
      '#name' => 'acquiadam_pager',
      '#acquiadam_page' => $page + 1,
      '#attributes' => [
        'class' => [
          'page-button',
          'page-next',
        ],
      ],
    ];

    // Add a button to go to the last page.
    $form['pager-container']['last'] = [
      '#type' => 'button',
      '#value' => '>>',
      '#name' => 'acquiadam_pager',
      '#acquiadam_page' => $last_page,
      '#attributes' => [
        'class' => [
          'page-button',
          'page-last',
        ],
      ],
    ];
  }
  return $form;
}