daily pastebin goal
8%
SHARE
TWEET

Untitled

a guest Feb 13th, 2018 54 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. field_categories:
  2.   plugin: sub_process
  3.   source: remote_categories
  4.   process:
  5.     target_id:
  6.       plugin: migration_lookup
  7.       migration: my_existing_taxonomy_migration
  8.       source: name_of_key
  9.    
  10. {
  11.   remote_categories: [ 'alpha', 'beta', 'gamma' ]
  12. }
  13.    
  14. <?php
  15.  
  16. namespace Drupalmy_modulePluginmigrateprocess;
  17.  
  18. use DrupalmigrateProcessPluginBase;
  19. use DrupalmigrateMigrateException;
  20. use DrupalmigrateMigrateExecutableInterface;
  21. use DrupalmigrateRow;
  22.  
  23. /**
  24.  * Transforms an array of values into an array of associative arrays with one key.
  25.  * E.g. ['alpha', 'beta'] becomes [[value => 'alpha'], [value => 'beta']]
  26.  *
  27.  * Use this plugin to preprocess a numeric/non-associative array for other plugins
  28.  * that requires an associative array as input, such as the sub_process plugin.
  29.  *  
  30.  * Available configuration keys:
  31.  * - source: Source property.
  32.  * - keyname: name of the key to be used for the associative sub-arrays, defaults to 'value'
  33.  *
  34.  * Example:
  35.  *
  36.  * @code
  37.  * source:
  38.  *   my_flat_array:
  39.  *     - category1
  40.  *     - category2
  41.  * process:
  42.  *   my_associative_array:
  43.  *     plugin: deepen
  44.  *     source: my_flat_array
  45.  *   field_taxonomy_term:
  46.  *     plugin: sub_process
  47.  *     source: '@my_associative_array'
  48.  *       target_id:
  49.  *         plugin: migration_lookup
  50.  *         migration: my_taxonomy_migration
  51.  *         source: value
  52.  * @endcode
  53.  *
  54.  * @see DrupalmigratePluginMigrateProcessInterface
  55.  *
  56.  * @MigrateProcessPlugin(
  57.  *   id = "deepen",
  58.  *   handle_multiples = TRUE
  59.  *  * )
  60.  */
  61. class Deepen extends ProcessPluginBase {
  62.  
  63.   /**
  64.    * {@inheritdoc}
  65.    */
  66.   public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) {
  67.     $keyname = (is_string($this->configuration['keyname']) && $this->configuration['keyname'] != '') ? $this->configuration['keyname'] : 'value';
  68.  
  69.     if (is_array($value) || $value instanceof Traversable) {
  70.       $result = [];
  71.       foreach ($value as $sub_value) {
  72.         $result[] = [$keyname => $sub_value];
  73.       }
  74.       return $result;
  75.     }
  76.     else {
  77.       throw new MigrateException(sprintf('%s is not traversable', var_export($value, TRUE)));
  78.     }
  79.   }
  80. }
RAW Paste Data
Top