Problem/Motivation
Great work on linkit, the 5.x branch is looking great. I think it would be good to abstract out some of the logic that's already in place in the input filter to switch between node and file handling and make this pluggable. I already have two use cases I have encountered that could do with such a feature. These are:
* Direct links to documents managed by a media entity.
* Links to URLs managed by a "managed url" entity.
If we make the substitutions pluggable and weighted, it would support all of these scenarios.
Proposed resolution
Introduce a plugin to achieve this.
Remaining tasks
Validate, patch and test.
User interface changes
None.
API changes
API additions only.
Data model changes
None.
Comments
Comment #2
sam152 commentedHere is my initial port of this. The existing test coverage is quite comprehensive and should already cover the special entity/file handling that is in place.
I also believe this makes the filtered content cachable again, as the string assigned to the file $url does not implement CacheableDependencyInterface and thus would kill the cache.
Comment #4
sam152 commentedCorrect file interface should do it.
Comment #6
anonI really like the idea here, but I'm not sure about the substitution weight and the way the manager is deciding which substitution to use. I might be missing something here but lets say that there are multiple substitutions that is applicable for an entity. How can a user choose which one to use? I think this will be the case for files with file entity.
Comment #7
sam152 commentedMy initial thoughts were that the module would be opinionated and decide what URL is appropriate for what entity. This is the exact same behavior the module has now, we decide file entities should link to a file. Similarly if I installed media_entity or linky (working title for an entity based link solution), as an implementor of this plugin I think it's reasonable to assume that I, the module author is best aware what the 90% use-case is for links to that entity in rich text. File entity could put any kind of logic into their plugin to determine where the best place to link to is based on some criteria.
The weight is to simply allow other plugins to take precedence over the canonical plugin, which acts as a catch-all.
Re: manager, I think it makes sense for the plugin manage to decide which plugin is appropriate for a given entity. I've had success with this kind of pattern in VEF.
Keen to hear your thoughts.
Comment #8
anonIn previews version of Linkit (Linkit 7.x-3.x) users was able to select "url_type" for the file entity url.
Maybe this is a corner case, but when dealing with file entities, there is no 90% use case as I see it, based on these three options.
However, we could make this a matcher setting.
All matchers is bound to a specific entity type, and we could therefor list available substitution plugins to choose from for each matcher plugin instance setting form. Basically use the SubstitutionManager::getPluginFromEntity() but return a List<Substitution> instead of a single Substitution.
I see your point here, but are there any disadvantages of letting the users decide this themselves?
Comment #9
sam152 commentedSounds like a plan. I can have a look at this.
Comment #10
sam152 commentedHere is an approach which allows configurable substitution plugins.
Comment #12
sam152 commentedAdded schema.
Comment #14
sam152 commentedSome more test fixes.
Comment #15
anonAwesome. I will take a look at this over the weekend.
Comment #16
anonThanks a lot Sam for all your work, I really appreciate this, and I really what this in the 5.x release. I just wanna make sure this is solid. I have done some mistakes in the previous versions that was very tricky to solve after the release. That is why I'm kind of picky with this.
I like the approach in the #14 patch. There are some minor coding standard issues,
but nothing big.This update hook needs a test.
I'm not sure this will work 100%.
The API only uses the \Drupal::configFactory(); to load configuration.
https://www.drupal.org/node/2535454
Make sure to set $has_trusted_data to TRUE when you save configuration, so that the schema will not be checked. The reason this is important is that a later update might update the config schema, and you don't know when running updates if someone delayed and ran this update later when the schema was different.
One thing though that crossed my mind is that now we kind of force a lot of values into the href field in the dialog. It is entity type, entity id, and substitution id, and I feel like this bloat the field to mush. Also, the settings page for the matchers is already kind of bloated. Maybe a redesign should be made here. However, these should be separate issues. Just to be clear, there is nothing wrong with the patch, this is just general thoughts.
Summary:
Then I think we are ready to commit.
Comment #17
anonHere is a patch that
Comment #18
anonThe coding standard report still gives one error.
Core does have this issue as well, but I would like to see if we can remove it from Linkit.
Comment #19
sam152 commentedThanks for the speedy review. Notes:
Comment #20
sam152 commentedBeat me to it! Looks like we took slightly different approaches. Happy to defer to your judgement.
Comment #21
anonAs said on IRC, I'm not that familiar with the 3 way merge.
Here is my comments, I have used my patch as the "base".
Mine adds a newline. Think we should use mine here.
Not sure these are needed now. If not, they should be removed. I have removed them in mine.
This is better then mine. We should use this.
This is better then mine. We should use this.
I have removed this from mine. This method is no longer used.
New: Rename this to $entity_type_id. Same for the interface and documentation.
Comment #22
sam152 commentedSounds like a plan.
Comment #23
larowlanThis looks great, couple of minor observations
whitespace issues
We code hide this if
count($options) === 1using#accessComment #24
anon@larowlan: 2. Good point.
@Sam152: Will you be able to do the 3 way merge?
Comment #25
sam152 commentedYeah, I'll get a patch out that includes the changes you suggest.
Comment #26
sam152 commentedAttached is your branch, with the changes in #26 and #23.
The only reason I wouldn't include the access check is to help people discover that the substitutions themselves are actually pluggable. Perhaps with the UI element, it might spark some ideas or use cases from the users of the module? It's in there for now though.
Comment #27
anonAwesome! I made some small fixes. (New lines and doc)
Comment #29
anonwoops wrong patch file.
Comment #30
anonComment #33
anonAnother try
Comment #35
anonComment #36
sam152 commentedThanks for getting this in. Was great collaborating.