1

I'm pretty new to functions and PHP (so please forgive my terminology) but here's what I would like to do:

Background

My homepage contains a view displaying various content type teasers. I would like to truncate the title of an article teaser (a custom teaser created with display suite) to 48 characters.

Action taken so far

If I add the following code to my subtheme's template.php file, nothing happens:

function THEMENAME_preprocess_node(&$variables) {
     if ($variables['type'] == 'article' && $variables['view_mode'] == 'teaser') {
     $variables['title'] = truncate_utf8($variables['title'], 48, TRUE, TRUE);
   }
}

However, if I add this other variation of the code (i.e. applying to all teasers, not just article teasers) the titles of some teasers are indeed shortened:

function eunic_global_preprocess_node(&$variables) {
if ($variables['teaser']) {
    $variables['title'] = truncate_utf8($variables['title'], 48, TRUE, TRUE);
   }
}

Conclusion & Question

I think the problem is to do with the fact that some teasers have been created using the Display Suite module while others use more or less the default teaser.

So my question is - how do I 'refer' to the title of a teaser made with display suite in order to apply the truncate_utf8 function? Fingers crossed there's a way to do it...

Thanks A~

  • I'd highly recommend the Devel Themer module- it helps track down the function/template you need to override. – adam_bear Oct 15 '14 at 18:53
  • Thank you very much for your response - I installed the recommended module and it shows that the template that controls the particular display I'm using is located at: sites/all/modules/ds/layouts/ds_1col/ds-1col--node.tpl.php. However, I don't know how to override the template - the file makes no specific references to title only to $ds_content and $ds_content_wrapper. Would you have any further suggestions? Thanks again. – user25780 Oct 16 '14 at 9:03
0

If you added the title as a field in Display Suite, I'd recommend you look at the field functions of Display Suite.

You may use a Code field, but I'd discourage that.

Rather remove the title from the display, then add the truncated title as a new field in the preprocess function, ie:

$variables['new_title'] = truncate_utf8($variables['title'], 48, TRUE, TRUE);

Then add a new preprocess field to display suite, using the exact same name as the new field, ie:

new_title

You can then place this new field in Display Suite.

| improve this answer | |
  • Thank you very much for your response. So I removed the title from the display, then commented out my previous attempts and added: $variables['content']['new_title'] = truncate_utf8($variables['title'], 48, TRUE, TRUE); and flushed the caches. This gave the following error: "Notice: Undefined variable: variables in include_once() etc.". Ignoring it I then added the custom preprocess field to Display Suite giving the same label i.e. new_title, but it didn't have any effect. Would you be able to tell me where you think I'm going wrong? Thanks again. – user25780 Oct 16 '14 at 8:52
  • Sorry, I made a mistake and changed my answer. The preprocess variable needs to be $variables['new_title'], and not $variables['content']['new_title'] – J. Reynolds Oct 16 '14 at 13:03
  • Thank you so much -- that worked!! I've accepted your answer. My next quest will be to figure out how to apply <h2> formatting to that new variable and link it to the full node. Thanks again, I really appreciate it. – – user25780 Oct 16 '14 at 16:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.