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

CommentFileSizeAuthor
#25 test.txt4 bytesthakurnishant_06
#16 widont.zip742 bytestfranz

Comments

mikl’s picture

That is a good suggestion, thank you.

I figure that should be fairly easy to add by implementing the 'view' op of hook_formapi.

TomMynd’s picture

Hmm... maybe I missing something, but which hook do you mean?

TomMynd’s picture

Hi,

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.

Moonshine’s picture

I 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.

mikl’s picture

Yeah, 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.

lennart’s picture

Any progress on title? Widont int title would be useful.

mikl’s picture

Version: 6.x-1.0-beta1 » 6.x-1.x-dev

Given 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:

module_load_include('class.php', 'typogrify');
$text = Typogrify::widont($text);
lennart’s picture

Thanks!

mikl’s picture

Status: Active » Closed (won't fix)

Unless 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.

jklmnop’s picture

you could also create another input filter that only uses Typogrify and give it no roles, then in your theme:

function HOOK_preprocess_page(&$variables) {  
  $variables['title'] = check_markup($variables['title'], [the id of your input filter], false); 
}
jklmnop’s picture

OR you can try doing it this way...

function HOOK_settings_preprocess_page(&$variables) {  
  if(!function_exists('smarty_modifier_smartypants')) {
    module_load_include('php', 'typogrify', 'smartypants');
    $variables['title'] = smarty_modifier_smartypants($variables['title'], 'qdew');
  }    
}
plazik’s picture

Version: 6.x-1.x-dev » 7.x-1.x-dev

I've tryed #10 and #11 and it didn't work for me.
Does anybody have a proper solution?

jklmnop’s picture

my 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...

plazik’s picture

Ok, I've found the solution:

function HOOK_preprocess_node(&$variables) {
  $variables['title'] = check_markup($variables['title'], 'the id of your input filter');
}

function HOOK_process_page(&$variables) { 
  $variables['title'] = check_markup($variables['title'], 'the id of your input filter');
}

function HOOK_preprocess_html(&$variables) {
  $variables['head_title'] = check_markup($variables['head_title'], 'the id of your input filter');
}
brightbold’s picture

Issue summary: View changes

Plazik'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.

tfranz’s picture

StatusFileSize
new742 bytes

Plazik'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".

pere orga’s picture

Title: Typogrify in title also? » Typogrify in title
Version: 7.x-1.x-dev » 8.x-1.x-dev

Adding 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):

/**
 * Implements hook_preprocess_HOOK().
 */
function custom_module_preprocess_page_title(&$variables) {

  // Apply smart quotes.
  $variables['title'] = check_markup($variables['title'], 'basic_html');
}

(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 }}

rick hood’s picture

FWIW this is working for me (Drupal 9):

function hook_preprocess_page(&$variables) {
  $variables['title'] = check_markup($variables['page']['#title'], 'basic_html');
}

in page.html.twig replace
<h1>{{ node.label }}</h1>
with
<h1>{{ title }}</h1>

I did not seem to need this
{{ title | typogrify }}

rick hood’s picture

Later 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

global-scripts:
  js:
    libraries/modernizr/modernizr.js: {}
    js/smartquotes.js: {}
    js/scripts.js: {}

and then in scripts.js this fires near the bottom
smartquotes();

lostcarpark’s picture

I 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).

maenjuel’s picture

#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:

AH01071: Got error 'PHP message: TypeError: preg_split(): Argument #2 ($subject) must be of type string, array given in [drupal root]/web/modules/contrib/typogrify/src/SmartyPants.php on line 1343 #0 [drupal root]/web/modules/contrib/typogrify/src/SmartyPants.php(1343): preg_split()\n#1 [drupal root]/web/modules/contrib/typogrify/src/SmartyPants.php(269): Drupal\\typogrify\\SmartyPants::tokenizeHtml()\n#2 [drupal root]/web/modules/contrib/typogrify/src/Typogrify.php(218): Drupal\\typogrify\\SmartyPants::process()\n#3 [drupal root]/web/modules/contrib/typogrify/src/TwigExtension/Typogrify.php(39): Drupal\\typogrify\\Typogrify::filter()\n#4 [drupal root]/web/sites/default/files/php/twig/63f28b52e004b_page-title.html.twig_nHmMQSGyX8r5q48osUipCs1t0/ho1qwEkSF0cd_ek5utcVkhFFL_JTTl_-qPCPhjoH3uU.php(53): Drupal\\typogrify\\TwigExtension\\Typogrify::filter()\n#5 /var/custome...', referer: [page I'm coming from]
lostcarpark’s picture

@maenjuel, here's what I'm currently using:

    {% if title is iterable %}
      {{ title }}
    {% else %}
      {{ title | typogrify }}
    {% endif %}

Unfortunately, it doesn't typogrify the title on views, but at least it doesn't give errors.

Hoping to find a better solution.

maenjuel’s picture

After 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 }}

lostcarpark’s picture

I've found another solution for iterable fields.

Prefixing the typogrify filter with the render filter forces all of the values to be combined into a single value.

It can be used like this: {{ title | render | typogrify }}

thakurnishant_06’s picture

StatusFileSize
new4 bytes