Hi,
I wonder if and how it would be possible to use the filter for the title of a node. Has anyone wrote a hook or something to get this to work?
Thanks in advance... Tom
| Comment | File | Size | Author |
|---|---|---|---|
| #25 | test.txt | 4 bytes | thakurnishant_06 |
| #16 | widont.zip | 742 bytes | tfranz |
Comments
Comment #1
miklThat is a good suggestion, thank you.
I figure that should be fairly easy to add by implementing the 'view' op of hook_formapi.
Comment #2
TomMynd commentedHmm... maybe I missing something, but which hook do you mean?
Comment #3
TomMynd commentedHi,
some small inserts regarding our short talk in Cologne: Not only the title should be "typogrifyabled" - e.g. content that is coming from CCK fields.
Comment #4
Moonshine commentedI believe mikl meant to say the "view" op of hook_nodeapi. That would let Typogrify jump in and filter the title before output. However titles don't have an input format associated with them so really the Typogrify module would need a little admin form with options for the title specifically for it to make sense. However this should be doable.
CCK text areas should already have the ability to use Typogrify if they are set to use input filters and you have Typogrify enabled on the filter. CCK text fields however are in the same boat as the title where they don't have an associated input filter. So those would be different. Technically Typogrify could offer up a CCK "formatter" as an option for text fields, with settings in the admin also.
I'm certainly not trying to create work here :) but I do think the title feature would be worthwhile! After I wrap up my current project I'll look into adding it and submitting a patch if the maintainer is interested.
Comment #5
miklYeah, moonshine is correct on the nodeapi part – I actually thought of an even better way to do it, namely the preprocess_node theming hook. That should be safer, since the nodeapi approach might cause us to inject HTML in places where it ought not to be (page titles, RSS feeds, etc).
However, I'm unconvinced that this would be a good idea. Typogrify uses some rather complex regular expressions to do its work, and running those on every page view (as would be the case were we to use preprocess hooks) would be a major performance hit. The only reason that Typogrify is usable on body text is that the result of the filtering is stored in the filter cache.
Comment #6
lennart commentedAny progress on title? Widont int title would be useful.
Comment #7
miklGiven that Typogrify intoduces HTML and/or HTML entities to the strings it processes, I think this would have to be to be done directly in the template, or perhaps in a preprocess hook, if it is to be considered safe.
If it's just widont you want, this will do the trick:
Comment #8
lennart commentedThanks!
Comment #9
miklUnless someone can come up with a way we could do this without risking HTML-leakage into JavaScript, titles, attributes, etc., I'm considering this a "won't fix". Doing it on your own theme isn't that hard, and I'd rather not take the chance of doing something that would create a security issue.
Comment #10
jklmnop commentedyou could also create another input filter that only uses Typogrify and give it no roles, then in your theme:
Comment #11
jklmnop commentedOR you can try doing it this way...
Comment #12
plazik commentedI've tryed #10 and #11 and it didn't work for me.
Does anybody have a proper solution?
Comment #13
jklmnop commentedmy fix is specifically for D6. i'm not sure if it's applicable or portable to D7.
here is an example of it in action:
http://i.imgur.com/bHMrUBS.png
http://www.lebow.drexel.edu/news/chairman-ceo-of-sei-investments-co-alfr...
Comment #14
plazik commentedOk, I've found the solution:
Comment #15
brightboldPlazik's comment in #14 worked for me. At first I didn't notice that the page function was a process and not preprocess function, so if you're implementing make sure you type that one correctly.
Comment #16
tfranz commentedPlazik's script works for most of the refinements, but unfortunately not for the "widont"-technique (at least for me).
I think it is because of the restriction to block-tags in line 166 of the file "typogrify.class.php".
(The 'h1'-tag of a title is just a wrapper; the title-text itself should not contain any block-tag like 'p' or 'li' or 'h2').
I attached my little, simple module merging Plazik's script and the widont-technique from Shaun Inman (http://www.shauninman.com/archive/2006/08/22/widont_wordpress_plugin) to solve the problem.
It works independent of Typogrify – just enable it and it should replace the last space in a title with a
"no break space".Comment #17
pere orgaAdding some documentation just in case it's useful to anybody reading this.
This is what I'm doing to add smart quotes to page titles in Drupal 9.1 ("basic_html" is a text format with typogrify enabled):
(In my case, I didn't need to do it for the HTML title tag.)
I also had to apply this transformation to a view field that was outputting the title. To do that, I installed the Twig Tweak module and I overwrote the field result in the view:
{{ title | check_markup('basic_html') }}Edit: There is no need to use an external module, a Twig filter ('typogrify') is already available in this module:
{{ title | typogrify }}Comment #18
rick hood commentedFWIW this is working for me (Drupal 9):
in page.html.twig replace
<h1>{{ node.label }}</h1>with
<h1>{{ title }}</h1>I did not seem to need this
{{ title | typogrify }}Comment #19
rick hood commentedLater I realized that all my plain text fields (in paragraphs mainly) have the same problem as the title.
Maybe I could do a similar fix as for the node titles, but I looked for a JS solution instead and found this: https://smartquotes.js.org/
So I now have this and it seems to work fine:
in the mytheme.libraries.yml
and then in scripts.js this fires near the bottom
smartquotes();Comment #20
lostcarpark commentedI think that @Pere was saying that you can get Typogrify to apply to titles with no PHP code, only the Twig template.
In my case I got it working by editing "page-title.html.twig" and changing:
{{ title }}
to:
{{ title | typogrify }}
And that was it. No hook needed.
The JS solution is pretty neat, though, since that will presumably apply to all text on the page, not just fields you explicitly apply Twig to (and ones covered by your input format).
Comment #21
maenjuel#20 does the trick for nodes, but when I visit /admin/content or a view where I display the node title, I get the following error:
Comment #22
lostcarpark commented@maenjuel, here's what I'm currently using:
Unfortunately, it doesn't typogrify the title on views, but at least it doesn't give errors.
Hoping to find a better solution.
Comment #23
maenjuelAfter posting here, I stumbled upon that solution as well as @lostcarpark. Indeed it does the trick. Thanks for getting back at me and documenting it for other people who might encounter the same problem.
For views, typogrify could be applied in specific views templates, i.e. like this
{{ fields.title.content|typogrify }}Comment #24
lostcarpark commentedI've found another solution for iterable fields.
Prefixing the
typogrifyfilter with therenderfilter forces all of the values to be combined into a single value.It can be used like this:
{{ title | render | typogrify }}Comment #25
thakurnishant_06 commented