Skip to content
Snippets Groups Projects
Commit f4ca2526 authored by Andrey Nuzhdov's avatar Andrey Nuzhdov
Browse files

Added #3318703 code.

parent 9233aa3c
Branches 1.0.x
Tags 1.0.7
No related merge requests found
......@@ -88,6 +88,7 @@ class ModulesInfo extends ContentEntityBase {
->setRequired(TRUE)
->setDescription(t('The module machine name.'))
->addConstraint('UniqueField', [])
->addConstraint('ValidateMachineName', [])
->addPropertyConstraints('value', ['Regex' => ['pattern' => '/^[a-z0-9_]+$/']])
->setDisplayOptions('view', [
'label' => 'above',
......
......@@ -201,4 +201,25 @@ class ModulesInfoService {
return FALSE;
}
/**
* Validates the machine name of module.
*
* @param string $machine_name
* Machine name.
*
* @return true|false
* Returns bolean value
*/
public function validateMachineName(string $machine_name) {
$uri = 'https://www.drupal.org/project/' . $machine_name;
try {
$response = $this->httpClient->get($uri, self::HEADERS);
return TRUE;
}
catch (TransferException $e) {
if ($e->getCode() == 404) {
return FALSE;
}
}
}
}
<?php
namespace Drupal\modules_info\Plugin\Validation\Constraint;
use Symfony\Component\Validator\Constraint;
/**
* Checks if the module machine name exists or not.
*
* @Constraint(
* id = "ValidateMachineName",
* label = @Translation("Validate Machine Name", context = "Validation"),
* type = "string"
* )
*/
class ValidateMachineNameConstraint extends Constraint {
// The message that will be shown if the machine name does not exist.
public $validMachineName = 'Machine name %value does not exist.';
}
<?php
namespace Drupal\modules_info\Plugin\Validation\Constraint;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
use Drupal\modules_info\ModulesInfoService;
/**
* Validates the ValidateMachineName constraint.
*/
class ValidateMachineNameConstraintValidator extends ConstraintValidator implements ContainerInjectionInterface{
/**
* The Module Info Service.
*
* @var \Drupal\modules_info\ModulesInfoService
*/
protected $moduleInfoService;
/**
* Creates a new ValidateMachineNameConstraintValidator instance.
*
* @param \Drupal\modules_info\ModulesInfoService $module_info_service
* The Module Info Service.
*/
public function __construct(ModulesInfoService $module_info_service) {
$this->moduleInfoService = $module_info_service;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('modules_info.service'),
);
}
/**
* {@inheritdoc}
*/
public function validate($items, Constraint $constraint) {
$entity = $items->getEntity();
if (!$this->moduleInfoService->validateMachineName($entity->machine_name->value)) {
$this->context->addViolation($constraint->validMachineName, ['%value' => $entity->machine_name->value]);
}
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment