Last active
August 29, 2015 14:20
-
-
Save chx/7f6baf855dd1f8be5f21 to your computer and use it in GitHub Desktop.
States to taxonomy IDs
This file contains 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
<?php | |
/** | |
* @file | |
* Contains \Drupal\migrate_plus\Plugin\migrate\process\TermReference. | |
*/ | |
namespace Drupal\migrate_plus\Plugin\migrate\process; | |
use Drupal\taxonomy\TermStorageInterface; | |
use Symfony\Component\DependencyInjection\ContainerInterface; | |
use Drupal\Core\Plugin\ContainerFactoryPluginInterface; | |
use Drupal\migrate\Entity\MigrationInterface; | |
use Drupal\migrate\ProcessPluginBase; | |
use Drupal\migrate\MigrateExecutableInterface; | |
use Drupal\migrate\Row; | |
/** | |
* Looks up a taxonomy term by title. | |
* | |
* @MigrateProcessPlugin( | |
* id = "term_reference" | |
* ) | |
*/ | |
class TermReference extends ProcessPluginBase implements ContainerFactoryPluginInterface { | |
/** | |
* {@inheritdoc} | |
*/ | |
public function __construct(array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration, TermStorageInterface $termStorage) { | |
parent::__construct($configuration, $plugin_id, $plugin_definition); | |
$this->migration = $migration; | |
$this->termStorage = $termStorage; | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration = NULL) { | |
return new static( | |
$configuration, | |
$plugin_id, | |
$plugin_definition, | |
$migration, | |
$container->get('entity.manager')->getStorage('taxonomy_term') | |
); | |
} | |
protected function getTermId($name) { | |
if (!$this->terms) { | |
$tids = $this->termStorage | |
->getQuery() | |
->condition('vid', 'state') | |
->execute(); | |
$terms = $this->termStorage->loadMultiple($tids); | |
// For taxonomy, $terms = $this->termStorage->loadTree('state', 0, NULL, TRUE); works as well. | |
foreach ($terms as $term) { | |
// This could be $term->label() instead of $term->name->value. | |
$this->terms[$term->name->value] = $term->id(); | |
} | |
} | |
return $this->terms[$name]; | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) { | |
return $this->getTermId($value); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment