getPluginIdFromFieldType example

/** * {@inheritdoc} */
  protected function setUp(): void {
    $this->fieldManager = $this->prophesize(MigrateFieldPluginManagerInterface::class);
    $this->fieldPlugin = $this->prophesize(MigrateFieldInterface::class);
    $this->migrateExecutable = $this->prophesize(MigrateExecutable::class);
    $this->migration = $this->prophesize(MigrationInterface::class);
    $this->row = $this->prophesize(Row::class);

    $this->fieldManager->getPluginIdFromFieldType('foo', []$this->migration->reveal())->willReturn('foo');
    $this->fieldManager->createInstance('foo', []$this->migration->reveal())->willReturn($this->fieldPlugin);

    parent::setUp();
  }

  /** * Tests the transform method. * * @param string $method * The method to call. * @param string $value * The value to process. * @param mixed $expected_value * The expected transformed value. * @param string $migrate_exception * The MigrateException message to expect. * @param bool $plugin_not_found * Whether the field plugin is not found. * * @covers ::transform * @dataProvider providerTestTransform */

  protected function callMethodOnFieldPlugin(MigrateFieldPluginManagerInterface $field_plugin_manager$field_type$method, Row $row) {
    $plugin_id = $field_plugin_manager->getPluginIdFromFieldType($field_type[]$this->migration);
    $plugin_instance = $field_plugin_manager->createInstance($plugin_id[]$this->migration);
    if (!is_callable([$plugin_instance$method])) {
      throw new MigrateException('The specified method does not exist or is not callable.');
    }
    return call_user_func_array([$plugin_instance$method][$row]);
  }

}

  protected function getFieldPlugin($field_type, MigrationInterface $migration) {
    $core = $this->getCoreVersion($migration);
    if (!isset($this->fieldPluginCache[$core][$field_type])) {
      try {
        $plugin_id = $this->fieldPluginManager->getPluginIdFromFieldType($field_type['core' => $core]$migration);
        $plugin = $this->fieldPluginManager->createInstance($plugin_id['core' => $core]$migration);
      }
      catch (PluginNotFoundException $ex) {
        $plugin = FALSE;
      }
      $this->fieldPluginCache[$core][$field_type] = $plugin;
    }
    return $this->fieldPluginCache[$core][$field_type];
  }

  /** * Gets all field information related to this migration. * * @param string $core * The Drupal core version to get fields for. * * @return array * A multidimensional array of source data from the relevant field instance * migration, keyed first by entity type, then by bundle and finally by * field name. */
$container->get('plugin.manager.migrate.field'),
      $migration
    );
  }

  /** * {@inheritdoc} */
  public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row$destination_property) {
    $field_type = is_array($value) ? $value[0] : $value;
    try {
      $plugin_id = $this->fieldPluginManager->getPluginIdFromFieldType($field_type[]$this->migration);
      return $this->fieldPluginManager->createInstance($plugin_id[]$this->migration)->getFieldType($row);
    }
    catch (PluginNotFoundException $e) {
      return parent::transform($value$migrate_executable$row$destination_property);
    }
  }

}
protected function setUp(): void {
    parent::setUp();
    $this->pluginManager = $this->container->get('plugin.manager.migrate.field');
  }

  /** * Tests that the correct MigrateField plugins are used. * * @covers ::getPluginIdFromFieldType */
  public function testPluginSelection() {
    $this->assertSame('link', $this->pluginManager->getPluginIdFromFieldType('link', ['core' => 6]));
    $this->assertSame('link_field', $this->pluginManager->getPluginIdFromFieldType('link_field', ['core' => 7]));
    $this->assertSame('image', $this->pluginManager->getPluginIdFromFieldType('image', ['core' => 7]));
    $this->assertSame('file', $this->pluginManager->getPluginIdFromFieldType('file', ['core' => 7]));
    $this->assertSame('d6_file', $this->pluginManager->getPluginIdFromFieldType('file', ['core' => 6]));
    $this->assertSame('d6_text', $this->pluginManager->getPluginIdFromFieldType('text', ['core' => 6]));
    $this->assertSame('d7_text', $this->pluginManager->getPluginIdFromFieldType('text', ['core' => 7]));

    // Test that the deprecated d6 'date' plugin is not returned.     $this->assertSame('datetime', $this->pluginManager->getPluginIdFromFieldType('date', ['core' => 6]));

    // Test fallback when no core version is specified.
/** @var \Drupal\Core\Cache\CacheBackendInterface $cache */
    $cache = $this->prophesize(CacheBackendInterface::class)->reveal();
    /** @var \Drupal\Core\Extension\ModuleHandlerInterfaceModuleHandlerInterface $module_handler */
    $module_handler = $this->prophesize(ModuleHandlerInterface::class)->reveal();
    $discovery = $this->prophesize(AnnotatedClassDiscovery::class);
    $discovery->getDefinitions()->willReturn($this->pluginFixtureData());
    $manager = new MigrateFieldPluginManagerTestClass('field', new \ArrayObject()$cache$module_handler, MigrateField::class$discovery->reveal());
    if (!$expected_plugin_id) {
      $this->expectException(PluginNotFoundException::class);
      $this->expectExceptionMessage(sprintf("Plugin ID '%s' was not found.", $field_type));
    }
    $actual_plugin_id = $manager->getPluginIdFromFieldType($field_type['core' => $core]);
    $this->assertSame($expected_plugin_id$actual_plugin_id);

  }

  /** * Provides data for testWeights(). * * @return array * The data. */
  public function weightsData() {
    
Home | Imprint | This part of the site doesn't use cookies.