Skip to content

Instantly share code, notes, and snippets.

@simesy
Last active March 27, 2018 03:07
Show Gist options
  • Save simesy/755897d43b24e06afeed to your computer and use it in GitHub Desktop.
Save simesy/755897d43b24e06afeed to your computer and use it in GitHub Desktop.

Revisions

  1. simesy revised this gist Dec 30, 2015. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions EntityLookup.php
    Original file line number Diff line number Diff line change
    @@ -8,6 +8,7 @@
    namespace Drupal\migrate_entity_lookup\Plugin\migrate\process;

    use Drupal\migrate\ProcessPluginBase;
    use Drupal\migrate\Row;
    use Drupal\migrate\MigrateException;
    use Drupal\migrate\MigrateExecutableInterface;
    use Drupal\Core\Entity\Query\QueryFactory;
  2. simesy revised this gist Dec 30, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion EntityLookup.php
    Original file line number Diff line number Diff line change
    @@ -70,7 +70,7 @@ public function transform($value, MigrateExecutableInterface $migrate_executable
    return null;
    }
    else {
    $query = $this->entityQuery->get($this->configuration['entity_type']);// $this->entityTypeManager->getStorage($this->configuration['entity_type']);
    $query = $this->entityQuery->get($this->configuration['entity_type']);
    $entity_ids = $query->condition($this->configuration['property'], $value)->range(0, 1)->execute();
    if (count($entity_ids)) {
    return reset($entity_ids);
  3. simesy revised this gist Dec 30, 2015. 1 changed file with 9 additions and 11 deletions.
    20 changes: 9 additions & 11 deletions EntityLookup.php
    Original file line number Diff line number Diff line change
    @@ -10,11 +10,11 @@
    use Drupal\migrate\ProcessPluginBase;
    use Drupal\migrate\MigrateException;
    use Drupal\migrate\MigrateExecutableInterface;
    use Drupal\migrate\Row;
    use Drupal\Core\Entity\EntityTypeManagerInterface;
    use Drupal\Core\Entity\Query\QueryFactory;
    use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
    use Symfony\Component\DependencyInjection\ContainerInterface;


    /**
    * This plugin changes the current value by using it for an entity property search
    * and returning an entity id.
    @@ -30,7 +30,7 @@ class EntityLookup extends ProcessPluginBase implements ContainerFactoryPluginIn
    *
    * @var \Drupal\Core\Entity\EntityTypeManagerInterface
    */
    protected $entityTypeManager;
    protected $entityQuery;

    /**
    * Constructs an EntityLookup object.
    @@ -41,12 +41,12 @@ class EntityLookup extends ProcessPluginBase implements ContainerFactoryPluginIn
    * The plugin_id for the plugin instance.
    * @param mixed $plugin_definition
    * The plugin implementation definition.
    * @param array $entity_type_manager
    * The entity type manager.
    * @param \Drupal\Core\Entity\Query\QueryFactory $entity_query
    * The entity query factory.
    */
    public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entity_type_manager) {
    public function __construct(array $configuration, $plugin_id, $plugin_definition, QueryFactory $entity_query) {
    parent::__construct($configuration, $plugin_id, $plugin_definition);
    $this->entityTypeManager = $entity_type_manager;
    $this->entityQuery = $entity_query;
    }

    /**
    @@ -57,7 +57,7 @@ public static function create(ContainerInterface $container, array $configuratio
    $configuration,
    $plugin_id,
    $plugin_definition,
    $container->get('entity_type.manager')
    $container->get('entity.query')
    );
    }

    @@ -70,10 +70,8 @@ public function transform($value, MigrateExecutableInterface $migrate_executable
    return null;
    }
    else {
    $storage = $this->entityTypeManager->getStorage($this->configuration['entity_type']);
    $query = $storage->getQuery();
    $query = $this->entityQuery->get($this->configuration['entity_type']);// $this->entityTypeManager->getStorage($this->configuration['entity_type']);
    $entity_ids = $query->condition($this->configuration['property'], $value)->range(0, 1)->execute();
    // drush_print_r(array($this->configuration['entity_type'], $this->configuration['property'], $value, $entity_ids));
    if (count($entity_ids)) {
    return reset($entity_ids);
    }
  4. simesy revised this gist Dec 30, 2015. No changes.
  5. simesy revised this gist Dec 30, 2015. 3 changed files with 92 additions and 6 deletions.
    86 changes: 86 additions & 0 deletions EntityLookup.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,86 @@
    <?php

    /**
    * @file
    * Contains \Drupal\migrate_entity_lookup\Plugin\migrate\process\EntityLookup.
    */

    namespace Drupal\migrate_entity_lookup\Plugin\migrate\process;

    use Drupal\migrate\ProcessPluginBase;
    use Drupal\migrate\MigrateException;
    use Drupal\migrate\MigrateExecutableInterface;
    use Drupal\migrate\Row;
    use Drupal\Core\Entity\EntityTypeManagerInterface;
    use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
    use Symfony\Component\DependencyInjection\ContainerInterface;

    /**
    * This plugin changes the current value by using it for an entity property search
    * and returning an entity id.
    *
    * @MigrateProcessPlugin(
    * id = "entity_lookup"
    * )
    */
    class EntityLookup extends ProcessPluginBase implements ContainerFactoryPluginInterface {

    /**
    * The entity type manager.
    *
    * @var \Drupal\Core\Entity\EntityTypeManagerInterface
    */
    protected $entityTypeManager;

    /**
    * Constructs an EntityLookup object.
    *
    * @param array $configuration
    * A configuration array containing information about the plugin instance.
    * @param string $plugin_id
    * The plugin_id for the plugin instance.
    * @param mixed $plugin_definition
    * The plugin implementation definition.
    * @param array $entity_type_manager
    * The entity type manager.
    */
    public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entity_type_manager) {
    parent::__construct($configuration, $plugin_id, $plugin_definition);
    $this->entityTypeManager = $entity_type_manager;
    }

    /**
    * {@inheritdoc}
    */
    public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
    return new static(
    $configuration,
    $plugin_id,
    $plugin_definition,
    $container->get('entity_type.manager')
    );
    }

    /**
    * {@inheritdoc}
    */
    public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) {
    if (empty($value)) {
    // @todo: allow a default_value if nothing in source.
    return null;
    }
    else {
    $storage = $this->entityTypeManager->getStorage($this->configuration['entity_type']);
    $query = $storage->getQuery();
    $entity_ids = $query->condition($this->configuration['property'], $value)->range(0, 1)->execute();
    // drush_print_r(array($this->configuration['entity_type'], $this->configuration['property'], $value, $entity_ids));
    if (count($entity_ids)) {
    return reset($entity_ids);
    }
    else {
    // @todo: Return a default value if failed to retrieve one.
    return null;
    }
    }
    }
    }
    6 changes: 0 additions & 6 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -1,6 +0,0 @@
    process:
    field_blah:
    plugin: entity_lookup
    source: somedata
    entity_type: node
    property: title
    6 changes: 6 additions & 0 deletions migrate.migration.SNIPPET.yml
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,6 @@
    process:
    field_dest:
    plugin: entity_lookup
    source: source_field
    entity_type: taxonomy_term
    property: name
  6. simesy created this gist Dec 30, 2015.
    6 changes: 6 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,6 @@
    process:
    field_blah:
    plugin: entity_lookup
    source: somedata
    entity_type: node
    property: title