Skip to content

Instantly share code, notes, and snippets.

@ericjgruber
Created January 7, 2019 19:19
Show Gist options
  • Save ericjgruber/a0ada4bf2c98bcb39ab0e0a48866b7a9 to your computer and use it in GitHub Desktop.
Save ericjgruber/a0ada4bf2c98bcb39ab0e0a48866b7a9 to your computer and use it in GitHub Desktop.
Trim url from a Drupal preprocess field
/**
* Implements hook_preprocess_field().
* @param $variables
*/
function THEMENAME_preprocess_field(&$variables) {
$node = \Drupal::routeMatch()->getParameter('node');
if ($node->hasField('field_links_social')) {
$twitter = $node->get('field_links_social')->getValue();
$twitterURL = $twitter[0]["uri"];
$regex = "@(https?://twitter.com/)@";
$trimmedTwitter = preg_replace($regex, '', $twitterURL);
$variables['twitter'] = $trimmedTwitter;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment