foldershare-8.x-1.2/src/Form/UISearchBox.php

src/Form/UISearchBox.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
<?php
 
namespace Drupal\foldershare\Form;
 
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Routing\RedirectDestinationTrait;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Logger\LoggerChannelTrait;
use Drupal\Component\Plugin\PluginManagerInterface;
 
use Symfony\Component\DependencyInjection\ContainerInterface;
 
use Drupal\foldershare\Constants;
use Drupal\foldershare\FolderShareInterface;
use Drupal\foldershare\Entity\FolderShare;
 
/**
 * Creates a form for the search user interface associated with a view.
 *
 * While a site may have a site-wide generic search feature (often found in
 * the site's page header), THIS search UI provides localized search through
 * a folder tree. The search starts on the folder currently being displayed.
 *
 * The search form includes a search text field and hidden search submit
 * button. On a carriage-return in the search text field, the search is
 * submitted.
 *
 * The search form is disabled if:
 * - Drupal's core "search" module is not enabled.
 * - FolderShare's search plugin is not available.
 * - The current user does not have "search content" permission.
 * - The page's entity is disabled or hidden.
 *
 * <B>Warning:</B> This class is strictly internal to the FolderShare
 * module. The class's existance, name, and content may change from
 * release to release without any promise of backwards compatability.
 *
 * @ingroup foldershare
 */
final class UISearchBox extends FormBase {
 
  use RedirectDestinationTrait;
  use LoggerChannelTrait;
 
  /*--------------------------------------------------------------------
   *
   * Fields.
   *
   *--------------------------------------------------------------------*/
 
  /**
   * The module handler, set at construction time.
   *
   * @var \Drupal\Core\Extension\ModuleHandlerInterface
   */
  private $moduleHandler;
 
  /**
   * The search plugin manager, if any, set at construction time.
   *
   * @var \Drupal\Component\Plugin\PluginManagerInterface
   */
  private $searchPluginManager;
 
  /*--------------------------------------------------------------------
   *
   * Construction.
   *
   *--------------------------------------------------------------------*/
 
  /**
   * Constructs a new form.
   *
   * @param \Drupal\Core\Extension\ModuleHandlerInterface $moduleHandler
   *   The module handler.
   * @param \Drupal\Component\Plugin\PluginManagerInterface $searchPluginManager
   *   The manager of search plugins, including FolderShare's own.
   */
  public function __construct(
    ModuleHandlerInterface $moduleHandler,
    PluginManagerInterface $searchPluginManager = NULL) {
 
    $this->moduleHandler = $moduleHandler;
    $this->searchPluginManager = $searchPluginManager;
  }
 
  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    $mh = $container->get('module_handler');
    $spm = NULL;
    if ($mh->moduleExists('search') === TRUE) {
      $spm = $container->get('plugin.manager.search');
    }
 
    return new static($mh, $spm);
  }
 
  /*--------------------------------------------------------------------
   *
   * Form setup.
   *
   *--------------------------------------------------------------------*/
 
  /**
   * {@inheritdoc}
   */
  public function getFormId() {
    $name = str_replace('\\', '_', get_class($this));
    return mb_convert_case($name, MB_CASE_LOWER);
  }
 
  /*--------------------------------------------------------------------
   *
   * Form build.
   *
   *--------------------------------------------------------------------*/
 
  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $formState = NULL) {
    //
    // Validate search possible
    // ------------------------
    // Check that the Drupal core Search module is enabled and that this
    // module's own search plugin is available. If not, there is no search
    // form.
    if ($this->moduleHandler->moduleExists('search') === FALSE) {
      // When the search module is not enabled, do not return a search form.
      return NULL;
    }
 
    if ($this->searchPluginManager === NULL ||
        $this->searchPluginManager->hasDefinition(Constants::SEARCH_PLUGIN) === FALSE) {
      // This is odd. The search module is enabled, which should automatically
      // install this module's search plugin. Something is broken. Log the
      // error, but don't tell the user since that's just confusing.
      $this->getLogger(Constants::MODULE)->emergency($this->t(
        "The module's '@pluginName' search plugin is missing.\nPlease notify the site administrator.",
        [
          '@pluginName' => Constants::SEARCH_PLUGIN,
        ]));
      return NULL;
    }
 
    $user = $this->currentUser();
    if ($user->hasPermission('search content') === FALSE) {
      // When the user does not have search permission, do not return a
      // search form.
      return NULL;
    }
 
    // Get parent entity ID, if any.
    $args = $formState->getBuildInfo()['args'];
    if (empty($args) === TRUE || $args[0] === '') {
      // No parent entity. Default to showing a root list.
      $parentId = FolderShareInterface::USER_ROOT_LIST;
      $parent   = NULL;
      $disabled = FALSE;
    }
    else {
      // Parent entity ID should be the sole argument. Load it.
      // Loading could fail and return a NULL if the ID is bad.
      $parentId = (int) $args[0];
      $parent   = FolderShare::load($parentId);
      $disabled = $parent->isSystemDisabled() || $parent->isSystemHidden();
    }
 
    //
    // Define form classes
    // -------------------
    // Define classes used to mark the form and its items. These classes
    // are then used in CSS to style the form.
    $form['#attributes']['class'][] = 'foldershare-searchbox-form';
    $form['#attributes']['role'] = 'search';
 
    $uiClass       = 'foldershare-searchbox';
    $submitClass   = $uiClass . '-submit';
    $keywordsClass = $uiClass . '-keywords';
 
    //
    // Create UI
    // ---------
    // The UI is wrapped in a container annotated with parent attributes.
    // Children of the container include a search field and a submit button.
    $form[$uiClass] = [
      '#type'              => 'container',
      '#weight'            => -100,
      '#name'              => $uiClass,
      '#attributes'        => [
        'class'            => [
          $uiClass,
        ],
      ],
 
      // Add a search field.
      $keywordsClass       => [
        '#type'            => 'search',
        '#size'            => 15,
        '#name'            => $keywordsClass,
        '#default_value'   => '',
        '#disabled'        => $disabled,
        '#attributes'      => [
          'placeholder'    => $this->t('Search folder...'),
          // Adding 'results' triggers addition of the magnifying glass
          // icon on Webkit-based browsers (e.g. Safari).
          'results'        => '',
          'aria-label'     => $this->t('Search through files and folders'),
          'class'          => [
            $keywordsClass,
          ],
        ],
      ],
 
      // Add a submit button. The button is always hidden and is triggered
      // automatically by a carriage-return on the search field.
      $submitClass         => [
        '#type'            => 'submit',
        '#value'           => $this->t('Search'),
        '#name'            => $submitClass,
        '#disabled'        => $disabled,
        '#attributes'      => [
          'class'          => [
            'hidden',
            $submitClass,
          ],
        ],
      ],
    ];
 
    return $form;
  }
 
  /*--------------------------------------------------------------------
   *
   * Form validate
   *
   *--------------------------------------------------------------------*/
 
  /**
   * {@inheritdoc}
   */
  public function validateForm(array &$form, FormStateInterface $formState) {
    // Do nothing.
  }
 
  /*--------------------------------------------------------------------
   *
   * Form submit
   *
   *--------------------------------------------------------------------*/
 
  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $formState) {
    //
    // Validate
    // --------
    // If the search module is not enabled or the module's search plugin
    // cannot be found, then abort.
    if ($this->moduleHandler->moduleExists('search') === FALSE) {
      // No search module. This form should not have been created if there
      // was no search module enabled. Silently do nothing.
      return;
    }
 
    if ($this->searchPluginManager === NULL ||
        $this->searchPluginManager->hasDefinition(Constants::SEARCH_PLUGIN) === FALSE) {
      // No search plugin. This form should not have been created if there
      // was no search plugin. Silently do nothing.
      return;
    }
 
    $user = $this->currentUser();
    if ($user->hasPermission('search content') === FALSE) {
      // When the user does not have search permission, do not return a
      // search form.
      return NULL;
    }
 
    // Get parent entity ID, if any.
    $args = $formState->getBuildInfo()['args'];
    if (empty($args) === TRUE || $args[0] === '') {
      // No parent entity. Default to showing a root folder list.
      $parentId = FolderShareInterface::USER_ROOT_LIST;
      $parent   = NULL;
      $disabled = FALSE;
    }
    else {
      // Parent entity ID should be the sole argument. Load it.
      // Loading could fail and return a NULL if the ID is bad.
      $parentId = (int) $args[0];
      $parent   = FolderShare::load($parentId);
      $disabled = $parent->isSystemDisabled() || $parent->isSystemHidden();
    }
 
    // Abort if disabled.
    if ($disabled === TRUE) {
      // Item disabled so cannot search it and its children.
      return;
    }
 
    //
    // Search
    // ------
    // Redirect to a search results page for the module's search plugin.
    // Pass keywords and the parent ID as URL query parameters.
    $uiClass       = 'foldershare-searchbox';
    $keywordsClass = $uiClass . '-keywords';
 
    $keywords = $formState->getValue($keywordsClass);
    if (empty($keywords) === TRUE) {
      // No search keywords. Do nothing.
      return;
    }
 
    $formState->setRedirect(
      'search.view_' . Constants::SEARCH_PLUGIN,
      [],
      [
        'query' => [
          'keys'     => $keywords,
          'parentId' => $parentId,
        ],
      ]
    );
  }
 
}

Главная | Обратная связь

drupal hosting | друпал хостинг | it patrol .inc