Completed
Push — 8.x-1.x ( 200447...d6a817 )
by Janez
02:18
created

EntityBrowserPagerElement::getInfo()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 8
nc 1
nop 0
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Drupal\entity_browser\Element;
4
5
use Drupal\Core\Form\FormStateInterface;
6
use Drupal\Core\Render\Element\FormElement;
7
8
/**
9
 * Provides an Entity Browser pager form element.
10
 *
11
 * Properties:
12
 * - #total_pages: Total number of pages. This is optional with default
13
 *   value set on NULL. With default value pager can't calculate last page
14
 *   correctly and "next" will be available even on last page. For
15
 *   correct functionality #total_pages must be set up.
16
 *
17
 * Example:
18
 * @code
19
 *   $form['pager'] = [
20
 *     '#type' => 'entity_browser_pager',
21
 *     '#total_pages' => 12,
22
 *   ];
23
 * @endcode
24
 *
25
 * Number of the current page is stored in the form state. In order to get it
26
 * the provided helper function needs to be utilized:
27
 *
28
 * @code
29
 *   $page = EntityBrowserPagerElement::getCurrentPage($form_state);
30
 * @endcode
31
 *
32
 * @see ::getCurrentPage($form_state).
33
 *
34
 * @FormElement("entity_browser_pager")
35
 */
36
class EntityBrowserPagerElement extends FormElement {
37
38
  /**
39
   * {@inheritdoc}
40
   */
41
  public function getInfo() {
42
    $class = get_class($this);
43
    return [
44
      '#process' => [[$class, 'processEntityBrowserPager']],
45
      '#theme_wrappers' => ['form_element'],
46
      '#total_pages' => NULL,
47
      '#attached' => [
48
        'library' => ['entity_browser/pager'],
49
      ],
50
    ];
51
  }
52
53
  /**
54
   * Process Entity browser pager element.
55
   */
56
  public static function processEntityBrowserPager(&$element, FormStateInterface $form_state, &$complete_form) {
57
    $page = static::getCurrentPage($form_state);
58
59
    $element['previous'] = [
60
      '#type' => 'submit',
61
      '#submit' => [[static::class, 'submitPager']],
62
      '#value' => t('‹ Previous'),
63
      '#name' => 'prev_page',
64
      '#disabled' => $page === 1,
65
      '#attributes' => ['class' => ['prev']],
66
      '#limit_validation_errors' => [array_merge($element['#parents'], ['previous'])],
67
    ];
68
    $element['current'] = [
69
      '#type' => 'html_tag',
70
      '#tag' => 'span',
71
      '#value' => t('Page @page', ['@page' => $page]),
72
      '#attributes' => ['class' => ['current']],
73
    ];
74
    $element['next'] = [
75
      '#type' => 'submit',
76
      '#submit' => [[static::class, 'submitPager']],
77
      '#value' => t('Next ›'),
78
      '#name' => 'next_page',
79
      '#disabled' => $element['#total_pages'] === $page,
80
      '#attributes' => ['class' => ['next']],
81
      '#limit_validation_errors' => [array_merge($element['#parents'], ['next'])],
82
    ];
83
84
    return $element;
85
  }
86
87
  /**
88
   * Submit handler for next and previous buttons.
89
   *
90
   * @param \Drupal\Core\Form\FormStateInterface $form_state
91
   *   Form state.
92
   */
93
  public static function submitPager($form, FormStateInterface $form_state) {
94
    $page = static::getCurrentPage($form_state);
95
96
    $triggering_element = $form_state->getTriggeringElement();
97
    if ($triggering_element['#name'] == 'prev_page') {
98
      $page--;
99
    }
100
    elseif ($triggering_element['#name'] == 'next_page') {
101
      $page++;
102
    }
103
104
    $form_state->set('page', $page);
105
    $form_state->setRebuild();
106
  }
107
108
  /**
109
   * Gets current page from the form state.
110
   *
111
   * @param \Drupal\Core\Form\FormStateInterface $form_state
112
   *   Form state.
113
   *
114
   * @return int
115
   *   Current page.
116
   */
117
  public static function getCurrentPage(FormStateInterface $form_state) {
118
    return !empty($form_state->get('page')) ? $form_state->get('page') : 1;
119
  }
120
121
  /**
122
   * Sets current page.
123
   *
124
   * @param \Drupal\Core\Form\FormStateInterface $form_state
125
   *   Form state.
126
   * @param int $page
127
   *   (Optional) Page to set as current. Pager will be reset to the first page
128
   *   if omitted.
129
   */
130
  public static function setCurrentPage(FormStateInterface $form_state, $page = 1) {
131
    $form_state->set('page', $page);
132
  }
133
}
134