class WorkflowRepository in Forms Steps 8
Class WorkflowRepository.
@package Drupal\forms_steps
Hierarchy
- class \Drupal\forms_steps\Repository\WorkflowRepository uses MessengerTrait, StringTranslationTrait
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'
1 service uses WorkflowRepository
File
- src/
Repository/ WorkflowRepository.php, line 15
Namespace
Drupal\forms_steps\RepositoryView 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
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
MessengerTrait:: |
protected | property | The messenger. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. | |
WorkflowRepository:: |
protected | property | The database connection. | |
WorkflowRepository:: |
public | function | Delete an entry from the database. | |
WorkflowRepository:: |
constant | |||
WorkflowRepository:: |
public | function | Save an entry in the database. | |
WorkflowRepository:: |
public | function | Read workflow from the database using a filter array. | |
WorkflowRepository:: |
public | function | Update an entry in the database. | |
WorkflowRepository:: |
public | function | Construct a repository object. |