You are here

public function FormsStepsCommands::attachEntityToStep in Forms Steps 8

Existing or migrated content to be added to a workflow.

@command forms_steps:attach-entity @aliases fs-attach-entity @options instance_id An existing instance id. @options ignore_entity_id_check Ignore entity id check. @usage forms_steps:attach-entity example_1 node article 12345678 default step1 Attach a specific entity entry to a new forms steps workflow. @usage forms_steps:attach-entity example_1 node article 12345678 default step1 --instance_id=51e4e52a-d9d9-44c4-9aa1-9b075255e18c Attach a specific entity entry to an existing forms steps workflow.

Parameters

string $workflow: The forms_steps name machine.

$entity_type: The entity type.

$bundle: The bundle.

$id: The entity id.

$form_mode: The form_mode id.

$step: The step id.

array $options: A list of options.

Return value

array|void

Throws

\Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException

\Drupal\Component\Plugin\Exception\PluginNotFoundException

File

src/Commands/FormsStepsCommands.php, line 79

Class

FormsStepsCommands
Class FormsStepsCommands

Namespace

Drupal\forms_steps\Commands

Code

public function attachEntityToStep($workflow, $entity_type, $bundle, $id, $form_mode, $step, $options = [
  'instance_id' => NULL,
  'ignore_entity_id_check' => false,
]) {

  /** @var FormsSteps $forms_steps */
  $forms_steps = FormsSteps::load($workflow);
  if (!$forms_steps) {
    $message = $this
      ->t("The workflow '@workflow' doesn't exists.", [
      '@workflow' => $workflow,
    ]);
    $this
      ->outputError($message
      ->render());
    return;
  }
  if (!$forms_steps
    ->hasStep($step)) {
    $message = $this
      ->t("The step '@step' doesn't exists for the workflow '@workflow'.", [
      '@workflow' => $workflow,
      '@step' => $step,
    ]);
    $this
      ->outputError($message
      ->render());
    return;
  }
  if (!$options['ignore_entity_id_check']) {
    $entity = $this->entityTypeManager
      ->getStorage($entity_type)
      ->load($id);
    if (!$entity) {
      $message = $this
        ->t("The @entity_type entity id '@id' doesn't exists.", [
        '@entity_type' => $entity_type,
        '@id' => $id,
      ]);
      $this
        ->outputError($message
        ->render());
      return;
    }
  }
  try {
    $instanceId = $options['instance_id'] ?? $this->uuidService
      ->generate();

    /** @var FormsSteps $workflow */
    $workflow = $this->entityTypeManager
      ->getStorage(Workflow::ENTITY_TYPE)
      ->create([
      'instance_id' => $instanceId,
      'entity_type' => $entity_type,
      'bundle' => $bundle,
      'step' => $step,
      'entity_id' => $id,
      'form_mode' => $form_mode,
      'forms_steps' => $workflow,
    ]);
    $workflow
      ->save();
    return [
      'id' => $workflow
        ->id(),
      'instance_id' => $instanceId,
    ];
  } catch (\Exception $ex) {
    $this
      ->logger()
      ->critical('An exception has been raised while attaching an entity to a workflow step using drush. @exception', [
      '@exception' => $ex
        ->getMessage(),
    ]);
    $this
      ->logger()
      ->critical($ex
      ->getTraceAsString());
  }
  $message = $this
    ->t('Error while inserting the workflow. Check watch dog for more information.');
  $this
    ->outputError($message
    ->render());
  return;
}