Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save DanLaufer/b6d28073ceb4d1e8ab32ec4bd1c72e95 to your computer and use it in GitHub Desktop.
Save DanLaufer/b6d28073ceb4d1e8ab32ec4bd1c72e95 to your computer and use it in GitHub Desktop.
Drupal 8 - Programmatically find custom canonical urls and set default
$query = \Drupal::entityQuery('node');
$node_ids = $query
->condition('status', 1)
->exists('field_meta_tags')
->execute();
$count = 0;
foreach($node_ids as $node_id) {
$node = \Drupal\node\Entity\Node::load($node_id);
$metatag = unserialize($node->get('field_meta_tags')->value);
if (strpos($node->get('field_meta_tags')->value,'canonical') > -1) {
print_r('<a href="https://mysite.com/node/'.$node_id.'/edit" target="_blank">https://mysite.com/node/'.$node_id.'/edit</a>'); echo '<br />';
print_r($metatag['canonical_url']); echo '<br /><br />';
unset($metatag['canonical_url']);
$node->set('field_meta_tags', serialize($metatag));
$node->save();
$count++;
}
}
print_r('Fixed: '.$count);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment