0

We have a site with numerous paragraphs, one of which is a Call-to-action, comprised of a title, summary and a link field (machine name: field_link).

The CTA is used so much that to change the word 'Enquire' to 'Inquire' for when we are on a US translation (en-US in url) would require the paragraph been edited manually for every content page that has a us translation would be a monumental task.

As such I've decide to attempt to simply handle this in the paragraph preprocess hook. The original idea was simply to check if we were in a us translation, then check for the word in the field_linl title and change it if needed, as per my snippet below.

/* Translation modifier:
   * The call to action paragraph is used on almost all pages and translations
   * doesn't work with paragraphs at all. So we need to detect if we are in a US
   * translation and change out the spelling of certain words.
   */
  if($paragraph_type == 'call_to_action'){

    if(is_us_translation()){
      $cta_link = $paragraph->get('field_link');
      $cta_link->list[0]->values['title'] = 'Inquire';
      $paragraph->set('field_link', $cta_link);

      // $paragraph->values['field_link']['x-default'][0]['title'] = 'Inquire';
    }
  }

The commented out line was my other attempt.

My problem though is nothing I have tried seems to manage to change the title of the field. Is there a specific way to change this?

0

This worked for me

function MYTHEME_preprocess_field(&$variables, $hook){
  $element = $variables['element'];

  /* Translation modifier:
   * The call to action paragraph is used on almost all pages and translations
   * doesn't work with paragraphs at all. So we need to detect if we are in a US
   * translation and change out the spelling of certain words.
   */
  if(is_us_translation()){
    if (isset($element['#field_name'])) {
      if ($element['#field_name'] == 'field_link') {
        $str_to_check = strtolower(trim($variables['items'][0]['content']['#title']));

        if(strpos($str_to_check, 'enquire') !== false){
          $variables['items'][0]['content']['#title'] = 'Inquire';
        }
      }
    }
  }
}

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.