Skip to content

Instantly share code, notes, and snippets.

@chx

chx/Metatag.php Secret

Created November 14, 2018 15:24
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chx/232ba2d2bb32ceb2eef68045d54bb53b to your computer and use it in GitHub Desktop.
Save chx/232ba2d2bb32ceb2eef68045d54bb53b to your computer and use it in GitHub Desktop.
<?php
namespace Drupal\sd8_migration\Plugin\migrate\process;
use Drupal\migrate\MigrateExecutableInterface;
use Drupal\migrate\ProcessPluginBase;
use Drupal\migrate\Row;
/**
* @MigrateProcessPlugin(
* id = "sd8_metatag"
* )
*/
class Metatag extends ProcessPluginBase {
public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) {
if ($value = @unserialize($value)) {
foreach ($value as $name => $data) {
if (!empty($data['value'])) {
$metatag[$this->configuration['map'][$name]] = $data['value'];
}
}
}
return !empty($metatag) ? serialize($metatag) : NULL;
}
}
$tags = \Drupal::service('plugin.manager.metatag.tag')->getDefinitions();
$definition['process']['meta_tags'] = [
'plugin' => 'sd8_metatag',
'source' => 'metatag',
'map' => array_column($tags, 'id', 'name'),
];
/**
* Implements hook_migrate_prepare_row().
*/
function sd8_migration_migrate_prepare_row(Row $row, MigrateSourceInterface $source, MigrationInterface $migration) {
if ($source instanceof Node) {
/** @var \Drupal\Core\Database\Query\SelectInterface $query */
$query = $source->getDatabase()->select('metatag', 'm')
->fields('m', ['data'])
->condition('entity_type', 'node')
->condition('entity_id', $row->getSourceProperty('nid'))
->condition('revision_id', $row->getSourceProperty('vid'))
->condition('language', $row->getSourceProperty('language'));
$row->setSourceProperty('metatag', $query->execute()->fetchField());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment