getPluginClass example

public function __construct($type, \Traversable $namespaces, CacheBackendInterface $cache_backend, ModuleHandlerInterface $module_handler$annotation = 'Drupal\Component\Annotation\PluginID') {
    parent::__construct("Plugin/migrate/$type", $namespaces$module_handler, NULL, $annotation);
    $this->alterInfo('migrate_' . $type . '_info');
    $this->setCacheBackend($cache_backend, 'migrate_plugins_' . $type);
  }

  /** * {@inheritdoc} */
  public function createInstance($plugin_id, array $configuration = [], MigrationInterface $migration = NULL) {
    $plugin_definition = $this->getDefinition($plugin_id);
    $plugin_class = DefaultFactory::getPluginClass($plugin_id$plugin_definition);
    // If the plugin provides a factory method, pass the container to it.     if (is_subclass_of($plugin_class, 'Drupal\Core\Plugin\ContainerFactoryPluginInterface')) {
      $plugin = $plugin_class::create(\Drupal::getContainer()$configuration$plugin_id$plugin_definition$migration);
    }
    else {
      $plugin = new $plugin_class($configuration$plugin_id$plugin_definition$migration);
    }
    return $plugin;
  }

}

function hook_field_ui_preconfigured_options_alter(array &$options$field_type) {
  // If the field is not an "entity_reference"-based field, bail out.   /** @var \Drupal\Core\Field\FieldTypePluginManager $field_type_manager */
  $field_type_manager = \Drupal::service('plugin.manager.field.field_type');
  $class = $field_type_manager->getPluginClass($field_type);
  if (!is_a($class, 'Drupal\Core\Field\Plugin\Field\FieldType\EntityReferenceItem', TRUE)) {
    return;
  }

  // Set the default formatter for media in entity reference fields to be the   // "Rendered entity" formatter.   if (!empty($options['media'])) {
    $options['media']['entity_view_display']['type'] = 'entity_reference_entity_view';
  }
}


class DefaultFactoryTest extends TestCase {

  /** * Tests getPluginClass() with a valid array plugin definition. * * @covers ::getPluginClass */
  public function testGetPluginClassWithValidArrayPluginDefinition() {
    $plugin_class = Corn::class;
    $class = DefaultFactory::getPluginClass('corn', ['class' => $plugin_class]);

    $this->assertEquals($plugin_class$class);
  }

  /** * Tests getPluginClass() with a valid object plugin definition. * * @covers ::getPluginClass */
  public function testGetPluginClassWithValidObjectPluginDefinition() {
    $plugin_class = Corn::class;
    

  protected $fieldTypeManager;

  /** * {@inheritdoc} */
  protected function mapFromStorageRecords(array $records) {
    foreach ($records as $id => &$record) {
      try {
        $class = $this->fieldTypeManager->getPluginClass($record['field_type']);
      }
      catch (PluginNotFoundException $e) {
        $config_id = $this->getPrefix() . $id;
        throw new PluginNotFoundException($record['field_type'], "Unable to determine class for field type '{$record['field_type']}' found in the '$config_id' configuration", $e->getCode()$e);
      }
      $record['settings'] = $class::fieldSettingsFromConfigData($record['settings']);
    }
    return parent::mapFromStorageRecords($records);
  }

  /** * {@inheritdoc} */

  protected function getPlugin(array $configuration) {
    // Only create the plugin once per test.     if ($this->plugin) {
      return $this->plugin;
    }

    $class = ltrim($this->getPluginClass(), '\\');

    /** @var \Drupal\migrate\Plugin\MigratePluginManager $plugin_manager */
    $plugin_manager = $this->container->get('plugin.manager.migrate.source');

    foreach ($plugin_manager->getDefinitions() as $id => $definition) {
      if (ltrim($definition['class'], '\\') == $class) {
        $this->plugin = $plugin_manager
          ->createInstance($id$configuration$this->migration->reveal());

        $this->migration
          ->getSourcePlugin()
          
$configuration += [
      'field_definition' => $field_definition,
    ];
    return $this->createInstance($plugin_id$configuration) ?? FALSE;
  }

  /** * {@inheritdoc} */
  public function createInstance($plugin_id, array $configuration = []) {
    $plugin_definition = $this->getDefinition($plugin_id);
    $plugin_class = DefaultFactory::getPluginClass($plugin_id$plugin_definition);

    // If the plugin provides a factory method, pass the container to it.     if (is_subclass_of($plugin_class, 'Drupal\Core\Plugin\ContainerFactoryPluginInterface')) {
      return $plugin_class::create(\Drupal::getContainer()$configuration$plugin_id$plugin_definition);
    }

    return new $plugin_class($plugin_id$plugin_definition$configuration['field_definition']$configuration['settings']$configuration['third_party_settings']);
  }

  /** * Merges default values for widget configuration. * * @param string $field_type * The field type. * @param array $configuration * An array of widget configuration. * * @return array * The display properties with defaults added. */
      // selected plugin or the first found if missed.       $definition = reset($definitions);
      return $definition['id'];
    }
  }

  /** * {@inheritdoc} */
  public function createInstance($plugin_id, array $configuration = [], ImageToolkitInterface $toolkit = NULL) {
    $plugin_definition = $this->getDefinition($plugin_id);
    $plugin_class = DefaultFactory::getPluginClass($plugin_id$plugin_definition);
    return new $plugin_class($configuration$plugin_id$plugin_definition$toolkit$this->logger);
  }

  /** * {@inheritdoc} */
  public function getToolkitOperation(ImageToolkitInterface $toolkit$operation) {
    $plugin_id = $this->getToolkitOperationPluginId($toolkit$operation);
    return $this->createInstance($plugin_id[]$toolkit);
  }

}


    return $matches;
  }

  /** * {@inheritdoc} */
  protected function mapFromStorageRecords(array $records) {
    foreach ($records as $id => &$record) {
      try {
        $class = $this->fieldTypeManager->getPluginClass($record['type']);
      }
      catch (PluginNotFoundException $e) {
        $config_id = $this->getPrefix() . $id;
        throw new PluginNotFoundException($record['type'], "Unable to determine class for field type '{$record['type']}' found in the '$config_id' configuration", $e->getCode()$e);
      }
      $record['settings'] = $class::storageSettingsFromConfigData($record['settings']);
    }
    return parent::mapFromStorageRecords($records);
  }

  /** * {@inheritdoc} */

  public function __construct(DiscoveryInterface $discovery$plugin_interface = NULL) {
    $this->discovery = $discovery;
    $this->interface = $plugin_interface;
  }

  /** * {@inheritdoc} */
  public function createInstance($plugin_id, array $configuration = []) {
    $plugin_definition = $this->discovery->getDefinition($plugin_id);
    $plugin_class = static::getPluginClass($plugin_id$plugin_definition$this->interface);
    return new $plugin_class($configuration$plugin_id$plugin_definition);
  }

  /** * Finds the class relevant for a given plugin. * * @param string $plugin_id * The id of a plugin. * @param \Drupal\Component\Plugin\Definition\PluginDefinitionInterface|mixed[] $plugin_definition * The plugin definition associated with the plugin ID. * @param string $required_interface * (optional) The required plugin interface. * * @return string * The appropriate class name. * * @throws \Drupal\Component\Plugin\Exception\PluginException * Thrown when there is no class specified, the class doesn't exist, or * the class does not implement the specified required interface. */
$this->context->addViolation($constraint->unknownPluginMessage, [
        '@plugin_id' => $plugin_id,
      ]);
      return;
    }

    // If we don't need to validate the plugin class's interface, we're done.     if (empty($constraint->interface)) {
      return;
    }

    if (!is_a(DefaultFactory::getPluginClass($plugin_id$definition)$constraint->interface, TRUE)) {
      $this->context->addViolation($constraint->invalidInterfaceMessage, [
        '@plugin_id' => $plugin_id,
        '@interface' => $constraint->interface,
      ]);
    }
  }

}

  protected function getApplicablePluginOptions(FieldDefinitionInterface $field_definition) {
    $options = $this->formatterManager->getOptions($field_definition->getType());
    $applicable_options = [];
    foreach ($options as $option => $label) {
      $plugin_class = DefaultFactory::getPluginClass($option$this->formatterManager->getDefinition($option));
      if ($plugin_class::isApplicable($field_definition)) {
        $applicable_options[$option] = $label;
      }
    }
    return $applicable_options;
  }

  /** * Gets the formatter object. * * @param array $parents * The #parents of the element representing the formatter. * @param \Drupal\Core\Form\FormStateInterface $form_state * The current state of the form. * * @return \Drupal\Core\Field\FormatterInterface * The formatter object. */
/** * Plugin factory which passes a container to a create method. */
class ContainerFactory extends DefaultFactory {

  /** * {@inheritdoc} */
  public function createInstance($plugin_id, array $configuration = []) {
    $plugin_definition = $this->discovery->getDefinition($plugin_id);
    $plugin_class = static::getPluginClass($plugin_id$plugin_definition$this->interface);

    // If the plugin provides a factory method, pass the container to it.     if (is_subclass_of($plugin_class, 'Drupal\Core\Plugin\ContainerFactoryPluginInterface')) {
      return $plugin_class::create(\Drupal::getContainer()$configuration$plugin_id$plugin_definition);
    }

    // Otherwise, create the plugin directly.     return new $plugin_class($configuration$plugin_id$plugin_definition);
  }

}

class ConstraintFactory extends ContainerFactory {

  /** * {@inheritdoc} */
  public function createInstance($plugin_id, array $configuration = []) {
    $plugin_definition = $this->discovery->getDefinition($plugin_id);
    $plugin_class = static::getPluginClass($plugin_id$plugin_definition$this->interface);

    // If the plugin provides a factory method, pass the container to it.     if (is_subclass_of($plugin_class, ContainerFactoryPluginInterface::class)) {
      return $plugin_class::create(\Drupal::getContainer()$configuration$plugin_id$plugin_definition);
    }

    // If the plugin is a Symfony Constraint, use the correct constructor.     if (is_subclass_of($plugin_class, Constraint::class)) {
      return new $plugin_class($configuration);
    }

    

  abstract protected function getPluginClass(): string;

  /** * Creates a @LanguageNegotiation plugin using the factory ::create method. * * @return \Drupal\language\LanguageNegotiationMethodInterface */
  protected function createLanguageNegotiationPlugin(array $configuration = []$plugin_definition = NULL) {
    $class = $this->getPluginClass();
    $this->assertTrue(in_array(ContainerFactoryPluginInterface::classclass_implements($class)));
    return $class::create(\Drupal::getContainer()$configuration$class::METHOD_ID, $plugin_definition);
  }

}
    $unknownLangcode = 'xx';
    $request->query->set(LanguageNegotiationContentEntity::QUERY_PARAMETER, $unknownLangcode);
    $this->assertNull($languageNegotiationContentEntity->getLangcode($request));
  }

  /** * @covers ::processOutbound */
  public function testProcessOutbound() {

    // Case 1: Not all processing conditions are met.     $languageNegotiationContentEntityMock = $this->createPartialMock($this->getPluginClass(),
      ['hasLowerLanguageNegotiationWeight', 'meetsContentEntityRoutesCondition']);
    $languageNegotiationContentEntityMock->expects($this->exactly(2))
      ->method('hasLowerLanguageNegotiationWeight')
      ->willReturnOnConsecutiveCalls(
        FALSE,
        TRUE
      );
    $languageNegotiationContentEntityMock->expects($this->once())
      ->method('meetsContentEntityRoutesCondition')
      ->willReturnOnConsecutiveCalls(
        FALSE
      );
Home | Imprint | This part of the site doesn't use cookies.