foldershare-8.x-1.2/src/Form/AdminSettingsTraits/AdminSettingsFilesTab.php

src/Form/AdminSettingsTraits/AdminSettingsFilesTab.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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
<?php
 
namespace Drupal\foldershare\Form\AdminSettingsTraits;
 
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\StreamWrapper\PrivateStream;
 
use Drupal\foldershare\Constants;
use Drupal\foldershare\ManageFilenameExtensions;
use Drupal\foldershare\Settings;
use Drupal\foldershare\Utilities\LinkUtilities;
use Drupal\foldershare\Entity\FolderShare;
 
/**
 * Manages the "Files" tab for the module's settings form.
 *
 * <B>Warning:</B> This is an internal trait that is strictly used by
 * the AdminSettings form class. It is a mechanism to group functionality
 * to improve code management.
 *
 * @ingroup foldershare
 */
trait AdminSettingsFilesTab {
 
  /*---------------------------------------------------------------------
   *
   * Build.
   *
   *---------------------------------------------------------------------*/
 
  /**
   * Builds the fiels tab.
   *
   * @param array $form
   *   An associative array containing the structure of the form. The form
   *   is modified to include additional render elements for the tab.
   * @param \Drupal\Core\Form\FormStateInterface $formState
   *   The current state of the form.
   * @param string $tabGroup
   *   The name of the tab group.
   */
  private function buildFilesTab(
    array &$form,
    FormStateInterface $formState,
    string $tabGroup) {
 
    //
    // Find installed modules.
    // -----------------------
    // Tab sections vary their presentation based upon what modules are
    // installed at the site.
    $mh = \Drupal::service('module_handler');
 
    $helpInstalled = $mh->moduleExists('help');
 
    //
    // Set up tab names.
    // -----------------
    // Create the machine name for the tab and the tab's title.
    $tabMachineName = 'files';
    $tabTitle       = $this->t('Files');
    $tabPaneTitle   = $this->t('Manage files');
 
    //
    // Set up class names.
    // -------------------
    // Use a set of standard class names for tab sections.
    $moduleName = self::makeCssSafe(Constants::MODULE);
 
    $tabName = $moduleName . '_' . $tabMachineName . '_tab';
 
    $tabSubtitleClass        = $moduleName . '-settings-subtitle';
    $sectionWrapperClass     = $moduleName . '-settings-section-wrapper';
    $sectionClass            = $moduleName . '-settings-section';
    $sectionDescriptionClass = $moduleName . '-settings-section-description';
    $itemDefaultClass        = $moduleName . '-settings-item-default';
    $itemNoteClass           = $moduleName . '-settings-item-note';
    $warningClass            = $moduleName . '-warning';
 
    $restoreExtensionsButton = $moduleName . '_extensions_restore';
 
    //
    // Create the tab
    // --------------
    // Start the tab with a title, subtitle, and description.
    $form[$tabName] = [
      '#type'                    => 'details',
      '#open'                    => FALSE,
      '#group'                   => $tabGroup,
      '#title'                   => $tabTitle,
 
      '#description'             => [
        'subtitle'               => [
          '#type'                => 'html_tag',
          '#tag'                 => 'h2',
          '#value'               => $tabPaneTitle,
          '#attributes'          => [
            'class'              => [$tabSubtitleClass],
          ],
        ],
 
        'description'            => [
          '#type'                => 'html_tag',
          '#tag'                 => 'p',
          '#value'               => $this->t(
            'Control where files are saved and the file name extensions supported.'),
          '#attributes'          => [
            'class'              => [
              $moduleName . '-settings-description',
            ],
          ],
        ],
      ],
      '#attributes'              => [
        'class'                  => [
          $moduleName . '-settings-tab',
          $moduleName . '-files-tab',
        ],
      ],
    ];
 
    //
    // File system.
    // ------------
    // If the site currently has stored files, then the storage scheme
    // and subdirectory choice cannot be changed.
    //
    // If the site doesn't have stored files, but the private file system
    // has not been configured, then the storage scheme cannot be changed.
    $inUseWarning          = '';
    $noPrivateWarning      = '';
    $storageSchemeDisabled = FALSE;
 
    if (FolderShare::hasFiles() === TRUE) {
      // Create a link to the Drupal core system page to delete all content
      // for the entity type.
      $inUseWarning = [
        '#type'                  => 'html_tag',
        '#tag'                   => 'p',
        '#value'                 => $this->t(
          'This setting is disabled because there are still files managed by this module. You must @deleteAllLink files first.',
          [
            '@deleteAllLink'     => LinkUtilities::createRouteLink(
              'system.prepare_modules_entity_uninstall',
              '',
              $this->t('Delete all'),
              [
                'entity_type_id' => FolderShare::ENTITY_TYPE_ID,
              ]),
          ]),
        '#attributes'            => [
          'class'                => [
            $warningClass,
            $itemNoteClass,
          ],
        ],
      ];
      $storageSchemeDisabled = TRUE;
    }
 
    if (empty(PrivateStream::basePath()) === TRUE) {
      if ($helpInstalled === TRUE) {
        $noPrivateWarning = [
          '#type'                  => 'html_tag',
          '#tag'                   => 'p',
          '#value'                 => $this->t(
            'This setting is limited to the <em>Public</em> directory because a <em>Private</em> directory has not been set up yet (see @fileHelpLink and @fileDocLink, @fileSettingsLink settings, and the "file_private_path" setting in the site\'s "settings.php" file).',
            [
              '@fileSettingsLink'  => LinkUtilities::createRouteLink(
                'system.file_system_settings',
                '',
                $this->t('File system')),
              '@fileHelpLink'      => LinkUtilities::createHelpLink(
                'file',
                $this->t('File help')),
              '@fileDocLink'       => LinkUtilities::createDocLink(
                'file',
                $this->t('File documentation')),
            ]),
          '#attributes'            => [
            'class'                => [
              $warningClass,
              $itemNoteClass,
            ],
          ],
        ];
      }
      else {
        $noPrivateWarning = [
          '#type'                  => 'html_tag',
          '#tag'                   => 'p',
          '#value'                 => $this->t(
            'This setting is limited to the <em>Public</em> directory because a <em>Private</em> directory has not been set up yet (see @fileDocLink, @fileSettingsLink settings, and the "file_private_path" setting in the site\'s "settings.php" file).',
            [
              '@fileSettingsLink'  => LinkUtilities::createRouteLink(
                'system.file_system_settings',
                '',
                $this->t('File system')),
              '@fileDocLink'       => LinkUtilities::createDocLink(
                'file',
                $this->t('File documentation')),
            ]),
          '#attributes'            => [
            'class'                => [
              $warningClass,
              $itemNoteClass,
            ],
          ],
        ];
      }
      $storageSchemeDisabled = TRUE;
    }
 
    $options = [
      'public'  => $this->t('Public directory'),
      'private' => $this->t('Private directory'),
    ];
 
    $form[$tabName]['directories'] = [
      '#type'                    => 'details',
      '#title'                   => $this->t('Directories'),
      '#open'                    => FALSE,
      '#attributes'              => [
        'class'                  => [$sectionWrapperClass],
      ],
 
      'section'                  => [
        '#type'                  => 'container',
        '#attributes'            => [
          'class'                => [$sectionClass],
        ],
 
        'section-description'    => [
          '#type'                => 'html_tag',
          '#tag'                 => 'p',
          '#value'               => $this->t(
            "Store files locally in the site's <em>Public</em> or <em>Private</em> directories. <em>Private</em> directories provide better security."),
          '#attributes'          => [
            'class'              => [$sectionDescriptionClass],
          ],
        ],
 
        'directory-choice'       => [
          '#type'                => 'select',
          '#options'             => $options,
          '#default_value'       => Settings::getFileScheme(),
          '#disabled'            => $storageSchemeDisabled,
          '#name'                => 'directory-choice',
        ],
 
        // The default setting.
        'directory-choice-description' => [
          '#type'                => 'html_tag',
          '#tag'                 => 'p',
          '#value'               => $this->t(
            'Default: @default',
            [
              '@default'         => $options[Settings::getFileSchemeDefault()],
            ]),
          '#attributes'          => [
            'class'              => [
              $itemDefaultClass,
              $itemNoteClass,
            ],
          ],
        ],
      ],
    ];
 
    // Add warning if there are things disabled.
    if (empty($noPrivateWarning) === FALSE) {
      $form[$tabName]['directories']['section']['warning'] = $noPrivateWarning;
    }
    elseif (empty($inUseWarning) === FALSE) {
      $form[$tabName]['directories']['section']['warning'] = $inUseWarning;
    }
 
    //
    // File name extensions
    // --------------------
    // Select whether file uploads should be restricted based on their
    // file name extensions, and what extensions are allowed.
    $form[$tabName]['fileextensions'] = [
      '#type'                    => 'details',
      '#title'                   => $this->t('File name extensions'),
      '#open'                    => FALSE,
      '#attributes'              => [
        'class'                  => [$sectionWrapperClass],
      ],
      'section'                  => [
        '#type'                  => 'container',
        '#attributes'            => [
          'class'                => [$sectionClass],
        ],
 
        'section-description'    => [
          '#type'                => 'html_tag',
          '#tag'                 => 'p',
          '#value'               => $this->t(
            'Allow all file name extensions or restrict them.'),
          '#attributes'          => [
            'class'              => [$sectionDescriptionClass],
          ],
        ],
 
        // A checkbox to enable/disable allowing all extensions.
        'all-file-extensions'    => [
          '#type'                => 'checkbox',
          '#title'               => $this->t('Allow all extensions'),
          '#default_value'       => (Settings::getFileRestrictExtensions() === FALSE),
          '#return_value'        => 'enabled',
          '#name'                => 'all-file-extensions',
        ],
 
        // The default setting.
        'all-file-extensions-default' => [
          '#type'                => 'html_tag',
          '#tag'                 => 'p',
          '#value'               => $this->t('Default: Allow all extensions'),
          '#attributes'          => [
            'class'              => [
              $itemDefaultClass,
              $itemNoteClass,
            ],
          ],
        ],
 
        // Specific extensions.
        'set-extensions'         => [
          '#type'                => 'container',
          '#states'              => [
            'visible'            => [
              'input[name="all-file-extensions"]' => [
                'checked'        => FALSE,
              ],
            ],
          ],
 
          // A list of allowed extensions.
          'file-extensions'      => [
            '#type'              => 'textarea',
            '#title'             => $this->t('Allowed extensions:'),
            '#default_value'     => Settings::getAllowedNameExtensions(),
            '#required'          => FALSE,
            '#rows'              => 15,
            '#name'              => 'file-extensions',
            '#description'       => $this->t(
              'Separate extensions with spaces. Do not include dots. Changing this list does not affect files that are already on the site.'),
          ],
 
          // A button to restore the default configuration.
          $restoreExtensionsButton => [
            '#type'              => 'submit',
            '#value'             => $this->t('Restore'),
            '#name'              => $restoreExtensionsButton,
            '#attributes'        => [
              'class'            => [
                $restoreExtensionsButton,
              ],
            ],
          ],
 
          // A description for the restore button.
          'extensions-restore-description' => [
            '#type'              => 'html_tag',
            '#tag'               => 'p',
            '#value'             => $this->t('Restore extensions to default values.'),
            '#attributes'        => [
              'class'            => [
                $restoreExtensionsButton . '_description',
              ],
            ],
          ],
        ],
      ],
    ];
 
    // Get the current extensions, if any, on the form.
    $value = $formState->getValue('file-extensions');
    if ($value !== NULL) {
      $form[$tabName]['fileextensions']['section']['file-extensions']['#value'] = $value;
    }
  }
 
  /*---------------------------------------------------------------------
   *
   * Validate.
   *
   *---------------------------------------------------------------------*/
 
  /**
   * Validates form values.
   *
   * @param array $form
   *   The form configuration.
   * @param \Drupal\Core\Form\FormStateInterface $formState
   *   The entered values for the form.
   */
  private function validateFilesTab(
    array &$form,
    FormStateInterface $formState) {
 
    $moduleName = self::makeCssSafe(Constants::MODULE);
    $trigger = $formState->getTriggeringElement();
 
    //
    // Restore filename extensions.
    // ----------------------------
    // Restore the list of allowed file name extensions.
    $restoreExtensionsButton = $moduleName . '_extensions_restore';
 
    if ($trigger['#name'] === $restoreExtensionsButton) {
      // Revert extensions configuration.
      $value = Settings::getAllowedNameExtensionsDefault();
      Settings::setAllowedNameExtensions($value);
      ManageFilenameExtensions::setAllowedNameExtensions($value);
 
      // Reset the form's extensions list.
      $formInput = $formState->getUserInput();
      $formInput['file-extensions'] = $value;
      $formState->setUserInput($formInput);
 
      // Rebuild the page.
      $formState->setRebuild(TRUE);
      $formState->cleanValues();
      $formState->setTriggeringElement(NULL);
 
      \Drupal::messenger()->addMessage(
        $this->t('The file extensions list has been restored to its default values.'),
        'status');
      return;
    }
 
    //
    // Directories.
    // ------------
    // It must be "public" or "private". If "private", the site's private
    // file system must have been configured.
    $hasPrivate = empty(PrivateStream::basePath()) === FALSE;
 
    $scheme = $formState->getValue('directory-choice');
    if ($scheme !== 'public' && $scheme !== 'private') {
      $formState->setErrorByName(
        'directory-choice',
        $this->t('The directory choice must be either <em>Public</em> or <em>Private</em>.'));
    }
 
    if ($scheme === 'private' && $hasPrivate === FALSE) {
      $formState->setErrorByName(
        'directory-choice',
        $this->t('The <em>Private</em> directory is not available because the site has not been configured to support it yet.'));
    }
  }
 
  /*---------------------------------------------------------------------
   *
   * Submit.
   *
   *---------------------------------------------------------------------*/
 
  /**
   * Stores submitted form values.
   *
   * @param array $form
   *   The form configuration.
   * @param \Drupal\Core\Form\FormStateInterface $formState
   *   The entered values for the form.
   */
  private function submitFilesTab(
    array &$form,
    FormStateInterface $formState) {
 
    // File directory.
    if (FolderShare::hasFiles() === FALSE) {
      $value = $formState->getValue('directory-choice');
      Settings::setFileScheme($value);
    }
 
    // Allow all file extensions or not.
    $value = $formState->getValue('all-file-extensions');
    $allowAll = ($value === 'enabled') ? TRUE : FALSE;
    Settings::setFileRestrictExtensions($allowAll === FALSE);
 
    // Specific extensions, set only if not allowing all extensions.
    if ($allowAll === FALSE) {
      $extensions = $formState->getValue('file-extensions');
      Settings::setAllowedNameExtensions($extensions);
    }
 
    // Forward extensions to the file field.
    if ($allowAll === TRUE) {
      // Everything allowed. No extensions list.
      ManageFilenameExtensions::setAllowedNameExtensions('');
    }
    else {
      ManageFilenameExtensions::setAllowedNameExtensions(
        Settings::getAllowedNameExtensions());
    }
  }
 
}

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

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