diff --git a/src/Entity/ModulesInfo.php b/src/Entity/ModulesInfo.php index 203254303e0e4d5c1a6444d4605ddcad31bf19fd..9b3008ccfe01acce0dda62e8f54b20227f36e38b 100644 --- a/src/Entity/ModulesInfo.php +++ b/src/Entity/ModulesInfo.php @@ -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', diff --git a/src/ModulesInfoService.php b/src/ModulesInfoService.php index 8a5c2effd8b368ce57f21059c9440c95209a7d7a..1e18f4948d05ee4f21e3b87747ded58b25d7379e 100644 --- a/src/ModulesInfoService.php +++ b/src/ModulesInfoService.php @@ -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; + } + } + } } diff --git a/src/Plugin/Validation/Constraint/ValidateMachineNameConstraint.php b/src/Plugin/Validation/Constraint/ValidateMachineNameConstraint.php new file mode 100644 index 0000000000000000000000000000000000000000..ad83e09559afded6094754b5ba2fcad5b9d8820f --- /dev/null +++ b/src/Plugin/Validation/Constraint/ValidateMachineNameConstraint.php @@ -0,0 +1,21 @@ +<?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.'; + +} diff --git a/src/Plugin/Validation/Constraint/ValidateMachineNameConstraintValidator.php b/src/Plugin/Validation/Constraint/ValidateMachineNameConstraintValidator.php new file mode 100644 index 0000000000000000000000000000000000000000..c50a617d984152fb8d5f3848ee6d8550a4451982 --- /dev/null +++ b/src/Plugin/Validation/Constraint/ValidateMachineNameConstraintValidator.php @@ -0,0 +1,50 @@ +<?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]); + } + } +}