file_bulkupload_translations-1.0.x-dev/file_bulkupload_translations.module
file_bulkupload_translations.module
<?php
/**
* @file
* Provides custom extensions related to document multiupload.
*/
declare(strict_types=1);
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\file\FileInterface;
/**
* Implements hook_form_FORM_ID_alter() for system_file_system_settings().
*
* Add translation rules according the filename.
*/
function file_bulkupload_translations_form_system_file_system_settings_alter(&$form, FormStateInterface $form_state, $form_id) {
$config = \Drupal::config('file_bulkupload_translations.settings');
$form['bulkupload_translation'] = [
'#type' => 'details',
'#title' => t('Bulkupload translations'),
'#description' => t('These settings only apply to new files as they are uploaded. Changes here do not affect existing file names.'),
'#open' => TRUE,
'#tree' => TRUE,
];
$tabset = &$form['bulkupload_translation'];
$tabset['bulkupload_translations__regex'] = [
'#type' => 'textfield',
'#title' => t('Regex to isolate the language code'),
'#description' => t('Example : prefix: /([a-zA-Z]{2})[-_]/i // suffix : /(?:[-_])([a-zA-Z]{2})(?=_\d+\.|[.-][a-zA-Z]+$)/i'),
'#default_value' => $config->get('regex') ?? '/[_-]([a-zA-Z]{2})(?=\.[a-zA-Z]+$)/i',
'#maxlength' => 255,
'#required' => TRUE,
'#weight' => 10,
];
$form['#submit'][] = '_file_bulkupload_translations_system_file_system_settings_submit';
}
/**
* Submit handler for the file system settings form.
*
* Save regex setting.
*/
function _file_bulkupload_translations_system_file_system_settings_submit(&$form, FormStateInterface $form_state) {
\Drupal::configFactory()->getEditable('file_bulkupload_translations.settings')
->set('regex', $form_state->getValues()['bulkupload_translation']['bulkupload_translations__regex'])
->save();
}
/**
* Implements hook_form_FORM_ID_alter() for 'field_storage_config_edit_form'.
*/
function file_bulkupload_translations_form_field_storage_config_edit_form_alter(&$form, FormStateInterface $form_state, $form_id) {
/** @var FieldStorageConfig $field_storage_config */
$field_storage_config = $form_state->getFormObject()->getEntity();
$entity_type_id = $form_state->getStorage()['entity_type_id'];
$bundle = $form_state->getStorage()['bundle'];
$display_config = \Drupal::entityTypeManager()
->getStorage('entity_form_display')
->load($entity_type_id . '.' . $bundle . '.default');
if (empty($display_config)) {
return;
}
$field_config = $display_config->getComponent($field_storage_config->getName());
if (empty($field_config['type']) || $field_config['type'] != 'file_bulkupload_translations_widget') {
return;
}
// Add callback submit to save the file_bulkupload_cardinality setting.
$form['#entity_builders'][] = '_file_bulkupload_translations_form_field_storage_config_submit';
// Locked the value of the cardinality.
$form['cardinality_container']['#disabled'] = TRUE;
$form['cardinality_container']['#description'] = t('Locked as Unlimited by File Bulkupload Translation module. <br>The limit of uploded files in a bulk will the PHP max_upload value.');
$original_cardinality = $field_storage_config->getCardinality();
$value = $field_storage_config->getThirdPartySetting('file_bulkupload_translations', 'cardinality', $original_cardinality);
$form['bulkupload_cardinality_number'] = [
'#type' => 'number',
'#min' => -1,
'#default_value' => $value,
'#title' => t('Bulkupload maximum number of Media entities impacted'),
'#description' => t('-1 as Unlimited'),
];
$form['cardinality_number']['#default_value'] = -1;
}
/**
* Implements hook_entity_bundle_field_info_alter().
*/
function file_bulkupload_translations_entity_bundle_field_info_alter(&$fields, EntityTypeInterface $entity_type, $bundle) {
foreach ($fields as &$field_definition) {
if ($field_definition->getType() !== 'file_bulkupload_translations') {
continue;
}
$field_definition->addConstraint('UniqueLanguageSuffix', []);
$field_definition->addConstraint('EnabledLanguageConstraint', []);
}
}
/**
* File validation callback; checks a valid language suffix in the filename.
*
* @param \Drupal\file\FileInterface $file
* The file entity.
*
* @return array
* An array of error messages or empty if check was ok.
*/
function file_bulkupload_translations_file_validate_language_suffix(FileInterface $file) {
/** @var Drupal\file_bulkupload_translations\ContentMultiUploadTranslationHandler $form_handler */
$form_handler = \Drupal::service('file_bulkupload_translations.content_multiupload_translation_handler');
return $form_handler->fileValidateLanguageSuffix($file);
}
/**
* Entity builder callback: Save the mapping.
*
* Save the file_bulkupload_cardinality setting.
*/
function _file_bulkupload_translations_form_field_storage_config_submit($entity_type, FieldStorageConfig $entity, array &$form, FormStateInterface $form_state) {
/** @var FieldStorageConfig $field_storage_config */
$entity->setThirdPartySetting('file_bulkupload_translations', 'cardinality', $form_state->getValue('bulkupload_cardinality_number'));
}
