Browse Source

Merge pull request #3 from discoverygarden/ctda9-107

CTDA9-107
pull/5/head v1.1.0
Noel Chiasson 3 years ago committed by GitHub
parent
commit
87a0c2d97e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 10
      config/schema/dgi_fixity.schema.yml
  2. 1
      dgi_fixity.info.yml
  3. 114
      dgi_fixity.module

10
config/schema/dgi_fixity.schema.yml

@ -23,3 +23,13 @@ dgi_fixity.settings:
notify_user_threshold:
type: string
label: 'Time elapsed between notifications'
field.widget.third_party.dgi_fixity:
type: mapping
mapping:
validate:
type: boolean
label: Show Validate Upload Elements
validate_require:
type: boolean
label: Require Checksums

1
dgi_fixity.info.yml

@ -9,4 +9,5 @@ dependencies:
- drupal:media
- drupal:user
- drupal:views
- drupal:field_ui
- filehash:filehash

114
dgi_fixity.module

@ -10,10 +10,15 @@ use Drupal\Core\Config\FileStorage;
use Drupal\Core\Config\InstallStorage;
use Drupal\Core\Config\StorageInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Field\WidgetInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Mail\MailFormatHelper;
use Drupal\Core\Render\Element;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Url;
use Drupal\dgi_fixity\Form\SettingsForm;
use Drupal\file\Plugin\Field\FieldWidget\FileWidget;
use Drupal\user\Entity\User;
/**
@ -231,3 +236,112 @@ function dgi_fixity_help($route_name, RouteMatchInterface $route_match) {
return $output;
}
}
/**
* Gets dgi_fixity file widget settings.
*/
function _dgi_fixity_file_widget_validate_settings(FileWidget $plugin) {
return [
'validate' => $plugin->getThirdPartySetting('dgi_fixity', 'validate', FALSE),
'validate_require' => $plugin->getThirdPartySetting('dgi_fixity', 'validate_require', FALSE),
];
}
/**
* Implements hook_field_widget_third_party_settings_form().
*/
function dgi_fixity_field_widget_third_party_settings_form(WidgetInterface $plugin, FieldDefinitionInterface $field_definition, $form_mode, $form, FormStateInterface $form_state) {
$element = [];
if ($plugin instanceof FileWidget) {
$settings = _dgi_fixity_file_widget_validate_settings($plugin);
$element['validate'] = [
'#type' => 'checkbox',
'#title' => \t('Show Validate Upload Elements'),
'#description' => \t('Displays a field for each enabled <em>filehash</em> algorithm, allowing the user to validate the uploaded file(s).'),
'#default_value' => $settings['validate'],
];
$element['validate_require'] = [
'#type' => 'checkbox',
'#title' => \t('Require Checksums'),
'#description' => \t('User is prevented from submitting the form unless all enabled <em>filehash</em> algorithms match the user provided values.'),
'#default_value' => $settings['validate_require'],
'#states' => [
'visible' => [
":input[name=\"fields[{$field_definition->getName()}][settings_edit_form][third_party_settings][dgi_fixity][validate]\"]" => ['checked' => TRUE],
],
],
];
}
return $element;
}
/**
* Implements hook_field_widget_single_element_form_alter().
*/
function dgi_fixity_field_widget_single_element_form_alter(&$element, FormStateInterface $form_state, $context) {
// Set a message if this is for the form displayed to set default value for
// the field.
$plugin = $context['widget'] ?? NULL;
if ($plugin instanceof FileWidget) {
$settings = _dgi_fixity_file_widget_validate_settings($plugin);
if ($settings['validate']) {
/** @var \Drupal\filehash\FileHashInterface $filehash */
$filehash = \Drupal::service('filehash');
$labels = $filehash->labels();
$descriptions = $filehash->descriptions();
$element['#process'][] = '_dgi_fixity_file_widget_process';
$element['#element_validate'][] = '_dgi_fixity_file_widget_validate';
$element['algorithms'] = [
'#title' => \t('Validate Upload'),
'#type' => 'details',
'#weight' => 100,
];
foreach ($filehash->columns() as $column) {
$element['algorithms'][$column] = [
'#type' => 'textfield',
'#title' => $labels[$column],
'#description' => $descriptions[$column],
'#column' => $column,
'#required' => $settings['validate_require'],
];
}
}
}
}
/**
* Sets default values for checksums if none provided.
*
* Done in the process step as the FileWidget process step is responsible for
* loading the file entity from which the default is derived.
*/
function _dgi_fixity_file_widget_process(&$element, FormStateInterface $form_state, &$complete_form) {
$file = reset($element['#files']);
$element['algorithms']['#access'] = $file != FALSE;
foreach (Element::children($element['algorithms']) as $column) {
$default_value = $element['#value']['algorithms'][$column] ?? $file->{$column}->value ?? NULL;
$element['algorithms'][$column]['#default_value'] = $default_value;
}
return $element;
}
/**
* Validate user provided value against the value calculated by filehash.
*/
function _dgi_fixity_file_widget_validate($element, FormStateInterface $form_state) {
$file = reset($element['#files']);
foreach (Element::children($element['algorithms']) as $column) {
$algorithm = &$element['algorithms'][$column];
$provided = $algorithm['#value'];
$expected = $file->{$column}->value;
// If not required and no value given skip validation.
$ignore = !$algorithm['#required'] && empty($provided);
if (!$ignore && $provided !== $expected) {
$form_state->setError($algorithm, \t(
'Provided value "@provided" did not match expected value "@expected".',
['@provided' => $provided, '@expected' => $expected]
));
}
}
}

Loading…
Cancel
Save