2

I want to implement it with theme_preprocess function. Every my attempt fails.

function mdrupal_mono_preprocess_page(&$variables) {
  $variables['title'] = drupal_get_title($variables['node']);
  $title = $variables['title'];
  if($title) {
    $words = explode(' ', $title);
    $words[0] = '<span>' . $words[0] . '</span>';
    $title = implode(' ', $words);
  }
}
  • 1
    drupal_get_title doesn't accept parameters – xurshid29 Apr 4 '14 at 8:29
  • If I am sure, then you want to display html markup within title of the node. – Jayendra Kainthola Apr 4 '14 at 8:37
  • yes @JayendraKainthola – Mr. Madamin Apr 4 '14 at 8:43
  • @DalerMadaminov, you can use better approach than this. – Jayendra Kainthola Apr 4 '14 at 8:44
  • and what would that be? @JayendraKainthola – Mr. Madamin Apr 4 '14 at 8:45
1

in my case this is working perfectly in bartik_preprocess_page() (bartik theme):

  $pageTitle = drupal_get_title();
  $pageTitleArr = explode(' ', $pageTitle);
  $firstPart = '<span>' . $pageTitleArr[0] . '</span>';
  $pageTitleArr[0] = $firstPart;
  $title = implode(' ', $pageTitleArr);

  drupal_set_title($title, PASS_THROUGH);
  $variables['title'] = $title;
| improve this answer | |
  • you should also sanitize and strip html tags or unsafe characters (except span tag) in title variable.. – xurshid29 Apr 4 '14 at 13:05
1

Not really a Drupal question, but you're setting the wrong variable

$title = implode(' ', $words);

should be

$variables['title'] = implode(' ', $words);

Or, change

$title = $variables['title'];

to

$title = &$variables['title'];

instead. Either will work

| improve this answer | |
  • yeah that was a mistake, but the code still doesn't work – Mr. Madamin Apr 4 '14 at 8:27
1

You can add some pseudotags like [span] or [i] in your node title and can use below function in your template.php file.

function bb2html($text) {
  $bbcode = array(
                  "[strong]", "[/strong]",
                  "[b]",  "[/b]",
                  "[u]",  "[/u]",
                  "[i]",  "[/i]",
                  "[em]", "[/em]"
                );
  $htmlcode = array(
                "<strong>", "</strong>",
                "<strong>", "</strong>",
                "<u>", "</u>",
                "<em>", "</em>",
                "<em>", "</em>"
              );
  return str_replace($bbcode, $htmlcode, $text);
} 

You need to use <?php print bb2html($head_title); ?> in your page.tpl file.

For more reference, you can check this.

| improve this answer | |

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.