0

I have a client with a D8 site who wants curly quotes and apostrophes on all rendered content. I was able to do this for the page titles with this preprocess function:

function mytheme_preprocess_page_title(&$variables) {
// Load the node entity from the current route
if ($node = \Drupal::request()->get('node')) {

    // Get the title
    $title = $node->getTitle();

    // Replace left quotes
    $title = preg_replace('/"(?=\w)/', '“', $title);

    // Replace right quotes
    $title = preg_replace('/(?<=\w)"/', '&rdquo;', $title);

    // Replace single quote/apostrophe
    $title = str_replace("'", '&rsquo;', $title);

    // Return the title variable
    $variables['title'] = t($title);
}
}

Is there a way to do this for the body field as well? hook_preprocess_field isn't getting me anywhere.

1 Answer 1

0

Instead of using a preprocess function, you could accomplish the desired behavior using CSS.

Using the ::before and ::after selectors you can insert additional content. This content could be the textual " character (i.e., content: '"';) or you could display it as an (inline-)block, provide width, height, margin and padding and set a background image.

For example:

.field--name-body {
  padding-left: 1em;
  padding-right: 1em;
}

.field--name-body::before {
  content: '"';
  position: absolute;
  top: 0;
  left: 0;
}

.field--name-body::after {
  content: '"';
  position: absolute;
  right: 0;
  bottom: 0;
}

This creates some space around the body field and provides " characters at the top left and bottom right location.

1
  • I think I might not have been clear in my initial post. I need to replace plain text quotation marks in the body text with smart quotes: &rdquo; and &ldquo;, and straight apostrophes need to be replaced with &rsquo;. Your solution will only add plain quotation marks around the entire body field.
    – JOakland
    Mar 2, 2018 at 1:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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