diff -u b/composer.json b/composer.json --- b/composer.json +++ b/composer.json @@ -21,5 +21,5 @@ }, "require": { - "drupal/core": "^10.1 || ^11 || ^12" + "drupal/core": "^10 || ^11 || ^12" } } diff -u b/file_temporary_validator.info.yml b/file_temporary_validator.info.yml --- b/file_temporary_validator.info.yml +++ b/file_temporary_validator.info.yml @@ -3,5 +3,5 @@ description: This module will help in validating temporary file duplication. package: File Management -core_version_requirement: ^10.1 || ^11 || ^12 +core_version_requirement: ^10 || ^11 || ^12 dependencies: - drupal:file reverted: --- b/file_temporary_validator.module +++ a/file_temporary_validator.module @@ -5,19 +5,31 @@ * Primary module hooks for File Temporary Validator module. */ -use Drupal\Core\Hook\Attribute\LegacyHook; -use Drupal\file_temporary_validator\Hook\FileTemporaryValidatorHooks; use Drupal\Core\Form\FormStateInterface; use Drupal\file\FileInterface; +use Drupal\file\Plugin\Field\FieldType\FileFieldItemList; /** * Implements hook_field_widget_single_element_form_alter(). * * {@inheritdoc} */ -#[LegacyHook] function file_temporary_validator_field_widget_single_element_form_alter(array &$element, FormStateInterface $form_state, array $context): void { + if (isset($element['#type']) && in_array($element['#type'], ['managed_file', 'managed_file_plus'])) { + $settings = $context['items']->getFieldDefinition()->getThirdPartySettings('file_temporary_validator'); + + if (isset($settings['enabled']) && $settings['enabled']) { + $upload_validators = $element['#upload_validators'] ?? []; + + if (version_compare(\Drupal::VERSION, '10.2.0', '<')) { + $upload_validators = ['file_temporary_validator_validate' => []] + $upload_validators; + } + else { + $upload_validators = ['duplicate_upload_validator' => []] + $upload_validators; + } + $element['#upload_validators'] = $upload_validators; + } + } - \Drupal::service(FileTemporaryValidatorHooks::class)->fieldWidgetSingleElementFormAlter($element, $form_state, $context); } /** @@ -44,9 +56,30 @@ * * {@inheritdoc} */ -#[LegacyHook] function file_temporary_validator_form_field_config_edit_form_alter(array &$form, FormStateInterface $form_state): void { + /** @var \Drupal\Core\Entity\EntityFormInterface $field_object */ + $field_object = $form_state->getFormObject(); + /** @var \Drupal\field\FieldConfigInterface $field_config */ + $field_config = $field_object->getEntity(); + $class = $field_config->getClass(); + + if (class_exists($class) && new $class($field_config->getItemDefinition()) instanceof FileFieldItemList) { + $settings = $field_config->getThirdPartySettings('file_temporary_validator'); + + $form['settings']['file_temporary_validator'] = [ + '#parents' => ['third_party_settings', 'file_temporary_validator'], + '#tree' => TRUE, + '#type' => 'container', + '#weight' => 2, + ]; + + $form['settings']['file_temporary_validator']['enabled'] = [ + '#default_value' => $settings['enabled'] ?? FALSE, + '#description' => t('File Temporary Validator restricts uploading files if the file with the same name already exists in the temporary folder.'), + '#title' => t('Enable File Temporary Validator?'), + '#type' => 'checkbox', + ]; + } - \Drupal::service(FileTemporaryValidatorHooks::class)->formFieldConfigEditFormAlter($form, $form_state); } /** @@ -54,7 +87,17 @@ * * {@inheritdoc} */ -#[LegacyHook] function file_temporary_validator_help($route_name): string { + switch ($route_name) { + case 'help.page.file_temporary_validator': + $output = ''; + $output .= '
'; + $output .= t('The File Temporary Validator module validates that there are currently no active temporary files with that filename. If there is, the file will not be uploaded and an error message will be displayed. This forces the site builder to close browser tabs or force delete files that are stuck in the temporary directory. This is helpful in preventing underscores in filenames.'); + $output .= '
'; + $output .= '' . t('Visit the Project page on Drupal.org for more information.', [ + ':project_link' => 'https://www.drupal.org/project/file_temporary_validator', + ]); + return $output; + } + return ''; - return \Drupal::service(FileTemporaryValidatorHooks::class)->help($route_name); } reverted: --- b/file_temporary_validator.services.yml +++ a/file_temporary_validator.services.yml @@ -2,7 +2,3 @@ file_temporary_validator: class: Drupal\file_temporary_validator\Service\FileTemporaryValidator arguments: ['@entity_type.manager', '@current_user'] - - Drupal\file_temporary_validator\Hook\FileTemporaryValidatorHooks: - class: Drupal\file_temporary_validator\Hook\FileTemporaryValidatorHooks - autowire: true reverted: --- b/src/Hook/FileTemporaryValidatorHooks.php +++ /dev/null @@ -1,98 +0,0 @@ -getFieldDefinition()->getThirdPartySettings('file_temporary_validator'); - if (isset($settings['enabled']) && $settings['enabled']) { - $upload_validators = $element['#upload_validators'] ?? []; - if (version_compare(\Drupal::VERSION, '10.2.0', '<')) { - $upload_validators = [ - 'file_temporary_validator_validate' => [], - ] + $upload_validators; - } - else { - $upload_validators = [ - 'duplicate_upload_validator' => [], - ] + $upload_validators; - } - $element['#upload_validators'] = $upload_validators; - } - } - } - - /** - * Implements hook_form_FORM_ID_alter(). - * - * {@inheritdoc} - */ - #[Hook('form_field_config_edit_form_alter')] - public function formFieldConfigEditFormAlter(array &$form, FormStateInterface $form_state): void { - /** @var \Drupal\Core\Entity\EntityFormInterface $field_object */ - $field_object = $form_state->getFormObject(); - /** @var \Drupal\field\FieldConfigInterface $field_config */ - $field_config = $field_object->getEntity(); - $class = $field_config->getClass(); - if (class_exists($class) && new $class($field_config->getItemDefinition()) instanceof FileFieldItemList) { - $settings = $field_config->getThirdPartySettings('file_temporary_validator'); - $form['settings']['file_temporary_validator'] = [ - '#parents' => [ - 'third_party_settings', - 'file_temporary_validator', - ], - '#tree' => TRUE, - '#type' => 'container', - '#weight' => 2, - ]; - $form['settings']['file_temporary_validator']['enabled'] = [ - '#default_value' => $settings['enabled'] ?? FALSE, - '#description' => $this->t('File Temporary Validator restricts uploading files if the file with the same name already exists in the temporary folder.'), - '#title' => $this->t('Enable File Temporary Validator?'), - '#type' => 'checkbox', - ]; - } - } - - /** - * Implements hook_help(). - * - * {@inheritdoc} - */ - #[Hook('help')] - public function help($route_name): string { - switch ($route_name) { - case 'help.page.file_temporary_validator': - $output = ''; - $output .= '
'; - $output .= $this->t('The File Temporary Validator module validates that there are currently no active temporary files with that filename. If there is, the file will not be uploaded and an error message will be displayed. This forces the site builder to close browser tabs or force delete files that are stuck in the temporary directory. This is helpful in preventing underscores in filenames.'); - $output .= '
'; - $output .= '' . $this->t('Visit the Project page on Drupal.org for more information.', [ - ':project_link' => 'https://www.drupal.org/project/file_temporary_validator', - ]); - return $output; - } - return ''; - } - -}