Last active
October 19, 2018 11:53
-
-
Save DanLaufer/b6d28073ceb4d1e8ab32ec4bd1c72e95 to your computer and use it in GitHub Desktop.
Drupal 8 - Programmatically find custom canonical urls and set default
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$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