file_bulkupload_translations-1.0.x-dev/src/Plugin/Validation/Constraint/EnabledLanguageConstraintValidator.php
src/Plugin/Validation/Constraint/EnabledLanguageConstraintValidator.php
<?php
declare(strict_types=1);
namespace Drupal\file_bulkupload_translations\Plugin\Validation\Constraint;
use Drupal\Core\Language\LanguageManagerInterface;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
use Drupal\file\FileInterface;
use Drupal\Core\Config\ConfigFactoryInterface;
/**
* Validates the language suffix, prefix, or location in a file name based on configuration.
*/
class EnabledLanguageConstraintValidator extends ConstraintValidator {
/**
* The language manager.
*
* @var \Drupal\Core\Language\LanguageManagerInterface
*/
protected $languageManager;
/**
* The config factory service.
*
* @var \Drupal\Core\Config\ConfigFactoryInterface
*/
protected $configFactory;
/**
* Constructs a new EnabledLanguageConstraintValidator.
*
* @param \Drupal\Core\Language\LanguageManagerInterface $languageManager
* The language manager service.
* @param \Drupal\Core\Config\ConfigFactoryInterface $configFactory
* The config factory service.
*/
public function __construct(LanguageManagerInterface $languageManager, ConfigFactoryInterface $configFactory) {
$this->languageManager = $languageManager;
$this->configFactory = $configFactory;
}
/**
* {@inheritdoc}
*/
public function validate(mixed $value, Constraint $constraint) {
if (!$constraint instanceof EnabledLanguageConstraint) {
throw new \Symfony\Component\Validator\Exception\UnexpectedTypeException($constraint, EnabledLanguageConstraint::class);
}
/** @var \Drupal\file\FileInterface $value */
if (!$value instanceof FileInterface) {
return;
}
$language_filename = $this->getLanguageFromFilename($value->getFilename());
//ddl($language_filename);
// Check the language_filename validity.
if (!in_array($language_filename, $this->getEnabledLanguageCodes(), true)) {
$this->context->addViolation($constraint::INVALID_LANGUAGE, ['%language_filename' => $language_filename]);
return;
}
}
/**
* Extract the language from the filename based on a configurable regex.
*
* @param string $filename
* The filename to extract the language from.
*
* @return string
* The extracted language suffix or prefix.
*/
private function getLanguageFromFilename(string $filename): string {
// Fetch the regex configuration.
$config = $this->configFactory->get('file_bulkupload_translations.settings');
$regex = $config->get('language_regex') ?: '/[_-]([a-zA-Z]{2})(_\d+)?\.[a-zA-Z]+$/i';
// Apply the regex to extract the language.
if (preg_match($regex, str_replace(' ', '', $filename), $match)) {
return mb_strtolower($match[1]);
}
// Default to the default language.
return $this->languageManager->getDefaultLanguage()->getId();
}
/**
* Get a list of enabled language codes.
*
* @return array
* An array of enabled language codes.
*/
private function getEnabledLanguageCodes(): array {
return array_keys($this->languageManager->getLanguages());
}
}
