You are here

class FormsStepsCommands in Forms Steps 8

Class FormsStepsCommands

@package Drupal\forms_steps\Commands

Hierarchy

Expanded class hierarchy of FormsStepsCommands

1 string reference to 'FormsStepsCommands'
drush.services.yml in ./drush.services.yml
drush.services.yml
1 service uses FormsStepsCommands
forms_steps.commands in ./drush.services.yml
Drupal\forms_steps\Commands\FormsStepsCommands

File

src/Commands/FormsStepsCommands.php, line 17

Namespace

Drupal\forms_steps\Commands
View source
class FormsStepsCommands extends DrushCommands {
  use StringTranslationTrait;

  /**
   * EntityTypeManager Service.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  private $entityTypeManager;

  /**
   * UUID Service.
   *
   * @var \Drupal\Component\Uuid\UuidInterface
   */
  private $uuidService;

  /**
   * Constructor().
   *
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   * @param \Drupal\Component\Uuid\UuidInterface $uuid_service
   */
  public function __construct(EntityTypeManagerInterface $entity_type_manager, UuidInterface $uuid_service) {
    parent::__construct();
    $this->entityTypeManager = $entity_type_manager;
    $this->uuidService = $uuid_service;
  }

  /**
   * Existing or migrated content to be added to a workflow.
   *
   * @param string $workflow
   *  The forms_steps name machine.
   * @param $entity_type
   *  The entity type.
   * @param $bundle
   *  The bundle.
   * @param $id
   *  The entity id.
   * @param $form_mode
   *  The form_mode id.
   * @param $step
   *  The step id.
   * @param array $options
   *  A list of options.
   *
   * @return array|void
   * @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
   * @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
   * @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.
   */
  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;
  }
  private function outputError($message) {
    $this
      ->yell($message, 40, 'red');
  }

}

Members

Namesort descending Modifiers Type Description Overrides
FormsStepsCommands::$entityTypeManager private property EntityTypeManager Service.
FormsStepsCommands::$uuidService private property UUID Service.
FormsStepsCommands::attachEntityToStep public function Existing or migrated content to be added to a workflow.
FormsStepsCommands::outputError private function
FormsStepsCommands::__construct public function Constructor().
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.