You are here

public function WorkflowManager::createInstance in State Machine 8

Creates a pre-configured instance of a plugin.

Parameters

string $plugin_id: The ID of the plugin being instantiated.

array $configuration: An array of configuration relevant to the plugin instance.

Return value

object A fully configured plugin instance.

Throws

\Drupal\Component\Plugin\Exception\PluginException If the instance cannot be created, such as if the ID is invalid.

Overrides PluginManagerBase::createInstance

File

src/WorkflowManager.php, line 80

Class

WorkflowManager
Manages discovery and instantiation of workflow plugins.

Namespace

Drupal\state_machine

Code

public function createInstance($plugin_id, array $configuration = []) {
  if (empty($this->plugins[$plugin_id])) {
    $plugin_definition = $this
      ->getDefinition($plugin_id);
    if (empty($plugin_definition['group'])) {
      throw new PluginException(sprintf('The workflow %s must define the group property.', $plugin_id));
    }
    $group_definition = $this->groupManager
      ->getDefinition($plugin_definition['group']);
    $plugin_class = $group_definition['workflow_class'];
    if (is_subclass_of($plugin_class, ContainerFactoryPluginInterface::class)) {
      $this->plugins[$plugin_id] = $plugin_class::create(\Drupal::getContainer(), $configuration, $plugin_id, $plugin_definition);
    }
    else {
      $this->plugins[$plugin_id] = new $plugin_class($configuration, $plugin_id, $plugin_definition);
    }
  }
  return $this->plugins[$plugin_id];
}