Sometimes during the development of a site, you need a value of a field, node title, or taxonomy term name to be unique and we had to add custom validations.

This module allows you to require that the content supplied for entity fields, node titles or taxonomy terms names will unique if so configured in each field or title/name of the entity, allowing the administrator to set a validation message in case the filled-in value already exists.

Project link

https://www.drupal.org/project/unique_content_field_validation

Git instructions

git clone --branch 1.0.x https://git.drupalcode.org/project/unique_content_field_validation.git

PAReview checklist

http://pareview.net/r/346

Comments

fabiansierra5191 created an issue. See original summary.

fabiansierra5191’s picture

Status: Active » Needs review
avpaderno’s picture

Title: Unique content field validation » [D8] Unique content field validation
Issue summary: View changes
Issue tags: +PAreview: project created less than ten days ago
avpaderno’s picture

Priority: Normal » Critical
Issue tags: -PAreview: project created less than ten days ago
drupal-ramesh’s picture

Hi
Kindly fix the below errors

FOUND 0 ERRORS AND 2 WARNINGS AFFECTING 2 LINES
--------------------------------------------------------------------------
259 | WARNING | Only string literals should be passed to t() where possible
285 | WARNING | Only string literals should be passed to t() where possible

avpaderno’s picture

Priority: Critical » Normal
Status: Needs review » Needs work
avpaderno’s picture

Priority: Normal » Minor
vipin.mittal18’s picture

Assigned: Unassigned » vipin.mittal18
Issue tags: +WiproContributions2021
vipin.mittal18’s picture

Assigned: vipin.mittal18 » Unassigned
Status: Needs work » Needs review
StatusFileSize
new68 bytes

Hello Fabian Sierra,

I have resolved the issue of literal strings and attached the patch over here. I have also installed the module and verified after code fixes.

Kindly have a look and implement the patch. We have pipeline in project which is getting affected due to this issue. So please apply patch ASAP as per your convenience.

Thanks
Vipin Mittal

poorva’s picture

Status: Needs review » Reviewed & tested by the community

RTBC +1

avpaderno’s picture

Status: Reviewed & tested by the community » Needs review
avpaderno’s picture

Status: Needs review » Needs work

Actually, until the user who applied doesn't change the project files as reported, the status is still Needs work. The task of reviewers isn't providing patches, but reporting what needs to be changed.

avpaderno’s picture

avpaderno’s picture

      $message_item = !empty($element['#unique_field_settings']['message']) ? $element['#unique_field_settings']['message'] : '%label "%value" must be unique.';
      $items_msg = implode(',', $items);
      $message = t($message_item, ['%label' => $field_label, '%value' => $items_msg]);

Passing a variable as first argument of t() is considered a possible security issue.

vipin.mittal18’s picture

Hello Fabian,

Could you please apply the patch of https://www.drupal.org/project/unique_content_field_validation/issues/32... to resolve the issue and make it security coverage of your module.

Thanks
Vipin

fabiansierra5191’s picture

Priority: Minor » Normal
Status: Needs work » Needs review

Hi guys,

Thanks for your feedback and help, I launched a new version 1.0.3 to fix the possible security issue.

Thanks!

vipin.mittal18’s picture

Issue summary: View changes

Added PAReview checklist in the issue description.

avpaderno’s picture

Status: Needs review » Needs work
  • What follows is a quick review of the project; it doesn't mean to be complete
  • For every point, I didn't make a complete list of the lines that should be changed, but I made an example of what needs to be changed
  • The review points report what should be changed to follow the Drupal coding standards, correctly use the Drupal API, or avoid any (possible) security issue; if a review point isn't about one of those topics, the point makes that clear
  private function uniqueValidation($field_name, $value, $entity_type, $bundle_field, $entity_bundle, $id_field) {
    if ($entity_type && $value && $field_name && $bundle_field && $entity_bundle) {
      $query = \Drupal::entityQuery($entity_type)
        ->condition($field_name, $value)
        ->condition($bundle_field, $entity_bundle)
        ->range(0, 1);
      // Exclude the current entity.
      if (!empty($id = $this->context->getRoot()->getEntity()->id())) {
        $query->condition($id_field, $id, '!=');
      }
      $entities = $query->execute();
      if (!empty($entities)) {
        return TRUE;
      }
    }
    return FALSE;
  }

When a validator constraint plugin needs to use dependencies, it needs to implement ContainerInjectionInterface, similarly to the following Drupal core plugin.

class ValidPathConstraintValidator extends ConstraintValidator implements ContainerInjectionInterface {

  /**
   * The path validator.
   *
   * @var \Drupal\Core\Path\PathValidatorInterface
   */
  protected $pathValidator;

  /**
   * Creates a new ValidPathConstraintValidator instance.
   *
   * @param \Drupal\Core\Path\PathValidatorInterface $path_validator
   *   The path validator.
   */
  public function __construct(PathValidatorInterface $path_validator) {
    $this->pathValidator = $path_validator;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static($container->get('path.validator'));
  }

  /**
   * {@inheritdoc}
   */
  public function validate($value, Constraint $constraint) {
    if (!isset($value)) {
      return;
    }
    $path = trim($value, '/');
    if (!$this->pathValidator->isValid($path)) {
      $this->context->addViolation($constraint->message, [
        '%link_path' => $value,
      ]);
    }
  }

}
        $form_state->setErrorByName($field_name, t('%message', [
          '%message' => $message,
        ]));

A string that only contains a placeholder cannot be translated, as a placeholder is the same for every language. A string that only contains two or more placeholders could be translated, as in that case the translation could at least change the placeholders order.

fabiansierra5191’s picture

thanks Alberto for all your comments, I haven't seen the errors with translations, and I thought that patch in the bug ticket #3223100 fixed that but unfortunately it did not, and instead create a new bug.

After a small research, I found that errors reported in comment #5 are not necessarily errors that need to be fixed, the message says where is possible, and in this case to keep the translation is not fixable.

I have undone this and fix the dependency injection items that need, it's not in the last release, for now, it is only on the branch 1.0.x

fabiansierra5191’s picture

Status: Needs work » Needs review
avpaderno’s picture

Assigned: Unassigned » avpaderno
Status: Needs review » Fixed

Thank you for your contribution! I am going to update your account.

These are some recommended readings to help with excellent maintainership:

You can find more contributors chatting on the IRC #drupal-contribute channel. So, come hang out and stay involved.
Thank you, also, for your patience with the review process.
Anyone is welcome to participate in the review process. Please consider reviewing other projects that are pending review. I encourage you to learn more about that process and join the group of reviewers.

I thank all the dedicated reviewers as well.

avpaderno’s picture

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.