Skip to content
Snippets Groups Projects

CO2: Adding new update function for database in order to update the value to...

1 file
+ 38
0
Compare changes
  • Side-by-side
  • Inline
@@ -5,6 +5,7 @@
* Primary module hooks for Carbon impact evaluator module.
*/
use \Drupal\Core\Entity\EntityInterface;
use \Drupal\node\Entity\Node;
/**
* Implements hook_theme().
@@ -318,4 +319,41 @@ function carbon_impact_evaluator_update_8001() {
// Adds a message indicating there is no field to be add to the table
\Drupal::logger('Carbon Impact Evaluator')->notice('A new field "'.$entity_field.'" was add to the Carbon Impact Evaluator table.');
}
}
/**
* Implements hook_update_N().
*
* This function will add a new value to the field field_identifier to the co2_info table.
*/
function carbon_impact_evaluator_update_8002() {
// Defines the new field
$table = 'co2_info';
$entity_field = 'field_identifier';
// Check if the field already exists in the schema
$database = \Drupal::database();
/** @var \Drupal\Core\Database\Schema $schema */
$schema = $database->schema();
$check_field = $schema->fieldExists($table, $entity_field);
// Check if the field already exists
if ($check_field) {
// Adds a value to the field
try {
$database->update('co2_info')
->condition($entity_field, NULL, 'IS NULL')
->fields([
'field_identifier' => 'node',
])
->execute();
// Adds a message indicating this field was updated in the table table
\Drupal::logger('Carbon Impact Evaluator')->notice('A new value to the field "'.$entity_field.'" was add to the Carbon Impact Evaluator table.');
} catch (Exception $e) {
\Drupal::logger('Carbon Impact Evaluator')->error('It was not possible to add a value to the field: '.$e->getMessage());
}
} else {
// Adds a message indicating it's not possible to add a value to this field
\Drupal::logger('Carbon Impact Evaluator')->notice('It was not possible to add a new value to the field "'.$entity_field.'" was add to the Carbon Impact Evaluator table.');
}
}
\ No newline at end of file
Loading