You are here

class WorkflowRepository in Forms Steps 8

Class WorkflowRepository.

@package Drupal\forms_steps

Hierarchy

Expanded class hierarchy of WorkflowRepository

2 files declare their use of WorkflowRepository
FormsStepsProgressBarBlock.php in src/Plugin/Block/FormsStepsProgressBarBlock.php
WorkflowController.php in src/Controller/WorkflowController.php
1 string reference to 'WorkflowRepository'
forms_steps.services.yml in ./forms_steps.services.yml
forms_steps.services.yml
1 service uses WorkflowRepository
forms_steps.workflow.repository in ./forms_steps.services.yml
Drupal\forms_steps\Repository\WorkflowRepository

File

src/Repository/WorkflowRepository.php, line 15

Namespace

Drupal\forms_steps\Repository
View source
class WorkflowRepository {
  use MessengerTrait;
  use StringTranslationTrait;

  /**
   * The database connection.
   *
   * @var \Drupal\Core\Database\Connection
   */
  protected $connection;
  const FORMS_STEPS_WORKFLOW_DB = 'forms_steps_workflow';

  /**
   * Construct a repository object.
   *
   * @param \Drupal\Core\Database\Connection $connection
   *   The database connection.
   * @param \Drupal\Core\StringTranslation\TranslationInterface $translation
   *   The translation service.
   */
  public function __construct(Connection $connection, TranslationInterface $translation) {
    $this->connection = $connection;
    $this
      ->setStringTranslation($translation);
  }

  /**
   * Save an entry in the database.
   *
   * Exception handling is shown in this example. It could be simplified
   * without the try/catch blocks, but since an insert will throw an exception
   * and terminate your application if the exception is not handled, it is best
   * to employ try/catch.
   *
   * @param array $entry
   *   An array containing all the fields of the database record.
   *
   * @return int
   *   The number of updated rows.
   *
   * @throws \Exception
   *   When the database insert fails.
   *
   * @see db_insert()
   */
  public function insert(array $entry) {
    $return_value = NULL;
    try {
      $return_value = $this->connection
        ->insert(self::FORMS_STEPS_WORKFLOW_DB)
        ->fields($entry)
        ->execute();
    } catch (\Exception $e) {
      $this
        ->messenger()
        ->addMessage($this
        ->t('db_insert failed. Message = %message', [
        '%message' => $e
          ->getMessage(),
      ]), 'error');
    }
    return $return_value;
  }

  /**
   * Update an entry in the database.
   *
   * @param array $entry
   *   An array containing all the fields of the item to be updated.
   *
   * @return int
   *   The number of updated rows.
   *
   * @see db_update()
   */
  public function update(array $entry) {
    try {

      // Connection->update()...->execute() returns the number of rows updated.
      $count = $this->connection
        ->update(self::FORMS_STEPS_WORKFLOW_DB)
        ->fields($entry)
        ->condition('id', $entry['id'])
        ->execute();
    } catch (\Exception $e) {
      $this
        ->messenger()
        ->addMessage($this
        ->t('db_update failed. Message = %message, query= %query', [
        '%message' => $e
          ->getMessage(),
        '%query' => $e->query_string,
      ]), 'error');
    }
    return $count;
  }

  /**
   * Delete an entry from the database.
   *
   * @param array $entry
   *   An array containing at least the person identifier 'pid' element of the
   *   entry to delete.
   *
   * @see Drupal\Core\Database\Connection::delete()
   */
  public function delete(array $entry) {
    $this->connection
      ->delete(self::FORMS_STEPS_WORKFLOW_DB)
      ->condition('id', $entry['id'])
      ->execute();
  }

  /**
   * Read workflow from the database using a filter array.
   *
   * @param array $entry
   *   An array containing all the fields used to search the entries in the
   *   table.
   *
   * @return object
   *   An object containing the loaded entries if found.
   *
   * @see Drupal\Core\Database\Connection::select()
   */
  public function load(array $entry = []) {

    // Read all the fields from the dbtng_example table.
    $select = $this->connection
      ->select(self::FORMS_STEPS_WORKFLOW_DB)
      ->fields(self::FORMS_STEPS_WORKFLOW_DB);

    // Add each field and value as a condition to this query.
    foreach ($entry as $field => $value) {
      $select
        ->condition($field, $value);
    }

    // Return the result in object format.
    return $select
      ->execute()
      ->fetchAll();
  }

}

Members

Namesort descending Modifiers Type Description Overrides
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
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.
WorkflowRepository::$connection protected property The database connection.
WorkflowRepository::delete public function Delete an entry from the database.
WorkflowRepository::FORMS_STEPS_WORKFLOW_DB constant
WorkflowRepository::insert public function Save an entry in the database.
WorkflowRepository::load public function Read workflow from the database using a filter array.
WorkflowRepository::update public function Update an entry in the database.
WorkflowRepository::__construct public function Construct a repository object.