3 namespace Drupal\file_mdm\Element;
5 use Drupal\Core\Form\FormStateInterface;
6 use Drupal\Core\Render\Element\FormElement;
9 * Implements a form element to enable capturing cache information for file_mdm.
11 * @FormElement("file_mdm_caching")
13 class FileMetadataCaching extends FormElement {
18 public function getInfo() {
19 $class = get_class($this);
22 '#process' => [[$class, 'processCaching']],
23 '#element_validate' => [[$class, 'validateCaching']],
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);
39 $input['disallowed_paths'] = $disallowed_paths ?: [];
42 $input['disallowed_paths'] = [];
50 * Processes a 'file_mdm_caching' form element.
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.
60 * The processed element.
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."),
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'] = [
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."),
80 ':input[name="' . $element['#name'] . '[enabled]"]' => array('checked' => TRUE),
84 $element['disallowed_paths'] = [
85 '#type' => 'textarea',
86 '#title' => t('Excluded paths'),
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>."),
92 ':input[name="' . $element['#name'] . '[enabled]"]' => array('checked' => TRUE),
101 * Form element validation handler.
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]));