0

We use a custom constraint on a custom entity type. In this entity type we have a string field "url basis" which is used to create aliases for many kinds of pages and data related to this entity.

Therefore it is important that this url basis is unique with every single entity. Here is the constraint:

class UniqueParliamentBasisUrl extends Constraint {
  public $notUniqueURLBasis = 'The basis URL needs to be unqiue but the set basis URL %basisurl is already in use: @parliament_link';
}

In UniqueParliamentBasisUrlValidator->validate() I try the following:

    $link = $parliament_using_basis_url->toLink($parliament_using_basis_url->label(), 'edit-form', ['attributes' => ['target' => '_blank']])->toString();
    $this->context->addViolation($constraint->notUniqueURLBasis, ['%basisurl' => $item->value, '@parliament_link' => $link]);

Here $link becomes the HTML markup for a link so that the user can easily switch to the entity which already uses this url basis in order to edit it. Unfortunately the markup becomes escaped so that the user does not see a clickable link on the form but the markup:

The basis URL needs to be unqiue but the set basis URL bundestag is already in use: <a href="/admin/parliament-watch/parliaments/5/edit" target="_blank" hreflang="und">Bundestag</a>

Is there a way to add markup to the violation message without being escaped?

3

You shouldn't chain up placeholders / use HTML inside placeholders. And for URLs use the :placeholder notation with double dots

 $notUniqueURLBasis = 'The basis URL needs to be unqiue but the set basis URL %basisurl is already in use: <a href=":existing_url">@link_text</a>';

See https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Component%21Render%21FormattableMarkup.php/class/FormattableMarkup/8.2.x

and

https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Component%21Render%21FormattableMarkup.php/function/FormattableMarkup%3A%3AplaceholderFormat/8.2.x

| improve this answer | |
  • Thank you, this solved my problem! – Tobias Krause Sep 11 at 13:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.