0

I'm trying import taxonomy terms from a json file which I can't change, into a drupal site taxonomy called skills_identified via a yaml file

id: sf_taxonomy_terms
label: 'Taxonomy Terms Migration'
migration_group: sf_taxonomy_terms
source:
  plugin: url
  data_fetcher_plugin: file
  data_parser_plugin: json
  urls: 'modules/custom/migration_sf/files/sf_taxonomy_terms.json'
  item_selector: skills_identified
  fields:
    - name: tid
      label: 'Term ID'
      selector: Id
    - name: name
      label: 'Name'
      selector: Value
  ids:
    tid:
      type: integer
process:
  vid:
    plugin: default_value
    default_value: skills_identified
  name: name
  parent:
    plugin: migration_lookup
    migration: sf_taxonomy_terms
    source: parent
destination:
  plugin: 'entity:taxonomy_term'
  default_bundle: skills_identified

This is my json file

[
  {
  "title": "Test title",
  "year_of_entry": "2024/25",
  "programme_code": 9135,
  "skills_identified": [
    {
      "Id": 0,
      "Value": "Research"
    },
    {
      "Id": 2,
      "Value": "Critical thinking"
    }
  ],
  "careers_direct": [],
  "careers_broad": [],
  "careers_gos": []
  },
  {
  "title": "A title for my content",
  "year_of_entry": "2024/25",
  "programme_code": 9146,
  "skills_identified": [
    {
      "Id": 3,
      "Value": "Leadership"
    },
    {
      "Id": 4,
      "Value": "Creative thinking"
    }
  ],
  "careers_direct": [],
  "careers_broad": [],
  "careers_gos": []
  }
]

I have imported "title","year_of_entry" and "programme_code" fields successfully into my content type but I'm having difficulty with taxonomy terms.

2
  • What is the exact error you're getting? What do you expect to happen, and what's happening? Commented Nov 6 at 11:40
  • I'm trying to add the "Value": ".. under skills_identified from the json file my taxonomy also called skills_identified via the yaml file, when Irun drush ms there are no entries in the total column. Note I have migrate tools and migrate plus enabled
    – hobbsie
    Commented Nov 6 at 14:02

1 Answer 1

0

I think the problem is that the terms are sub values of the actual items inside the JSON file. So you would need to do some extra iteration to create migration items from these. I am not sure if the url source plugin that you use, can actually do this. I did not find any examples that could be helpful for this situation.

One approach would be to use the JSON as source for the node migration and then use the entity_generate process plugin to generate the terms while migrating the nodes.

Another approach would be to create a custom source plugin that does some extra iterations and fetches each of the terms from the JSON. Here is an example of how that can be done in your case:

<?php

namespace Drupal\tic\Plugin\migrate\source;

use Drupal\migrate\Plugin\migrate\source\SourcePluginBase;

/**
 * Custom source plugin.
 *
 * @MigrateSource(
 *   id = "skills"
 * )
 */
class Skills extends SourcePluginBase {

  /**
   * {@inheritdoc}
   */
  public function getIds() {
    $ids = [
      'id' => [
        'type' => 'string',
      ],
    ];
    return $ids;
  }

  /**
   * {@inheritdoc}
   */
  public function fields() {
    return [
      'name' => $this->t('Name'),
      'id' => $this->t('Id'),
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function __toString() {
    return "skills";
  }

  /**
   * Fetch data.
   *
   * @return array|bool
   *   Return the response as decoded array.
   */
  public function getData() {
    $file = file_get_contents('modules/custom/tic/files/sf_taxonomy_terms.json');
    $data = json_decode($file);
    return $data;
  }

  /**
   * Initializes the iterator with the source data.
   *
   * @return \Iterator
   *   An iterator containing the data for this source.
   */
  protected function initializeIterator() {
    $data = $this->getData();
    foreach ($data as $datum) {
      foreach ($datum->skills_identified as $skill) {
        $rows[] = [
          'id' => $skill->Id,
          'name' => $skill->Value,
        ];
      }
    }
    return new \ArrayIterator($rows);
  }
}

This will provide a migration source with rows with id and name fields.

You can use this as follows in your migration yml file:

source:
  plugin: skills

There is one thing that I find puzzling: you mention that you can not change the file although you are able to store it locally in the files directory. How does that work?

1
  • The json file is from a url which I copied and saved to my local file directory for development
    – hobbsie
    Commented Nov 28 at 18:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.