Hi all,
I use glossify on multiple Drupal websites at my job. A while ago my team on one of these sites requested a feature to add synonyms to our site's glossary, and asked that a synonym be glossifiable without need for creating a whole new entry in the glossary. They asked that the node title be considered the core term, parent term, however you'd like to call it, and that that node have a list of strings that could be used interchangeably with the parent term. Whichever term first appears in an article, either parent or synonym, that should be the only instance of that term to be glossified.
For example: A hypothetical content type named Glossary Item has 3 fields - Title (string), Body (string), Synonyms (array(string)). On my example website, I have a node of content type Glossary Item with the title set as "President of the United States". There is one string object in the Synonyms array, and that Synonym string is set as "POTUS".
With my patch, both "President of the United States" and "POTUS" are candidates to be glossified. Since my sites require only the first found match be glossified, if one term is glossified then the other will not be, regardless of which one occurs first.
One of my projects just requested I port that feature to the new site, so I took the time to go in, clean it up a bit, and make it glossify-2.0.1,compatible and I thought it would be worthwhile to post on Drupal.org in case any other developers might find this useful. A few things to note:
- For the feature to work, the synonyms field MUST have the machine name `field_synonym`.
- I added an update hook to glossify.install to auto-generate that field, but in order for the field to be auto-generated, there MUST exist a content type with the word "glossary" in its machine name. I could not figure out how to make this configurable in the time I had to port and improve the feature, but maybe someone else can.
- I did not go in and test how this feature works when glossifying ALL instances of a term. Both of my projects' requirements state that we only glossify the first instance of any term on a page, so I didn't take the time (read: use our budget) to test that aspect. Rest assured it works fine if you only want to glossify the first instance of a term or its synonym.
- The patchfile also includes a statement to force glossify to ignore th and h1-6 header tags.
- This patchfile also goes in and updates the to-be-deprecated calls to render() with \Drupal::service('renderer')->render()
This patchfile is really meant to serve more as a guide than as a plug and play feature, but I thought it would be useful nonetheless.
| Comment | File | Size | Author |
|---|---|---|---|
| #8 | synonym.png | 190.49 KB | yospyn |
Issue fork glossify-3269949
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
Comments
Comment #2
JakeAPoe commentedComment #3
avpadernoComment #4
tgoeg commentedThis would be a pretty neat feature. Thanks for your work!
Just thinking aloud: Wouldn't be using the taxonomy term's hierarchy feature be a lighter solution that uses more of Drupal core's available functionality so a hard coded field name for the synonym wouldn't be necessary?
This would also solve the second bullet point in the list above as the field would not have to be created anyway.
I.e., to stay with the OP's example, define the main taxonomy term "President of the United States" and, as a child below it, define "POTUS" and just see whether there is a parent term and link to it instead of directly to the (child) term when processing terms/adding links in the content pages? Something along the lines of
create_link(term.parent ? term.parent.url : term.url);?Or may this even be solvable in twig like outlined in https://www.drupal.org/forum/support/theme-development/2017-04-19/get-th... ?
Comment #5
jmester13 commented@tgoeg hoping you might be able to point me in the right direction here. I'm working to continue @JakeAPoe's work and finding the following situation tricky.
If you have glossary terms like denominator and denominator exclusion used at the same time only denominator is picked up.
For example "The denominator for this things was X, and the denominator exclusion was Z"
Only the first denominator gets a tooltip. The second term denominator exclusion is completely ignored.
Comment #6
tgoeg commentedSorry, I don't have any synonym functionality implemented here, yet.
This was more of a design consideration, as the original patch seems a little too off from core functionality for me.
If you're talking about a general implementation option/algorithm ideas and how this could be solved: That's a little tricky indeed.
I'd probably first build some kind of decision tree to see whether one tag is contained in another one, and if so, search for the "bigger" term first and then replace the smaller ones, excluding the ones already found. Could be expensive however.
Searching forward for every hit if there is some bigger hit might also be costly.
Another option would probably be a sliding window of the size the maximum length of a term (word count of longest term) is.
So if the longest term is 3 words long, look at the first 3 words and see if it matches, if not, look at words 2+3+4 and see if they match, etc.
Whenever a shorter term is found in the window, remember it and only replace it if the sliding window has passed and nothing longer could be found.
I think this would fare better as it scales with the actual length needed. In the simplest case of only 1 word long terms, it should not cause any additional penalty. And it should also perform better on longer texts with different markup (that has to be rendered first) than iterating over the whole node times.
Sorry if you wanted to hear something completely different, but at least the ideas are written down, now ;-)
(There may be numerous other options - I am not a developer, just a sysadmin with the occasional bash coding need)
Comment #7
jmester13 commentedThanks for the quick reply @tgoeg. I've poked around a bit already and it's certainly a tricky one. I'll take your thoughts into consideration as I'm working through things. Always good to have ideas written down.
Comment #8
yospyn commentedResurfacing this feature request for v3 if possible. Very similar need (see screenshot), except I'm using glossify_taxonomy instead of node:
- Have a term such as President of the United States
- Create field_acronym plain text field for same taxonomy, then enter POTUS into that field
- Glossify would treat either the name or field_synonym equally, applying to whichever appears first on a page
Great module! We're using it with the book module as a glossary of terms, which display as pop-ups on hover.
Comment #9
diaodiallo commentedHello, I am sharing a patch I refactored for my colleague @yospyn.
As mentioned @yospyn we have synonyms in a term field which need to be called 'field_synonym' (Just add this field and your synonyms to use the patch).
Thanks to @JakeAPoe for the patch I used it for this version 3 patch.
Comment #10
anybodyI'd like this very much. I think it should allow to select a certain field with a list of synonyms from the source entity.
Please use MRs instead of patches!
Comment #11
anybodyRaising the priority and hope someone finds the time for the implementation!
Comment #17
philyWe might need a wider entity type support: in the project I would have the use of synonyms, the glossary definitions are nodes.
Can someone have a look at the Synonyms module so efforts could be joined as a sub-module either for Glossify or Synonyms.
Comment #18
tgoeg commentedSecond that.
There might be glossary terms/explanations that are more complex than what just a taxonomy term allows for.
Comment #19
joao.ramos.costa commentedHi @phily,
In fact, in the project where I apply the patch from this MR, we're using synonyms module, but only because synonyms field matches the one used here.
I think you're right — and when I have some time, I’ll take a look. I know that using the service provided by the module won’t work, since we’re grouping the synonyms differently, but maybe it’s possible to make the query a bit more complex, not sure. Need a proper analysis .
Thank you
Comment #20
anybodyI still think this would be a really helpful feature, also for a different case: If an entity (for some good reasons) has a different title, than it should represent as glossary term.
Typically you'd see synonyms as a list of additional terms, but it might also be just one "Glossary" synonym that represents the entity in texts, for (stupid) example: "Imprint". Maybe the Imprint needs to be named "Our imprint", but should appear linked on "Imprint" everywhere... So even if it's only a single term on certain nodes and not a lst, this would be great. I'd then call it "alias"
Comment #23
grevil commentedWe were in need of such a feature for a few customers, so I took the time to finish this. It works similar to the approach by @joao.ramos.costa, I appreciate the work and urge to give him credit, once this is reviewed and merged.
You basically have to create a Text (plain, long) field called "field_glossify_synonyms" on the bundle you wish to enable synonyms on. Then when you create a bundle, for example "Technology" you can define "Tech" and "IT" as their synonyms in the "field_glossify_synonyms" text field.
Now when you type either Technology, Tech or IT, all of these terms will be glossified and link to the main node (if link is enabled).
We can improve on this in the feature through adding support for "Synonyms" in the future. I haven't used the module yet, and I am unsure whether it would add much overhead.
Comment #26
anybodyGREAT WORK @grevil! I left some finale comments (okay, some work left TBH... ;)) but I think then this will be a very helpful, fabulous feature.
Would be great, if the others here could also give it a try and review. Thanks!!
Comment #27
grevil commentedAlright, all done, please review finally @anybody!
Comment #28
anybody@grevil: Please see and fix the final comments. After the final review, let's wait a bit with a tagged release to give us all a chance to test this for a while.
Comment #29
grevil commentedThat should be it! Please review!
Comment #30
anybodyGreat @grevil! All resolved and green :)
RTBC! As written above, let's wait with a release for some time...
Comment #32
anybodyComment #34
joao.ramos.costa commentedDear @anybody would it be possible to complete the credits to reflect the various contributions ?
Not asking just for myself, but as a reminder that contributing takes time, helps improve things, and recognition really supports ongoing collaboration! Thanks!
Cheers!
Comment #35
anybody@joao.ramos.costa definitely yes! No idea why that didn't happen. Typically, I do that... Some days ago the functionality was broken for me, perhaps that caused it.
Comment #36
joao.ramos.costa commentedThank you! <3