Problem/Motivation
Only external links should be added as managed links. At present a user can enter tel: and mailto: links, in addition to an invalid external link, i.e. http:blah.com. Constraints should be applied to the linky link field to fine tune what is allowed and what is not, in addition to ensuring external links are valid.

Steps to reproduce
Proposed resolution
Provide the user with settings to allow or disallow the use of tel: and mailto: links, and based off of this validate their use through a constraint. If the the link isn't either one of these, then ensure it is external.
Inspiration was taken from linkyreplacer. If this feature is implemented then linkyreplacer can just delegate its checks to the linky entity rather than having to perform its own.
Remaining tasks
User interface changes
API changes
Data model changes
| Comment | File | Size | Author |
|---|---|---|---|
| #21 | linky_constraints.patch | 14.57 KB | mortim07 |
| #17 | linky_constraints.patch | 14.29 KB | mortim07 |
| #12 | linky_constraints.patch | 14.95 KB | mortim07 |
| #11 | linky_constraints.patch | 14.96 KB | mortim07 |
| #8 | linky_constraints.patch | 9.52 KB | mortim07 |
Comments
Comment #2
mortim07 commentedComment #3
mortim07 commentedI'll be adding tests to this patch in the coming days.
Comment #4
mortim07 commentedComment #5
mortim07 commentedComment #6
larowlanLooking good, some cleanup we can do to reduce the complexity of the contraint validator by a fair bit.
In terms of testing, we should be able to test the validation with a Kernel test, which would just leave testing the form.
Here's an example of a kernel test for a validation plugin
In terms of testing the form it would be good to test
a) that the route can't be seen without the permission
b) that the form can be submitted and the new values are saved
Some white space issues here, can you set your IDE settings
Do we need this?
I don't think there's a security implication here, so we can probably drop this
we could use the built in #checkboxes here.
You can still set the #description etc
never seen this used before - what's it for?
👌
this would always be FALSE here right?
this could also include +, ( and -
would it be simpler to just use
parse_url($uri, PHP_URL_SCHEME) === 'tel'and side-step all the myriad of phone numbering schemes across the world - browsers don't ship a defaultpatternattribute for<input type="tel">because its in the too hard basket, so we should toowe can combine these into a single if
at these two points, we can return, because if the url is a tel link, it can't be a mail link or an external link, so we can return early
that means we can also do away with the $validated variable and remove another layer of nesting, and therefore reduce the cyclic complexity
same comment here re parse_url
and re making this a single if
we can return here and avoid the else and the following
if(!$valid_external), again simplifying the code.In my opinion, using else is always a chance to refactor for an early return
Nice work 😎
Comment #7
mortim07 commented@larowlan I've made most of the changes you suggested. I haven't combined the if statements in the validator because if a tel: or mailto: exists we'd like to return. We don't want it progressing towards being validated as an external link.
Comment #8
mortim07 commentedComment #9
mortim07 commented@larowlan I've updated the patch with a kernel test. I've also added install config for the settings.
Comment #10
mortim07 commentedComment #11
mortim07 commentedComment #12
mortim07 commentedFix linting.
Comment #13
mortim07 commentedComment #14
larowlanThis is nearly ready in my book, just some minor cleanup
let's make these booleans
needs a blank line here
there's only one use of this, let's inline it
this doesn't meet our coding standards https://www.drupal.org/node/1354
suggest
'Provides a settings form for linky'
and remove the @package
love the typehinting
I don't think we need this helper method - if we switch these to booleans, simply
$config->set('storage', array_values(array_filter($form_state->get('storage')))will cover thiscould we make these
'Supported additional schemes'
and
'Select additional schemes to support in addition to HTTP and HTTPS'
note to self: this is a single cardinality field, so this is correct
😍 nice tests
Comment #15
mortim07 commented@larowlan Updated the patch with your feedback.
Comment #16
larowlanthese don't match anymore, other than that we're good to go
Comment #17
mortim07 commentedLet's try this again.
Comment #18
larowlanComment #19
larowlanComment #20
jibranPatch LGTM nice work! Just some observations and suggestions.
I think we need this as we can't add constraint in Linky.php as entity level constraints are not supported.
Maybe add a reason why we have to implement an alter hook.
Maybe add a comment why first.
I think 0.uri is fine as the cardinality is 1 here.
Comment #21
mortim07 commentedThanks @ jibran. I've added those comments.
Comment #23
larowlanFixed on commit
This will go out as 8.x-1.0-beta1