Updated all the contrib modules to their latest versions.
[yaffs-website] / web / modules / contrib / file_mdm / src / Element / FileMetadataCaching.php
1 <?php
2
3 namespace Drupal\file_mdm\Element;
4
5 use Drupal\Core\Form\FormStateInterface;
6 use Drupal\Core\Render\Element\FormElement;
7
8 /**
9  * Implements a form element to enable capturing cache information for file_mdm.
10  *
11  * @FormElement("file_mdm_caching")
12  */
13 class FileMetadataCaching extends FormElement {
14
15   /**
16    * {@inheritdoc}
17    */
18   public function getInfo() {
19     $class = get_class($this);
20     return [
21       '#input' => TRUE,
22       '#process' => [[$class, 'processCaching']],
23       '#element_validate' => [[$class, 'validateCaching']],
24     ];
25   }
26
27   /**
28    * {@inheritdoc}
29    */
30   public static function valueCallback(&$element, $input, FormStateInterface $form_state) {
31     if ($input !== FALSE && $input !== NULL) {
32       $disallowed_paths = $input['disallowed_paths'];
33       if (!empty($disallowed_paths)) {
34         $disallowed_paths = preg_replace('/\r/', '', $disallowed_paths);
35         $disallowed_paths = explode("\n", $disallowed_paths);
36         while (empty($disallowed_paths[count($disallowed_paths) - 1])) {
37           array_pop($disallowed_paths);
38         }
39         $input['disallowed_paths'] = $disallowed_paths ?: [];
40       }
41       else {
42         $input['disallowed_paths'] = [];
43       }
44       return $input;
45     }
46     return NULL;
47   }
48
49   /**
50    * Processes a 'file_mdm_caching' form element.
51    *
52    * @param array $element
53    *   The form element to process.
54    * @param \Drupal\Core\Form\FormStateInterface $form_state
55    *   The current state of the form.
56    * @param array $complete_form
57    *   The complete form structure.
58    *
59    * @return array
60    *   The processed element.
61    */
62   public static function processCaching(array &$element, FormStateInterface $form_state, array &$complete_form) {
63     $element['enabled'] = [
64       '#type' => 'checkbox',
65       '#title' => t('Cache metadata'),
66       '#default_value' => $element['#default_value']['enabled'],
67       '#description' => t("If selected, metadata retrieved from files will be cached for further access."),
68     ];
69     $options = [86400, 172800, 604800, 1209600, 3024000, 7862400];
70     $options = array_map([\Drupal::service('date.formatter'), 'formatInterval'], array_combine($options, $options));
71     $options = [-1 => t('Never')] + $options;
72     $element['expiration'] = [
73       '#type' => 'select',
74       '#title' => t('Cache expires'),
75       '#default_value' => $element['#default_value']['expiration'],
76       '#options' => $options,
77       '#description' => t("Specify the required lifetime of cached entries. Longer times may lead to increased cache sizes."),
78       '#states' => [
79         'visible' => [
80           ':input[name="' . $element['#name'] . '[enabled]"]' => array('checked' => TRUE),
81         ],
82       ],
83     ];
84     $element['disallowed_paths'] = [
85       '#type' => 'textarea',
86       '#title' => t('Excluded paths'),
87       '#rows' => 3,
88       '#default_value' => implode("\n", $element['#default_value']['disallowed_paths']),
89       '#description' => t("Only files prefixed by a valid URI scheme will be cached, like for example <kbd>public://</kbd>. Files in the <kbd>temporary://</kbd> scheme will never be cached. Specify here if there are any paths to be additionally <strong>excluded</strong> from caching, one per line. Use wildcard patterns when entering the path. For example, <kbd>public://styles/*</kbd>."),
90       '#states' => [
91         'visible' => [
92           ':input[name="' . $element['#name'] . '[enabled]"]' => array('checked' => TRUE),
93         ],
94       ],
95     ];
96
97     return $element;
98   }
99
100   /**
101    * Form element validation handler.
102    */
103   public static function validateCaching(&$element, FormStateInterface $form_state, &$complete_form) {
104     // Validate cache exclusion paths.
105     foreach ($element['#value']['disallowed_paths'] as $path) {
106       if (!file_valid_uri($path)) {
107         $form_state->setError($element['disallowed_paths'], t("'@path' is an invalid URI path", ['@path' => $path]));
108       }
109     }
110   }
111
112 }