DefaultFactory example



    // In addition to finding all of the plugins available for a type, a plugin     // type must also be able to create instances of that plugin. For example, a     // specific instance of a "User login" block, configured with a custom     // title. To handle plugin instantiation, plugin managers can use one of the     // factory classes included with the plugin system, or create their own.     // DefaultFactory is a simple, general purpose factory suitable for     // many kinds of plugin types. Factories need access to the plugin     // definitions (e.g., since that's where the plugin's class is specified),     // so we provide it the discovery object.     $this->factory = new DefaultFactory($this->discovery);
  }

}

  public function __construct(\Traversable $namespaces, array $definitions, ModuleHandlerInterface $module_handler = NULL, $alter_hook = NULL, $interface = NULL) {
    // Create the object that can be used to return definitions for all the     // plugins available for this type. Most real plugin managers use a richer     // discovery implementation, but StaticDiscovery lets us add some simple     // mock plugins for unit testing.     $this->discovery = new StaticDiscovery();
    $this->factory = new DefaultFactory($this->discovery, $interface);

    // Add the static definitions.     foreach ($definitions as $key => $definition) {
      $this->discovery->setDefinition($key$definition);
    }

    $this->moduleHandler = $module_handler;

    if ($alter_hook) {
      $this->alterInfo($alter_hook);
    }
  }

  public function __construct(ModuleHandlerInterface $module_handler) {
    // Create the object that can be used to return definitions for all the     // plugins available for this type. Most real plugin managers use a richer     // discovery implementation, but StaticDiscovery lets us add some simple     // mock plugins for unit testing.     $this->discovery = new StaticDiscovery();
    $this->factory = new DefaultFactory($this, 'Drupal\Component\Plugin\PluginInspectionInterface');
    $this->moduleHandler = $module_handler;

    // Specify default values.     $this->defaults = [
      'metadata' => [
        'default' => TRUE,
      ],
    ];

    // Add a plugin with a custom value.     $this->discovery->setDefinition('test_block1', [
      
Home | Imprint | This part of the site doesn't use cookies.